LeetCode 2072 - The Winner University
The problem gives us two database tables, NewYork and California. Each table contains the exam scores of students from a university. Every row represents a single student, identified by a unique studentid, along with their score. The competition rule is straightforward.
Difficulty: 🟢 Easy
Topics: Database
Solution
Problem Understanding
The problem gives us two database tables, NewYork and California. Each table contains the exam scores of students from a university. Every row represents a single student, identified by a unique student_id, along with their score.
The competition rule is straightforward. A student is considered an excellent student if their score is greater than or equal to 90. The university with more excellent students wins the competition.
The output must contain exactly one row with a single column named winner. The possible results are:
"New York University"if New York has more excellent students"California University"if California has more excellent students"No Winner"if both universities have the same number of excellent students
The problem is essentially asking us to:
- Count how many students in
NewYorkhave scores greater than or equal to90 - Count how many students in
Californiahave scores greater than or equal to90 - Compare the two counts
- Return the correct winner string based on the comparison
The constraints are small and simple because this is an SQL Easy problem. There are no joins between tables, no duplicate student IDs within the same table, and no complicated filtering logic. The important observation is that we only care about whether a score is at least 90, not the exact value of the score itself.
Several edge cases are important to consider. One university might have zero excellent students while the other has some. Both universities might have zero excellent students, which should produce "No Winner". Another important case is when both universities have the same number of excellent students even if their total scores differ significantly. Only the count of students scoring at least 90 matters.
Approaches
Brute Force Approach
The brute force idea is to scan every row from both tables manually and keep track of the number of excellent students using procedural logic.
Conceptually, we would:
- Iterate through every row in
NewYork - Increment a counter whenever
score >= 90 - Repeat the same process for
California - Compare the two counters
- Return the appropriate winner string
This approach is correct because it explicitly evaluates every student and counts all excellent students accurately.
However, in SQL, manually simulating iteration is unnecessary and inefficient. SQL is designed for set-based operations, so procedural counting would be verbose and less elegant than aggregation functions.
Optimal Approach
The optimal solution uses SQL aggregation with conditional counting.
The key insight is that we only need two numbers:
- The number of excellent students in
NewYork - The number of excellent students in
California
SQL aggregation functions such as COUNT combined with conditional logic allow us to compute these counts efficiently in a single query.
After obtaining the two counts, we can use a CASE expression to determine the winner.
This approach is optimal because SQL databases are highly optimized for aggregate operations like counting and filtering.
| Approach | Time Complexity | Space Complexity | Notes |
|---|---|---|---|
| Brute Force | O(N + M) | O(1) | Iterates manually through both tables |
| Optimal | O(N + M) | O(1) | Uses SQL aggregation and conditional logic |
Here, N is the number of rows in NewYork, and M is the number of rows in California.
Algorithm Walkthrough
- Count the number of excellent students in the
NewYorktable by checking how many rows satisfyscore >= 90. - Count the number of excellent students in the
Californiatable using the same condition. - Compare the two counts using a
CASEstatement. - If New York's count is greater, return
"New York University". - If California's count is greater, return
"California University". - Otherwise, return
"No Winner"because the competition ends in a draw.
Why it works
The algorithm works because the competition winner depends entirely on the number of students scoring at least 90. By counting exactly those students in each table and comparing the totals, we directly implement the competition rules described in the problem statement. Since every student is evaluated exactly once, the counts are accurate, and the comparison always produces the correct result.
Python Solution
Although this is a Database problem intended for SQL, the following Python implementation demonstrates the same logic programmatically.
from typing import List
class Solution:
def findWinner(
self,
new_york_scores: List[int],
california_scores: List[int]
) -> str:
new_york_excellent = sum(
score >= 90 for score in new_york_scores
)
california_excellent = sum(
score >= 90 for score in california_scores
)
if new_york_excellent > california_excellent:
return "New York University"
if california_excellent > new_york_excellent:
return "California University"
return "No Winner"
The implementation first counts excellent students for both universities. In Python, boolean expressions evaluate to integers, where True equals 1 and False equals 0. This allows sum(score >= 90 for score in scores) to count excellent students directly.
After computing both counts, the code compares them with simple conditional statements and returns the correct winner string.
For the actual LeetCode Database submission, the SQL solution would look like this:
SELECT
CASE
WHEN ny_count > ca_count THEN 'New York University'
WHEN ca_count > ny_count THEN 'California University'
ELSE 'No Winner'
END AS winner
FROM
(
SELECT
(SELECT COUNT(*)
FROM NewYork
WHERE score >= 90) AS ny_count,
(SELECT COUNT(*)
FROM California
WHERE score >= 90) AS ca_count
) counts;
This SQL query computes both counts using subqueries and then determines the winner with a CASE expression.
Go Solution
package main
func findWinner(newYorkScores []int, californiaScores []int) string {
newYorkExcellent := 0
for _, score := range newYorkScores {
if score >= 90 {
newYorkExcellent++
}
}
californiaExcellent := 0
for _, score := range californiaScores {
if score >= 90 {
californiaExcellent++
}
}
if newYorkExcellent > californiaExcellent {
return "New York University"
}
if californiaExcellent > newYorkExcellent {
return "California University"
}
return "No Winner"
}
The Go implementation follows the same logic as the Python version. Instead of using Python's boolean summation shortcut, Go explicitly increments counters whenever a score is greater than or equal to 90.
Go does not support implicit boolean-to-integer conversion, so manual counting is necessary. Otherwise, the algorithm and complexity remain identical.
Worked Examples
Example 1
Input:
NewYork = [90, 87]
California = [89, 88]
Step-by-step execution
| University | Score | Excellent? | Running Count |
|---|---|---|---|
| New York | 90 | Yes | 1 |
| New York | 87 | No | 1 |
| California | 89 | No | 0 |
| California | 88 | No | 0 |
Final counts:
- New York excellent students = 1
- California excellent students = 0
Since 1 > 0, the result is:
New York University
Example 2
Input:
NewYork = [89, 88]
California = [90, 87]
Step-by-step execution
| University | Score | Excellent? | Running Count |
|---|---|---|---|
| New York | 89 | No | 0 |
| New York | 88 | No | 0 |
| California | 90 | Yes | 1 |
| California | 87 | No | 1 |
Final counts:
- New York excellent students = 0
- California excellent students = 1
Since 1 > 0, the result is:
California University
Example 3
Input:
NewYork = [89, 90]
California = [87, 99]
Step-by-step execution
| University | Score | Excellent? | Running Count |
|---|---|---|---|
| New York | 89 | No | 0 |
| New York | 90 | Yes | 1 |
| California | 87 | No | 0 |
| California | 99 | Yes | 1 |
Final counts:
- New York excellent students = 1
- California excellent students = 1
Since the counts are equal, the result is:
No Winner
Complexity Analysis
| Measure | Complexity | Explanation |
|---|---|---|
| Time | O(N + M) | Each student record is checked exactly once |
| Space | O(1) | Only a few integer counters are used |
The algorithm scans both datasets once and performs constant-time work for each student. No additional data structures proportional to input size are required, so the space usage remains constant.
Test Cases
def find_winner(new_york_scores, california_scores):
new_york_excellent = sum(score >= 90 for score in new_york_scores)
california_excellent = sum(score >= 90 for score in california_scores)
if new_york_excellent > california_excellent:
return "New York University"
if california_excellent > new_york_excellent:
return "California University"
return "No Winner"
# Example 1
assert find_winner([90, 87], [89, 88]) == "New York University" # NY wins
# Example 2
assert find_winner([89, 88], [90, 87]) == "California University" # California wins
# Example 3
assert find_winner([89, 90], [87, 99]) == "No Winner" # Tie case
# Both universities have no excellent students
assert find_winner([50, 60], [70, 80]) == "No Winner" # Both zero
# All students are excellent
assert find_winner([95, 98], [90, 91]) == "No Winner" # Equal counts
# New York dominates
assert find_winner([90, 91, 92], [50, 60, 70]) == "New York University" # NY higher
# California dominates
assert find_winner([10, 20], [95, 96]) == "California University" # California higher
# Single student tie
assert find_winner([90], [99]) == "No Winner" # Both have one excellent student
# Boundary value exactly 90
assert find_winner([90], [89]) == "New York University" # 90 counts as excellent
# Empty inputs
assert find_winner([], []) == "No Winner" # No students
| Test | Why |
|---|---|
[90, 87] vs [89, 88] |
Validates New York winning |
[89, 88] vs [90, 87] |
Validates California winning |
[89, 90] vs [87, 99] |
Validates draw condition |
[50, 60] vs [70, 80] |
Ensures zero excellent students handled correctly |
[95, 98] vs [90, 91] |
Ensures equal nonzero counts handled correctly |
[90, 91, 92] vs [50, 60, 70] |
Tests strong New York advantage |
[10, 20] vs [95, 96] |
Tests strong California advantage |
[90] vs [99] |
Tests smallest nontrivial tie |
[90] vs [89] |
Verifies boundary condition at exactly 90 |
[] vs [] |
Tests empty input handling |
Edge Cases
One important edge case occurs when both universities have zero excellent students. A naive implementation might incorrectly assume that at least one university must win. However, the problem clearly states that equal counts result in "No Winner". The implementation handles this naturally because both counters remain zero and the final return statement executes.
Another critical edge case is the boundary score of exactly 90. Since the definition says students scoring 90% or more are excellent, the comparison must use >= 90 instead of > 90. Using the wrong comparison operator would incorrectly exclude students with exactly 90.
A third edge case involves equal numbers of excellent students despite very different overall scores. For example, one university could have many high scores below 90, while the other has exactly one score above 90. Only the count of excellent students matters. The implementation correctly ignores all scores below 90 and compares only the qualifying counts.
A final edge case is empty input datasets. Although the database version typically guarantees rows exist, the algorithm still behaves correctly if both inputs are empty. Both excellent-student counts become zero, producing "No Winner" safely without errors.