Skip to content

Commit 67215fa

Browse files
committed
Add Python file with SQL solution for problem 3764 (frontend compatibility)
1 parent ae2788d commit 67215fa

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

solutions/3764/01.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Solution for LeetCode 3764: Most Common Course Pairs
2+
# Find skill mastery pathways by analyzing course completion sequences among top-performing students
3+
4+
"""
5+
SQL Solution:
6+
7+
WITH top_performers AS (
8+
-- Step 1: Identify top-performing students
9+
-- Must have at least 5 courses with average rating >= 4
10+
SELECT
11+
user_id
12+
FROM
13+
course_completions
14+
GROUP BY
15+
user_id
16+
HAVING
17+
COUNT(*) >= 5
18+
AND AVG(course_rating) >= 4
19+
),
20+
ordered_courses AS (
21+
-- Step 2: Get courses for top performers in chronological order
22+
SELECT
23+
cc.user_id,
24+
cc.course_name,
25+
cc.completion_date,
26+
ROW_NUMBER() OVER (
27+
PARTITION BY cc.user_id
28+
ORDER BY cc.completion_date
29+
) AS course_order
30+
FROM
31+
course_completions cc
32+
INNER JOIN
33+
top_performers tp ON cc.user_id = tp.user_id
34+
),
35+
course_pairs AS (
36+
-- Step 3: Create consecutive course pairs
37+
SELECT
38+
oc1.course_name AS first_course,
39+
oc2.course_name AS second_course
40+
FROM
41+
ordered_courses oc1
42+
INNER JOIN
43+
ordered_courses oc2
44+
ON oc1.user_id = oc2.user_id
45+
AND oc2.course_order = oc1.course_order + 1
46+
)
47+
-- Step 4: Count pair frequencies and order results
48+
SELECT
49+
first_course,
50+
second_course,
51+
COUNT(*) AS transition_count
52+
FROM
53+
course_pairs
54+
GROUP BY
55+
first_course,
56+
second_course
57+
ORDER BY
58+
transition_count DESC,
59+
first_course ASC,
60+
second_course ASC;
61+
"""
62+

0 commit comments

Comments
 (0)