Problem

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 arrayanswer.

Examples

Example 1

1
2
3
4
5
6
7
8
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 2 candies (type 0) on day 0 and 2 candies (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 4 candies (type 0) on day 0 and 4 candies (type 0 and type 1) on day 1.
   On day 2, you can only eat 4 candies (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.

Example 2

1
2
Input: candiesCount = [5,2,6,4,1], queries = [[3,1,2],[4,10,3],[3,10,100],[4,100,30],[1,3,1]]
Output: [false,true,true,false,false]

Constraints

  • 1 <= candiesCount.length <= 10^5
  • 1 <= candiesCount[i] <= 10^5
  • 1 <= queries.length <= 10^5
  • queries[i].length == 3
  • 0 <= favoriteTypei < candiesCount.length
  • 0 <= favoriteDayi <= 10^9
  • 1 <= dailyCapi <= 10^9

Solution

Method 1 – Prefix Sum and Range Overlap

Intuition

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.

Approach

  1. Compute prefix sums of candiesCount to get the total candies up to each type.
  2. For each query [favoriteType, favoriteDay, dailyCap]:
    • The earliest candy you could eat on favoriteDay is favoriteDay + 1 (eating 1 per day).
    • The latest is (favoriteDay + 1) * dailyCap (eating dailyCap per day).
    • The range of candies of favoriteType is from sum of candies before favoriteType + 1 to sum of candies up to favoriteType.
    • If the ranges overlap, answer is true; else, false.
  3. Return the answer array.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
public:
    vector<bool> canEat(vector<int>& candiesCount, vector<vector<int>>& queries) {
        int n = candiesCount.size();
        vector<long long> pre(n + 1);
        for (int i = 0; i < n; ++i) pre[i + 1] = pre[i] + candiesCount[i];
        vector<bool> ans;
        for (auto& q : queries) {
            int t = q[0], d = q[1], cap = q[2];
            long long minEat = d + 1;
            long long maxEat = (long long)(d + 1) * cap;
            long long candyStart = pre[t] + 1;
            long long candyEnd = pre[t + 1];
            ans.push_back(minEat <= candyEnd && maxEat >= candyStart);
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
func canEat(candiesCount []int, queries [][]int) []bool {
    n := len(candiesCount)
    pre := make([]int64, n+1)
    for i := 0; i < n; i++ {
        pre[i+1] = pre[i] + int64(candiesCount[i])
    }
    ans := make([]bool, len(queries))
    for i, q := range queries {
        t, d, cap := q[0], q[1], q[2]
        minEat := int64(d+1)
        maxEat := int64(d+1) * int64(cap)
        candyStart := pre[t] + 1
        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
class Solution {
    public boolean[] canEat(int[] candiesCount, int[][] queries) {
        int n = candiesCount.length;
        long[] pre = new long[n + 1];
        for (int i = 0; i < n; i++) pre[i + 1] = pre[i] + candiesCount[i];
        boolean[] ans = new boolean[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
class Solution {
    fun canEat(candiesCount: IntArray, queries: Array<IntArray>): BooleanArray {
        val n = candiesCount.size
        val pre = LongArray(n + 1)
        for (i in 0 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 + 1L
            val maxEat = (d + 1L) * cap
            val candyStart = pre[t] + 1
            val 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
class Solution:
    def canEat(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
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
impl Solution {
    pub fn can_eat(candies_count: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<bool> {
        let n = candies_count.len();
        let mut pre = vec![0i64; n + 1];
        for i in 0..n {
            pre[i + 1] = pre[i] + candies_count[i] as i64;
        }
        queries
            .iter()
            .map(|q| {
                let t = q[0] as usize;
                let d = q[1] as i64;
                let cap = q[2] as i64;
                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()
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
    canEat(candiesCount: number[], queries: number[][]): boolean[] {
        const n = candiesCount.length;
        const pre = new Array(n + 1).fill(0);
        for (let i = 0; i < n; i++) pre[i + 1] = pre[i] + candiesCount[i];
        const ans: boolean[] = [];
        for (const [t, d, cap] of queries) {
            const minEat = d + 1;
            const maxEat = (d + 1) * cap;
            const candyStart = pre[t] + 1;
            const candyEnd = pre[t + 1];
            ans.push(minEat <= candyEnd && maxEat >= candyStart);
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n + q)
  • 🧺 Space complexity: O(n)