Can You Eat Your Favorite Candy on Your Favorite Day Problem
Description
LeetCode Problem 1744.
You are given a (0-indexed) array of positive integers candiesCount where candiesCount[i] represents the number of candies of the i^th type you have. You are also given a 2D array queries where queries[i] = [favoriteType_i, favoriteDay_i, dailyCap_i].
You play a game with the following rules:
- You start eating candies on day
**0**. - You cannot eat any candy of type
iunless you have eaten all candies of typei - 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 favoriteType_i on day favoriteDay_i without eating more than dailyCap_i 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.
Example 1:
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:
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^51 <= candiesCount[i] <= 10^51 <= queries.length <= 10^5queries[i].length == 30 <= favoriteType_i < candiesCount.length0 <= favoriteDay_i <= 10^91 <= dailyCap_i <= 10^9
Solution
Approach: Prefix Sums and Range Overlap
Candies are eaten in order, type 0 first. With the prefix sums s of candiesCount (s[t] is the number of candies of types before t), the candies of type t are the ones numbered s[t] + 1 to s[t + 1] in eating order.
For a query (t, day, cap), with days counted from 0, think of the candy numbers that can be eaten on that day:
- At least 1 candy is eaten per day, so by the end of day
daythe count of eaten candies can be as low asday + 1. The smallest candy number we can reach is thereforeday + 1. - At most
capcandies are eaten per day, so the largest candy number we can reach on that day is(day + 1) * cap.
We can eat a candy of type t on that day exactly when the range [day + 1, (day + 1) * cap] overlaps the range [s[t] + 1, s[t + 1]]. This gives two comparisons: day + 1 <= s[t + 1] (that is, day < s[t + 1]) and (day + 1) * cap > s[t]. Use 64-bit integers where needed, because (day + 1) * cap can reach about 1014.
Complexity: O(n + q) time and O(n) extra space, for n candy types and q queries.
from itertools import accumulate
class Solution:
def canEat(self, candiesCount: List[int], queries: List[List[int]]) -> List[bool]:
s = list(accumulate(candiesCount, initial=0))
ans = []
for t, day, mx in queries:
least, most = day, (day + 1) * mx
ans.append(least < s[t + 1] and most > s[t])
return ans