|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../restore-the-array-from-adjacent-pairs "Restore the Array From Adjacent Pairs") |
| 9 | + |
| 10 | +[Next >](../palindrome-partitioning-iv "Palindrome Partitioning IV") |
| 11 | + |
| 12 | +## [1744. Can You Eat Your Favorite Candy on Your Favorite Day? (Medium)](https://leetcode.com/problems/can-you-eat-your-favorite-candy-on-your-favorite-day "你能在你最喜欢的那天吃到你最喜欢的糖果吗?") |
| 13 | + |
| 14 | +<p>You are given a <strong>(0-indexed)</strong> array of positive integers <code>candiesCount</code> where <code>candiesCount[i]</code> represents the number of candies of the <code>i<sup>th</sup></code> type you have. You are also given a 2D array <code>queries</code> where <code>queries[i] = [favoriteType<sub>i</sub>, favoriteDay<sub>i</sub>, dailyCap<sub>i</sub>]</code>.</p> |
| 15 | + |
| 16 | +<p>You play a game with the following rules:</p> |
| 17 | + |
| 18 | +<ul> |
| 19 | + <li>You start eating candies on day <code><strong>0</strong></code>.</li> |
| 20 | + <li>You <b>cannot</b> eat <strong>any</strong> candy of type <code>i</code> unless you have eaten <strong>all</strong> candies of type <code>i - 1</code>.</li> |
| 21 | + <li>You must eat <strong>at least</strong> <strong>one</strong> candy per day until you have eaten all the candies.</li> |
| 22 | +</ul> |
| 23 | + |
| 24 | +<p>Construct a boolean array <code>answer</code> such that <code>answer.length == queries.length</code> and <code>answer[i]</code> is <code>true</code> if you can eat a candy of type <code>favoriteType<sub>i</sub></code> on day <code>favoriteDay<sub>i</sub></code> without eating <strong>more than</strong> <code>dailyCap<sub>i</sub></code> candies on <strong>any</strong> day, and <code>false</code> otherwise. Note that you can eat different types of candy on the same day, provided that you follow rule 2.</p> |
| 25 | + |
| 26 | +<p>Return <em>the constructed array </em><code>answer</code>.</p> |
| 27 | + |
| 28 | +<p> </p> |
| 29 | +<p><strong>Example 1:</strong></p> |
| 30 | + |
| 31 | +<pre> |
| 32 | +<strong>Input:</strong> candiesCount = [7,4,5,3,8], queries = [[0,2,2],[4,2,4],[2,13,1000000000]] |
| 33 | +<strong>Output:</strong> [true,false,true] |
| 34 | +<strong>Explanation:</strong> |
| 35 | +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. |
| 36 | +2- You can eat at most 4 candies each day. |
| 37 | + 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. |
| 38 | + 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. |
| 39 | +3- If you eat 1 candy each day, you will eat a candy of type 2 on day 13. |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p><strong>Example 2:</strong></p> |
| 43 | + |
| 44 | +<pre> |
| 45 | +<strong>Input:</strong> candiesCount = [5,2,6,4,1], queries = [[3,1,2],[4,10,3],[3,10,100],[4,100,30],[1,3,1]] |
| 46 | +<strong>Output:</strong> [false,true,true,false,false] |
| 47 | +</pre> |
| 48 | + |
| 49 | +<p> </p> |
| 50 | +<p><strong>Constraints:</strong></p> |
| 51 | + |
| 52 | +<ul> |
| 53 | + <li><code>1 <= candiesCount.length <= 10<sup>5</sup></code></li> |
| 54 | + <li><code>1 <= candiesCount[i] <= 10<sup>5</sup></code></li> |
| 55 | + <li><code>1 <= queries.length <= 10<sup>5</sup></code></li> |
| 56 | + <li><code>queries[i].length == 3</code></li> |
| 57 | + <li><code>0 <= favoriteType<sub>i</sub> < candiesCount.length</code></li> |
| 58 | + <li><code>0 <= favoriteDay<sub>i</sub> <= 10<sup>9</sup></code></li> |
| 59 | + <li><code>1 <= dailyCap<sub>i</sub> <= 10<sup>9</sup></code></li> |
| 60 | +</ul> |
| 61 | + |
| 62 | +### Related Topics |
| 63 | + [[Math](../../tag/math/README.md)] |
| 64 | + |
| 65 | +### Hints |
| 66 | +<details> |
| 67 | +<summary>Hint 1</summary> |
| 68 | +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. |
| 69 | +</details> |
| 70 | + |
| 71 | +<details> |
| 72 | +<summary>Hint 2</summary> |
| 73 | +To get the earliest day, you need to eat dailyCap candies every day. To get the latest day, you need to eat 1 candy every day. |
| 74 | +</details> |
| 75 | + |
| 76 | +<details> |
| 77 | +<summary>Hint 3</summary> |
| 78 | +The latest possible day is the total number of candies with a smaller type plus the number of your favorite candy minus 1. |
| 79 | +</details> |
| 80 | + |
| 81 | +<details> |
| 82 | +<summary>Hint 4</summary> |
| 83 | +The earliest possible day that you can eat your favorite candy is the total number of candies with a smaller type divided by dailyCap. |
| 84 | +</details> |
0 commit comments