3421. Find Students Who Improved LeetCode Solution
In this guide, you will get 3421. Find Students Who Improved LeetCode Solution with the best time and space complexity. The solution to Find Students Who Improved problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find Students Who Improved solution in C++
- Find Students Who Improved solution in Java
- Find Students Who Improved solution in Python
- Additional Resources
Problem Statement of Find Students Who Improved
Table: Scores
+————-+———+
| Column Name | Type |
+————-+———+
| student_id | int |
| subject | varchar |
| score | int |
| exam_date | varchar |
+————-+———+
(student_id, subject, exam_date) is the primary key for this table.
Each row contains information about a student’s score in a specific subject on a particular exam date. score is between 0 and 100 (inclusive).
Write a solution to find the students who have shown improvement. A student is considered to have shown improvement if they meet both of these conditions:
Have taken exams in the same subject on at least two different dates
Their latest score in that subject is higher than their first score
Return the result table ordered by student_id, subject in ascending order.
The result format is in the following example.
Example:
Input:
Scores table:
+————+———-+——-+————+
| student_id | subject | score | exam_date |
+————+———-+——-+————+
| 101 | Math | 70 | 2023-01-15 |
| 101 | Math | 85 | 2023-02-15 |
| 101 | Physics | 65 | 2023-01-15 |
| 101 | Physics | 60 | 2023-02-15 |
| 102 | Math | 80 | 2023-01-15 |
| 102 | Math | 85 | 2023-02-15 |
| 103 | Math | 90 | 2023-01-15 |
| 104 | Physics | 75 | 2023-01-15 |
| 104 | Physics | 85 | 2023-02-15 |
+————+———-+——-+————+
Output:
+————+———-+————-+————–+
| student_id | subject | first_score | latest_score |
+————+———-+————-+————–+
| 101 | Math | 70 | 85 |
| 102 | Math | 80 | 85 |
| 104 | Physics | 75 | 85 |
+————+———-+————-+————–+
Explanation:
Student 101 in Math: Improved from 70 to 85
Student 101 in Physics: No improvement (dropped from 65 to 60)
Student 102 in Math: Improved from 80 to 85
Student 103 in Math: Only one exam, not eligible
Student 104 in Physics: Improved from 75 to 85
Result table is ordered by student_id, subject.
Example not found
Constraints not found
Complexity Analysis
- Time Complexity: Google AdSense
- Space Complexity: Google Analytics
3421. Find Students Who Improved LeetCode Solution in C++
WITH
RankedScores AS (
SELECT
student_id,
subject,
score,
exam_date,
RANK() OVER (PARTITION BY student_id, subject ORDER BY exam_date) AS rn_asc,
RANK() OVER (PARTITION BY student_id, subject ORDER BY exam_date DESC) AS rn_desc
FROM Scores
),
FirstLastScores AS (
SELECT
student_id,
subject,
MIN(CASE WHEN rn_asc = 1 THEN score END) AS first_score,
MAX(CASE WHEN rn_desc = 1 THEN score END) AS latest_score
FROM RankedScores GROUP BY 1, 2
HAVING COUNT(*) > 1
)
SELECT student_id, subject, first_score, latest_score
FROM FirstLastScores
WHERE latest_score > first_score;
/* code provided by PROGIEZ */
3421. Find Students Who Improved LeetCode Solution in Java
N/A
// code provided by PROGIEZ
3421. Find Students Who Improved LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.