1744. Can You Eat Your Favorite Candy on Your Favorite Day?
Description
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 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 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
.
Example 1:
Example 2:
Constraints:
1 <= candiesCount.length <= 105
1 <= candiesCount[i] <= 105
1 <= queries.length <= 105
queries[i].length == 3
0 <= favoriteTypei < candiesCount.length
0 <= favoriteDayi <= 109
1 <= dailyCapi <= 109
Tags
Math
Solution
The query is true if and only if your favorite day is in between the earliest and latest possible days to eat your favorite candy. The latest possible day is the total number of candies with all smaller types plus the number of your favorite candy minus 1. The earliest possible day that you can eat your favorite candy is the total number of candies with all smaller types divided by dailyCap.
To accelerate the sum of smaller types candies, we can pre-compute and obtain the prefix-sum array of candiesCount
.
Complexity
Time complexity: ,
m
andn
are the length of two input arrays, respectively;Space complexity:
Code
Reference
Last updated
Was this helpful?