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

## LeetCode [1744. Can You Eat Your Favorite Candy on Your Favorite Day?](https://leetcode-cn.com/problems/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 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`

### 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: $$O(\max(m,n))$$, `m` and `n` are the length of two input arrays, respectively;
* Space complexity: $$O(\max(m,n))$$

### Code

```go
func canEat(candiesCount []int, queries [][]int) []bool {
	ans, smallerSum := make([]bool, len(queries)), make([]int, len(candiesCount))
	for i := 1; i < len(smallerSum); i++ {
		smallerSum[i] = candiesCount[i-1] + smallerSum[i-1]
	}
	for i, query := range queries {
		favoriteType, favoriteDay, dailyCap := query[0], query[1], query[2]
		earliest, latest := smallerSum[favoriteType]/dailyCap, smallerSum[favoriteType]+candiesCount[favoriteType]-1
		ans[i] = favoriteDay >= earliest && favoriteDay <= latest
	}
	return ans
}
```

## Reference

1. [你能在你最喜欢的那天吃到你最喜欢的糖果吗？](https://leetcode-cn.com/problems/can-you-eat-your-favorite-candy-on-your-favorite-day/solution/ni-neng-zai-ni-zui-xi-huan-de-na-tian-ch-boa0/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://txfs19260817.gitbook.io/leetcode-go-notes/solutions/1744.-can-you-eat-your-favorite-candy-on-your-favorite-day.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
