1744. Can You Eat Your Favorite Candy on Your Favorite Day

1744. Can You Eat Your Favorite Candy on Your Favorite Day? #

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 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 <= 105
  • 1 <= candiesCount[i] <= 105
  • 1 <= queries.length <= 105
  • queries[i].length == 3
  • 0 <= favoriteTypei < candiesCount.length
  • 0 <= favoriteDayi <= 109
  • 1 <= dailyCapi <= 109

Problem Summary #

You are given a positive integer array candiesCount with indices starting from 0, where candiesCount[i] represents the number of candies you have of type i. You are also given a 2D array queries, where queries[i] = [favoriteTypei, favoriteDayi, dailyCapi]. You play a game according to the following rules:

  • You start eating candies on day 0.
  • Before eating all candies of type i - 1, you cannot eat any candy of type i.
  • Before you have eaten all the candies, you must eat at least one candy every day.

Please construct a boolean array answer such that answer.length == queries.length. The condition for answer[i] to be true is: under the premise of eating no more than dailyCapi candies per day, you can eat a candy of type favoriteTypei on day favoriteDayi; otherwise answer[i] is false. Note that as long as the second of the above 3 rules is satisfied, you can eat different types of candies on the same day. Please return the resulting array answer.

Solution Approach #

  • The lower bound on the number of candies eaten per day is 1, and the upper bound is dailyCap. For each query, determine whether you can eat a candy of type i on day i. To eat a candy of type i, you must have finished all candies of type i-1. This means checking whether the interval [favoriteDayi + 1, (favoriteDayi+1)×dailyCapi] can contain a candy of type favoriteTypei. If it can, output true; otherwise output false. The number of candies eaten is cumulative, so here we use prefix sums to compute the interval where the cumulative number of eaten candies falls: [sum[favoriteTypei−1]+1, sum[favoriteTypei]]. Finally, determine whether the query interval and the cumulative candy count interval overlap. If they overlap, output true.
  • There are several cases for determining whether two intervals overlap: one contains the other, complete containment, partial containment, and so on. There are fewer cases with no intersection, so we can use exclusion. For intervals [x1, y1] and [x2, y2], they have no intersection if and only if x1 > y2 or y1 < x2.

Code #

package leetcode

func canEat(candiesCount []int, queries [][]int) []bool {
	n := len(candiesCount)
	prefixSum := make([]int, n)
	prefixSum[0] = candiesCount[0]
	for i := 1; i < n; i++ {
		prefixSum[i] = prefixSum[i-1] + candiesCount[i]
	}
	res := make([]bool, len(queries))
	for i, q := range queries {
		favoriteType, favoriteDay, dailyCap := q[0], q[1], q[2]
		x1 := favoriteDay + 1
		y1 := (favoriteDay + 1) * dailyCap
		x2 := 1
		if favoriteType > 0 {
			x2 = prefixSum[favoriteType-1] + 1
		}
		y2 := prefixSum[favoriteType]
		res[i] = !(x1 > y2 || y1 < x2)
	}
	return res
}

Calendar Jun 25, 2026
Edit Edit this page
Total visits:   You are visitor No.
中文