[LeetCode] Can You Eat Your Favorite Candy on Your Favorite Day?

1744. Can You Eat Your Favorite Candy on Your Favorite Day?

You are given a (0-indexed) array of positive integers candiesCount where candiesCount[i] represents the number of candies of the ith type you have. You are also given a 2D array queries where queries[i] = [favoriteTypei, favoriteDayi, dailyCapi].

You play a game with the following rules:

  • You start eating candies on day 0.
  • You cannot eat any candy of type i unless you have eaten all candies of type i - 1.
  • You must eat at least one candy per day until you have eaten all the candies.

Construct a boolean array answer such that answer.length == queries.length and answer[i] is true if you can eat a candy of type favoriteTypei on day favoriteDayi without eating more than dailyCapi candies on any day, and false otherwise. Note that you can eat different types of candy on the same day, provided that you follow rule 2.

Return the constructed array answer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
vector<bool> canEat(vector<int>& ca, vector<vector<int>>& queries) {
vector<long> sum(1);
vector<bool> res;

for(auto& c : ca) {
sum.push_back(sum.back() + c);
}

for(auto& q : queries) {
res.push_back(sum[q[0] + 1] > q[1] and sum[q[0]] < 1l * q[2] * (q[1] + 1));
}

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/12/21/PS/LeetCode/can-you-eat-your-favorite-candy-on-your-favorite-day/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.