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 leastone 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 thandailyCapi 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.
Input: candiesCount =[7,4,5,3,8], queries =[[0,2,2],[4,2,4],[2,13,1000000000]]Output: [true,false,true]Explanation:
1- If you eat 2candies(type 0) on day 0 and 2candies(type 0) on day 1, you will eat a candy of type 0 on day 2.2- You can eat at most 4 candies each day. If you eat 4 candies every day, you will eat 4candies(type 0) on day 0 and 4candies(type 0 and type 1) on day 1. On day 2, you can only eat 4candies(type 1 and type 2), so you cannot eat a candy of type 4 on day 2.3- If you eat 1 candy each day, you will eat a candy of type 2 on day 13.
To determine if you can eat your favorite candy on your favorite day, calculate the minimum and maximum number of candies you could have eaten by that day. Use prefix sums to quickly find how many candies of each type are available. The answer for each query is true if the ranges of possible candies eaten and the candies available for the favorite type overlap.
classSolution {
publicboolean[]canEat(int[] candiesCount, int[][] queries) {
int n = candiesCount.length;
long[] pre =newlong[n + 1];
for (int i = 0; i < n; i++) pre[i + 1]= pre[i]+ candiesCount[i];
boolean[] ans =newboolean[queries.length];
for (int i = 0; i < queries.length; i++) {
int t = queries[i][0], d = queries[i][1], cap = queries[i][2];
long minEat = d + 1;
long maxEat = (long)(d + 1) * cap;
long candyStart = pre[t]+ 1;
long candyEnd = pre[t + 1];
ans[i]= minEat <= candyEnd && maxEat >= candyStart;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funcanEat(candiesCount: IntArray, queries: Array<IntArray>): BooleanArray {
val n = candiesCount.size
val pre = LongArray(n + 1)
for (i in0 until n) pre[i + 1] = pre[i] + candiesCount[i]
val ans = BooleanArray(queries.size)
for (i in queries.indices) {
val(t, d, cap) = queries[i]
val minEat = d + 1Lval maxEat = (d + 1L) * cap
val candyStart = pre[t] + 1val candyEnd = pre[t + 1]
ans[i] = minEat <= candyEnd && maxEat >= candyStart
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defcanEat(self, candiesCount: list[int], queries: list[list[int]]) -> list[bool]:
n = len(candiesCount)
pre = [0] * (n +1)
for i in range(n):
pre[i +1] = pre[i] + candiesCount[i]
ans = []
for t, d, cap in queries:
min_eat = d +1 max_eat = (d +1) * cap
candy_start = pre[t] +1 candy_end = pre[t +1]
ans.append(min_eat <= candy_end and max_eat >= candy_start)
return ans
impl Solution {
pubfncan_eat(candies_count: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<bool> {
let n = candies_count.len();
letmut pre =vec![0i64; n +1];
for i in0..n {
pre[i +1] = pre[i] + candies_count[i] asi64;
}
queries
.iter()
.map(|q| {
let t = q[0] asusize;
let d = q[1] asi64;
let cap = q[2] asi64;
let min_eat = d +1;
let max_eat = (d +1) * cap;
let candy_start = pre[t] +1;
let candy_end = pre[t +1];
min_eat <= candy_end && max_eat >= candy_start
})
.collect()
}
}