Leetcode 1744: Can You Eat Your Favorite Candy on Your Favorite Day?

grid47
grid47
Exploring patterns and algorithms
May 16, 2024 8 min read

You are given an array candiesCount where each element represents the number of candies of a particular type. You are also given a set of queries, each asking whether it’s possible to eat a candy of a certain type on a specific day without exceeding a daily candy limit. You must follow these game rules:

  • You start eating candies on day 0.
  • You cannot eat candies of type i until you have eaten all candies of type i-1.
  • You must eat at least one candy per day.

Your task is to return an array of booleans where each element indicates whether it’s possible to eat a candy of the specified type on the given day, subject to the daily cap.

Problem
Approach
Steps
Complexity
Input: The input consists of an array `candiesCount`, where `candiesCount[i]` indicates the number of candies of the ith type, and a 2D array `queries`, where each query specifies a favorite candy type, a day, and the daily cap of candies that can be eaten.
Example: Input: candiesCount = [6, 3, 2, 4], queries = [[1, 2, 3], [2, 7, 5], [0, 5, 10]]
Constraints:
• 1 <= candiesCount.length <= 10^5
• 1 <= candiesCount[i] <= 10^5
• 1 <= queries.length <= 10^5
• queries[i].length == 3
• 0 <= favoriteTypei < candiesCount.length
• 0 <= favoriteDayi <= 10^9
• 1 <= dailyCapi <= 10^9
Output: Return an array of booleans indicating whether it's possible to eat a candy of the specified type on the given day without exceeding the daily cap.
Example: Output: [true, false, true]
Constraints:
• For each query, the result should be `true` if it's possible to eat a candy of the favorite type on the specified day without exceeding the daily cap, otherwise `false`.
Goal: The goal is to determine if, given the daily candy limit, it's possible to eat a specific type of candy on a specific day while respecting the game rules.
Steps:
• 1. Compute the cumulative sum of the number of candies for each type.
• 2. For each query, calculate the minimum and maximum possible days the candy of the favorite type can be eaten, based on the daily limit.
• 3. Check if the specified day falls within the valid range of days for the favorite type and return the corresponding result.
Goal: Ensure that the solution handles the upper limits efficiently, as the input sizes can be quite large.
Steps:
• The solution must be optimized for handling large arrays and queries.
Assumptions:
• Each query is valid, meaning the favorite type and day are within the bounds of the candies array and the daily cap.
Input: Input: candiesCount = [6, 3, 2, 4], queries = [[1, 2, 3], [2, 7, 5], [0, 5, 10]]
Explanation: For the first query, you want to check if it's possible to eat a candy of type 1 on day 2 with a daily cap of 3 candies. If you eat 3 candies per day, by day 2, you've eaten 3 candies from type 0 and 3 from type 1, so the answer is `true`. For the second query, you cannot eat type 2 on day 7 without exceeding the daily cap of 5, so the answer is `false`. For the third query, if you eat 10 candies per day, you will eat a candy of type 0 on day 5, so the answer is `true`.

Input: Input: candiesCount = [5, 2, 6, 4, 1], queries = [[3, 1, 2], [4, 10, 3], [3, 10, 100], [4, 100, 30], [1, 3, 1]]
Explanation: The answers for the queries are `false`, `true`, `true`, `false`, and `false` because they depend on the daily cap and how candies accumulate over the days.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus