|
| 1 | +[](https://github.com/javadev/LeetCode-in-Kotlin) |
| 2 | +[](https://github.com/javadev/LeetCode-in-Kotlin/fork) |
| 3 | + |
| 4 | +## 3617\. Find Students with Study Spiral Pattern |
| 5 | + |
| 6 | +Hard |
| 7 | + |
| 8 | +Table: `students` |
| 9 | + |
| 10 | + +--------------+---------+ |
| 11 | + | Column Name | Type | |
| 12 | + +--------------+---------+ |
| 13 | + | student_id | int | |
| 14 | + | student_name | varchar | |
| 15 | + | major | varchar | |
| 16 | + +--------------+---------+ |
| 17 | + student_id is the unique identifier for this table. |
| 18 | + Each row contains information about a student and their academic major. |
| 19 | + |
| 20 | +Table: `study_sessions` |
| 21 | + |
| 22 | + +---------------+---------+ |
| 23 | + | Column Name | Type | |
| 24 | + +---------------+---------+ |
| 25 | + | session_id | int | |
| 26 | + | student_id | int | |
| 27 | + | subject | varchar | |
| 28 | + | session_date | date | |
| 29 | + | hours_studied | decimal | |
| 30 | + +---------------+---------+ |
| 31 | + session_id is the unique identifier for this table. |
| 32 | + Each row represents a study session by a student for a specific subject. |
| 33 | + |
| 34 | +Write a solution to find students who follow the **Study Spiral Pattern** - students who consistently study multiple subjects in a rotating cycle. |
| 35 | + |
| 36 | +* A Study Spiral Pattern means a student studies at least `3` **different subjects** in a repeating sequence |
| 37 | +* The pattern must repeat for **at least** `2` **complete cycles** (minimum `6` study sessions) |
| 38 | +* Sessions must be **consecutive dates** with no gaps longer than `2` days between sessions |
| 39 | +* Calculate the **cycle length** (number of different subjects in the pattern) |
| 40 | +* Calculate the **total study hours** across all sessions in the pattern |
| 41 | +* Only include students with cycle length of **at least** `3` **subjects** |
| 42 | + |
| 43 | +Return _the result table ordered by cycle length in **descending** order, then by total study hours in **descending** order_. |
| 44 | + |
| 45 | +The result format is in the following example. |
| 46 | + |
| 47 | +**Example:** |
| 48 | + |
| 49 | +**Input:** |
| 50 | + |
| 51 | +students table: |
| 52 | + |
| 53 | +| student_id | student_name | major | |
| 54 | +|------------|--------------|-------------------| |
| 55 | +| 1 | Alice Chen | Computer Science | |
| 56 | +| 2 | Bob Johnson | Mathematics | |
| 57 | +| 3 | Carol Davis | Physics | |
| 58 | +| 4 | David Wilson | Chemistry | |
| 59 | +| 5 | Emma Brown | Biology | |
| 60 | + |
| 61 | +study\_sessions table: |
| 62 | + |
| 63 | +| session_id | student_id | subject | session_date | hours_studied | |
| 64 | +|------------|------------|------------|--------------|----------------| |
| 65 | +| 1 | 1 | Math | 2023-10-01 | 2.5 | |
| 66 | +| 2 | 1 | Physics | 2023-10-02 | 3.0 | |
| 67 | +| 3 | 1 | Chemistry | 2023-10-03 | 2.0 | |
| 68 | +| 4 | 1 | Math | 2023-10-04 | 2.5 | |
| 69 | +| 5 | 1 | Physics | 2023-10-05 | 3.0 | |
| 70 | +| 6 | 1 | Chemistry | 2023-10-06 | 2.0 | |
| 71 | +| 7 | 2 | Algebra | 2023-10-01 | 4.0 | |
| 72 | +| 8 | 2 | Calculus | 2023-10-02 | 3.5 | |
| 73 | +| 9 | 2 | Statistics | 2023-10-03 | 2.5 | |
| 74 | +| 10 | 2 | Geometry | 2023-10-04 | 3.0 | |
| 75 | +| 11 | 2 | Algebra | 2023-10-05 | 4.0 | |
| 76 | +| 12 | 2 | Calculus | 2023-10-06 | 3.5 | |
| 77 | +| 13 | 2 | Statistics | 2023-10-07 | 2.5 | |
| 78 | +| 14 | 2 | Geometry | 2023-10-08 | 3.0 | |
| 79 | +| 15 | 3 | Biology | 2023-10-01 | 2.0 | |
| 80 | +| 16 | 3 | Chemistry | 2023-10-02 | 2.5 | |
| 81 | +| 17 | 3 | Biology | 2023-10-03 | 2.0 | |
| 82 | +| 18 | 3 | Chemistry | 2023-10-04 | 2.5 | |
| 83 | +| 19 | 4 | Organic | 2023-10-01 | 3.0 | |
| 84 | +| 20 | 4 | Physical | 2023-10-05 | 2.5 | |
| 85 | + |
| 86 | +**Output:** |
| 87 | + |
| 88 | +| student_id | student_name | major | cycle_length | total_study_hours | |
| 89 | +|------------|--------------|-------------------|--------------|-------------------| |
| 90 | +| 2 | Bob Johnson | Mathematics | 4 | 26.0 | |
| 91 | +| 1 | Alice Chen | Computer Science | 3 | 15.0 | |
| 92 | + |
| 93 | +**Explanation:** |
| 94 | + |
| 95 | +* **Alice Chen (student\_id = 1):** |
| 96 | + * Study sequence: Math → Physics → Chemistry → Math → Physics → Chemistry |
| 97 | + * Pattern: 3 subjects (Math, Physics, Chemistry) repeating for 2 complete cycles |
| 98 | + * Consecutive dates: Oct 1-6 with no gaps > 2 days |
| 99 | + * Cycle length: 3 subjects |
| 100 | + * Total hours: 2.5 + 3.0 + 2.0 + 2.5 + 3.0 + 2.0 = 15.0 hours |
| 101 | +* **Bob Johnson (student\_id = 2):** |
| 102 | + * Study sequence: Algebra → Calculus → Statistics → Geometry → Algebra → Calculus → Statistics → Geometry |
| 103 | + * Pattern: 4 subjects (Algebra, Calculus, Statistics, Geometry) repeating for 2 complete cycles |
| 104 | + * Consecutive dates: Oct 1-8 with no gaps > 2 days |
| 105 | + * Cycle length: 4 subjects |
| 106 | + * Total hours: 4.0 + 3.5 + 2.5 + 3.0 + 4.0 + 3.5 + 2.5 + 3.0 = 26.0 hours |
| 107 | +* **Students not included:** |
| 108 | + * Carol Davis (student\_id = 3): Only 2 subjects (Biology, Chemistry) - doesn't meet minimum 3 subjects requirement |
| 109 | + * David Wilson (student\_id = 4): Only 2 study sessions with a 4-day gap - doesn't meet consecutive dates requirement |
| 110 | + * Emma Brown (student\_id = 5): No study sessions recorded |
| 111 | + |
| 112 | +The result table is ordered by cycle\_length in descending order, then by total\_study\_hours in descending order. |
| 113 | + |
| 114 | +## Solution |
| 115 | + |
| 116 | +```sql |
| 117 | +# Write your MySQL query statement below |
| 118 | +-- WITH studentstudysummary AS ( |
| 119 | +WITH studentstudysummary AS ( |
| 120 | + SELECT |
| 121 | + student_id, |
| 122 | + SUM(hours_studied) AS total_study_hours, |
| 123 | + COUNT(DISTINCT subject) AS cycle_length |
| 124 | + FROM |
| 125 | + study_sessions |
| 126 | + GROUP BY |
| 127 | + student_id |
| 128 | + HAVING |
| 129 | + COUNT(DISTINCT subject) >= 3 |
| 130 | +), |
| 131 | +rankedstudysessionswithgaps AS ( |
| 132 | + SELECT |
| 133 | + ss.student_id, |
| 134 | + ss.subject, |
| 135 | + ss.session_date, |
| 136 | + DATEDIFF( |
| 137 | + LEAD(ss.session_date, 1, ss.session_date) |
| 138 | + OVER (PARTITION BY ss.student_id ORDER BY ss.session_date), |
| 139 | + ss.session_date |
| 140 | + ) AS gap_to_next_session, |
| 141 | + ROW_NUMBER() OVER (PARTITION BY ss.student_id ORDER BY ss.session_date) AS rn, |
| 142 | + sss.total_study_hours, |
| 143 | + sss.cycle_length |
| 144 | + FROM |
| 145 | + study_sessions ss |
| 146 | + INNER JOIN studentstudysummary sss |
| 147 | + ON ss.student_id = sss.student_id |
| 148 | +), |
| 149 | +cyclicstudents AS ( |
| 150 | + SELECT |
| 151 | + rss1.student_id, |
| 152 | + rss1.cycle_length, |
| 153 | + rss1.total_study_hours |
| 154 | + FROM |
| 155 | + rankedstudysessionswithgaps rss1 |
| 156 | + INNER JOIN rankedstudysessionswithgaps rss2 |
| 157 | + ON rss1.student_id = rss2.student_id |
| 158 | + AND rss2.rn = rss1.rn + rss1.cycle_length |
| 159 | + AND rss1.subject = rss2.subject |
| 160 | + WHERE |
| 161 | + rss1.gap_to_next_session < 3 |
| 162 | + AND rss2.gap_to_next_session < 3 |
| 163 | + GROUP BY |
| 164 | + rss1.student_id, |
| 165 | + rss1.cycle_length, |
| 166 | + rss1.total_study_hours |
| 167 | + HAVING |
| 168 | + COUNT(DISTINCT rss1.subject) >= 3 |
| 169 | +) |
| 170 | +SELECT |
| 171 | + s.student_id, |
| 172 | + s.student_name, |
| 173 | + s.major, |
| 174 | + cs.cycle_length, |
| 175 | + cs.total_study_hours |
| 176 | +FROM |
| 177 | + cyclicstudents cs |
| 178 | + INNER JOIN students s |
| 179 | + ON cs.student_id = s.student_id |
| 180 | +ORDER BY |
| 181 | + cs.cycle_length DESC, |
| 182 | + cs.total_study_hours DESC; |
| 183 | +``` |
0 commit comments