LeetCode 1431: Kids With the Greatest Number of Candies
Problem Description
Explanation:
To solve this problem, we need to find the kid(s) who will have the greatest number of candies after adding the extra candies. We can achieve this by finding the maximum number of candies among all kids first. Then, we iterate through each kid and check if adding the extra candies to their current candies count would make them have the greatest number of candies.
-
Algorithm:
- Find the maximum number of candies among all kids.
- Iterate through each kid and check if adding the extra candies to their current candies count would make them have the greatest number of candies.
- Store the result in a boolean array.
-
Time Complexity: O(n) where n is the number of kids.
-
Space Complexity: O(n) for storing the result array.
:
Solutions
class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
List<Boolean> result = new ArrayList<>();
int maxCandies = Arrays.stream(candies).max().getAsInt();
for (int i = 0; i < candies.length; i++) {
result.add(candies[i] + extraCandies >= maxCandies);
}
return result;
}
}
Loading editor...