Can You Eat Your Favorite Candy on Your Favorite Day?

Approach

  1. Calculate Prefix Sum: Compute a prefix sum of candiesCount to find the total candies needed to reach each type.

  2. Iterate Through Queries: For each query, calculate the minimum and maximum days needed to reach the favoriteType considering the dailyCap.

  3. Check Conditions: Based on the minimum and maximum days needed, determine whether the favoriteDay falls within that range.

  4. Result: Add True or False to the result array depending on whether the conditions are met.

Python solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from typing import List

class Solution:
    def canEat(self, candiesCount: List[int], queries: List[List[int]]) -> List[bool]:
        prefix_sum = [0]

        # Calculate the prefix sum of candiesCount
        for count in candiesCount:
            prefix_sum.append(prefix_sum[-1] + count)

        # Iterate through the queries to find the result
        result = []
        for favoriteType, favoriteDay, dailyCap in queries:
            min_day = prefix_sum[favoriteType] // dailyCap
            max_day = prefix_sum[favoriteType + 1] - 1

            # Check if the favoriteDay falls within the range of min_day and max_day
            if favoriteDay >= min_day and favoriteDay <= max_day:
                result.append(True)
            else:
                result.append(False)

        return result

The code takes into account the given rules and constraints, and returns a boolean array as required.