Can You Eat Your Favorite Candy on Your Favorite Day? LeetCode solution in C++

1
class Solution {
2
public:
3
vector<bool> canEat(vector<int> &candiesCount, vector<vector<int>> &queries) {
4
/// lower bound and higher bound
5
// lower bound: eat one per day, on favourite day, the candy type i does not
6
// run out higher bound: eat dailycap, on favourite day, the cap reaches
7
// type i
8

9
// type + day + cap
10

11
vector<bool> outcome;
12

13
vector<long int> candyIndex(candiesCount.size());
14
long int x = 0;
15
for (int i = 0; i < candiesCount.size(); i++) {
16
candyIndex[i] = x;
17
x += candiesCount[i];
18
}
19

20
for (int i = 0; i < queries.size(); i++) {
21
long int highBound = (long int)queries[i][2] * (queries[i][1] + 1),
22
lowBound = queries[i][1] + 1;
23

24
bool answer = candyIndex[queries[i][0]] < highBound &&
25
(candyIndex[queries[i][0]] + candiesCount[queries[i][0]] - 1) >= (lowBound - 1);
26
outcome.push_back(answer);
27
}
28
return outcome;
29
}
30
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0