Children With the Most Treats
Problem
Each child has some treats. You hold extra additional treats and may give all of them to a single child. For each child, return whether giving them the extras would make them have at least as many as the current top child. Compute the maximum once, then test each child.
Input
treats = [4, 2, 7, 1], extra = 3Output
[true, false, true, false]Max is 7. With +3 extras: 7, 5, 10, 4 → ≥7? true, false, true, false.
def top_after_extras(treats, extra):
top = max(treats)
return [t + extra >= top for t in treats]
function topAfterExtras(treats, extra) {
const top = Math.max(...treats);
return treats.map(t => t + extra >= top);
}
class Solution {
public List<Boolean> topAfterExtras(int[] treats, int extra) {
int top = 0;
for (int t : treats) if (t > top) top = t;
List<Boolean> ans = new ArrayList<>();
for (int t : treats) ans.add(t + extra >= top);
return ans;
}
}
vector<bool> topAfterExtras(vector<int>& treats, int extra) {
int top = *max_element(treats.begin(), treats.end());
vector<bool> ans;
for (int t : treats) ans.push_back(t + extra >= top);
return ans;
}