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 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 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.

For each query, determine if you can eat a candy of type favoriteType_i on day favoriteDay_i without eating more than dailyCap_i candies on any day.

Return a boolean array answer where answer[i] is true if you can eat your favorite candy on your favorite day for query i, and false otherwise.

Input & Output

Example 1 — Multiple Query Types
$ Input: candiesCount = [7,4,5,3,8], queries = [[0,2,2],[4,2,4],[2,13,1000000000]]
Output: [true,false,true]
💡 Note: Query 1: Type 0 candy available days 0-6, day 2 ∈ [0,6] ✓. Query 2: Type 4 needs 11 days minimum, but day 2 < 11 ✗. Query 3: Type 2 available days 1-15, day 13 ∈ [1,15] ✓
Example 2 — Single Type Query
$ Input: candiesCount = [5,2,6,4,1], queries = [[3,1,6],[4,10,3],[3,0,1]]
Output: [false,true,false]
💡 Note: Query 1: Need 7 days minimum for type 3, day 1 too early. Query 2: Type 4 available days 3-17, day 10 ∈ [3,17] ✓. Query 3: Type 3 earliest day 7, day 0 too early
Example 3 — Edge Case Small Array
$ Input: candiesCount = [1], queries = [[0,0,1],[0,1,1]]
Output: [true,false]
💡 Note: Only 1 candy of type 0. Available only on day 0. First query matches exactly, second query too late

Constraints

  • 1 ≤ candiesCount.length ≤ 105
  • 1 ≤ candiesCount[i] ≤ 105
  • 1 ≤ queries.length ≤ 105
  • queries[i].length == 3
  • 0 ≤ favoriteType_i < candiesCount.length
  • 0 ≤ favoriteDay_i ≤ 109
  • 1 ≤ dailyCap_i ≤ 109

Visualization

Tap to expand
Can You Eat Your Favorite Candy on Your Favorite Day?Candies: [7,4,5,3,8]74538Type 0Type 1Type 2Type 3Type 4Query: [0,2,2]Type 0, Day 2Rules: Sequential order, 1+ candy/day, max daily limitType 0 available: Days 0-6Day 2 ∈ [0,6] ✓TRUEType 4 available: Days 3-26Day 2 ∉ [3,26] ✗FALSEOutput: [true, false, true]Each query checked against valid day range for candy type
Understanding the Visualization
1
Input
Candy counts array and queries with type, day, and daily limit
2
Process
Calculate valid day ranges for each candy type using constraints
3
Output
Boolean array indicating if each query is possible
Key Takeaway
🎯 Key Insight: Use prefix sums to find the earliest and latest day each candy type is available
Asked in
Google 15 Amazon 8 Facebook 6
38.5K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen