Find Cutoff Score for Each School Problem
Description
LeetCode Problem 1988.
Table: Schools
+-------------+------+
| Column Name | Type |
+-------------+------+
| school_id | int |
| capacity | int |
+-------------+------+
school_id is the primary key for this table.
This table contains information about the capacity of some schools. The capacity is the maximum number of students the school can accept.
Table: Exam
+---------------+------+
| Column Name | Type |
+---------------+------+
| score | int |
| student_count | int |
+---------------+------+
score is the primary key for this table.
Each row in this table indicates that there are student_count students that got at least score points in the exam.
The data in this table will be logically correct, meaning a row recording a higher score will have the same or smaller student_count compared to a row recording a lower score. More formally, for every two rows i and j in the table, if scorei > scorej then student_counti <= student_countj.
Every year, each school announces a minimum score requirement that a student needs to apply to it. The school chooses the minimum score requirement based on the exam results of all the students:
- They want to ensure that even if every student meeting the requirement applies, the school can accept everyone.
- They also want to maximize the possible number of students that can apply.
- They must use a score that is in the Exam table.
Write an SQL query to report the minimum score requirement for each school. If there are multiple score values satisfying the above conditions, choose the smallest one. If the input data is not enough to determine the score, report -1.
Return the result table in any order.
The query result format is in the following example.
Example 1:
Input:
Schools table:
+-----------+----------+
| school_id | capacity |
+-----------+----------+
| 11 | 151 |
| 5 | 48 |
| 9 | 9 |
| 10 | 99 |
+-----------+----------+
Exam table:
+-------+---------------+
| score | student_count |
+-------+---------------+
| 975 | 10 |
| 966 | 60 |
| 844 | 76 |
| 749 | 76 |
| 744 | 100 |
+-------+---------------+
Output:
+-----------+-------+
| school_id | score |
+-----------+-------+
| 5 | 975 |
| 9 | -1 |
| 10 | 749 |
| 11 | 744 |
+-----------+-------+
Explanation:
- School 5: The school's capacity is 48. Choosing 975 as the min score requirement, the school will get at most 10 applications, which is within capacity.
- School 10: The school's capacity is 99. Choosing 844 or 749 as the min score requirement, the school will get at most 76 applications, which is within capacity. We choose the smallest of them, which is 749.
- School 11: The school's capacity is 151. Choosing 744 as the min score requirement, the school will get at most 100 applications, which is within capacity.
- School 9: The data given is not enough to determine the min score requirement. Choosing 975 as the min score, the school may get 10 requests while its capacity is 9. We do not have information about higher scores, hence we report -1.
Solution
Approach: Smallest Score That Fits, with COALESCE
Choosing a requirement score means up to student_count students can apply, so the school can only use the scores with student_count <= capacity.
Among those, the school wants the most possible applicants. Exam is monotonic: a lower score never has fewer students. So within the scores that fit, the lowest score has the largest student_count, and if several scores share that count (844 and 749 both have 76), the problem asks for the smallest one anyway. Both goals are met by one value: MIN(score) over the rows with student_count <= capacity.
A correlated subquery computes that for each school. When no score fits (school 9: even the top score has 10 students, above its capacity of 9), MIN over no rows is NULL, and COALESCE(..., -1) turns it into the required -1.
The Pandas version cross-merges schools with exam rows, keeps the pairs within capacity, takes the minimum score per school, and maps it onto all schools with fillna(-1).
SELECT s.school_id,
-- Lowest score whose applicants still fit; -1 when no score fits.
COALESCE(
(SELECT MIN(e.score)
FROM Exam AS e
WHERE e.student_count <= s.capacity),
-1
) AS score
FROM Schools AS s;
Now write it from memory
Close the write-up and solve it yourself against the tests.