Skip to content

Commit fc4f384

Browse files
committed
Added task 3617
1 parent ce0198f commit fc4f384

File tree

3 files changed

+200
-52
lines changed
  • src/main/kotlin
    • g3501_3600/s3586_find_covid_recovery_patients
    • g3601_3700/s3617_find_students_with_study_spiral_pattern

3 files changed

+200
-52
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,7 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3617 |[Find Students with Study Spiral Pattern](src/main/kotlin/g3601_3700/s3617_find_students_with_study_spiral_pattern)| Hard | Database | 553 | 100.00
20912092
| 3615 |[Longest Palindromic Path in Graph](src/main/kotlin/g3601_3700/s3615_longest_palindromic_path_in_graph)| Hard | String, Dynamic_Programming, Bit_Manipulation, Graph | 635 | 100.00
20922093
| 3614 |[Process String with Special Operations II](src/main/kotlin/g3601_3700/s3614_process_string_with_special_operations_ii)| Hard | String, Simulation | 37 | 100.00
20932094
| 3613 |[Minimize Maximum Component Cost](src/main/kotlin/g3601_3700/s3613_minimize_maximum_component_cost)| Medium | Binary_Search, Graph, Union_Find, Sort | 34 | 100.00

src/main/kotlin/g3501_3600/s3586_find_covid_recovery_patients/readme.md

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -111,61 +111,25 @@ Output table is ordered by recovery\_time in ascending order, and then by patien
111111

112112
```sql
113113
# Write your MySQL query statement below
114-
-- mysql
115114
-- SELECT
116-
-- p.patient_id,
117-
-- p.patient_name,
118-
-- p.age,
119-
-- DATEDIFF(
120-
-- min(neg.test_date),
121-
-- min(pos.test_date)
122-
-- ) AS recovery_time
123-
-- FROM
124-
-- patients p
125-
-- JOIN covid_tests pos ON
126-
-- p.patient_id = pos.patient_id AND pos.result = 'Positive'
127-
-- JOIN covid_tests neg ON
128-
-- p.patient_id = neg.patient_id AND neg.result = 'Negative'
129-
-- WHERE
130-
-- neg.test_date > pos.test_date
131-
-- GROUP BY
132-
-- p.patient_id, p.patient_name, p.age
133-
-- ORDER BY
134-
-- recovery_time, p.patient_name;
135-
select
115+
SELECT
136116
p.patient_id,
137117
p.patient_name,
138118
p.age,
139-
datediff(
140-
day,
141-
pos.first_pos_date,
142-
neg.first_neg_date
143-
) as recovery_time
144-
from
119+
DATEDIFF(
120+
min(neg.test_date),
121+
min(pos.test_date)
122+
) AS recovery_time
123+
FROM
145124
patients p
146-
join (
147-
select patient_id, min(test_date) as first_pos_date
148-
from covid_tests
149-
where result = 'Positive'
150-
group by patient_id
151-
) pos on p.patient_id = pos.patient_id
152-
join (
153-
select
154-
c1.patient_id,
155-
min(c1.test_date) as first_neg_date
156-
from
157-
covid_tests c1
158-
join (
159-
select patient_id, min(test_date) as first_pos_date
160-
from covid_tests
161-
where result = 'Positive'
162-
group by patient_id
163-
) p2 on c1.patient_id = p2.patient_id
164-
where
165-
c1.result = 'Negative'
166-
and c1.test_date > p2.first_pos_date
167-
group by c1.patient_id
168-
) neg on p.patient_id = neg.patient_id
169-
order by
170-
recovery_time ASC, p.patient_name ASC;
125+
JOIN covid_tests pos ON
126+
p.patient_id = pos.patient_id AND pos.result = 'Positive'
127+
JOIN covid_tests neg ON
128+
p.patient_id = neg.patient_id AND neg.result = 'Negative'
129+
WHERE
130+
neg.test_date > pos.test_date
131+
GROUP BY
132+
p.patient_id, p.patient_name, p.age
133+
ORDER BY
134+
recovery_time, p.patient_name;
171135
```
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](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

Comments
 (0)