Leetcode 3238. Find the Number of Winning Players
LeetCode Problem 3238: Find the Number of Winning Players
Given an integer array scores
that represents the scores of players in a competition, you are asked to determine the number of winning players.
A player is considered winning if their score is strictly greater than the average score of all players.
Return the count of winning players.
Input: scores = [1, 2, 3, 4, 5]
Output: 2
Explanation: The average score is 3, and the players with scores 4 and 5 are the winning players.
scores
array?
scores
array be empty?
The core steps to solving this problem are:
scores
array and count the number of players whose scores are strictly greater than the calculated average.scores
array.scores
array.public class NumberOfWinningPlayers {
public int findWinningPlayers(int[] scores) {
if (scores == null || scores.length == 0) {
return 0;
}
// Calculate the average score
double sum = 0;
for (int score : scores) {
sum += score;
}
double average = sum / scores.length;
// Count the number of players with scores greater than the average
int winningPlayersCount = 0;
for (int score : scores) {
if (score > average) {
winningPlayersCount++;
}
}
return winningPlayersCount;
}
public static void main(String[] args) {
NumberOfWinningPlayers solution = new NumberOfWinningPlayers();
int[] scores = {1, 2, 3, 4, 5};
System.out.println(solution.findWinningPlayers(scores)); // Output: 2
}
}
The time complexity of this solution is O(n), where n is the number of elements in the scores
array. Here’s why:
Feel free to ask if any more clarifying questions are needed or if additional explanations on any part of the problem or solution would be helpful!
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?