1744. Can You Eat Your Favorite Candy on Your Favorite Day? LeetCode Solution

In this guide, you will get 1744. Can You Eat Your Favorite Candy on Your Favorite Day? LeetCode Solution with the best time and space complexity. The solution to Can You Eat Your Favorite Candy on Your Favorite Day? problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Can You Eat Your Favorite Candy on Your Favorite Day? solution in C++
  4. Can You Eat Your Favorite Candy on Your Favorite Day? solution in Java
  5. Can You Eat Your Favorite Candy on Your Favorite Day? solution in Python
  6. Additional Resources
1744. Can You Eat Your Favorite Candy on Your Favorite Day? LeetCode Solution image

Problem Statement of Can You Eat Your Favorite Candy on Your Favorite Day?

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.

See also  3045. Count Prefix and Suffix Pairs II LeetCode Solution

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1744. Can You Eat Your Favorite Candy on Your Favorite Day? LeetCode Solution in C++

class Solution {
 public:
  vector<bool> canEat(vector<int>& candiesCount, vector<vector<int>>& queries) {
    const int n = candiesCount.size();
    vector<bool> ans;
    vector<long> prefix{0};

    for (int i = 0; i < n; ++i)
      prefix.push_back(prefix.back() + candiesCount[i]);

    for (const vector<int>& query : queries) {
      const int type = query[0];
      const int day = query[1];
      const int cap = query[2];
      // the minimum day required to eat
      const long minDay = prefix[type] / cap;
      // the maximum day required to eat
      const long maxDay = prefix[type + 1] - 1;
      ans.push_back(minDay <= day && day <= maxDay);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1744. Can You Eat Your Favorite Candy on Your Favorite Day? LeetCode Solution in Java

class Solution {
  public boolean[] canEat(int[] candiesCount, int[][] queries) {
    final int n = candiesCount.length;
    boolean[] ans = new boolean[queries.length];
    long[] prefix = new long[n + 1];

    for (int i = 0; i < n; ++i)
      prefix[i + 1] = prefix[i] + candiesCount[i];

    for (int i = 0; i < queries.length; ++i) {
      final int type = queries[i][0];
      final long day = (long) queries[i][1];
      final long cap = (long) queries[i][2];
      // the minimum day required to eat
      final long minDay = prefix[type] / cap;
      // the maximum day required to eat
      final long maxDay = prefix[type + 1] - 1;
      ans[i] = minDay <= day && day <= maxDay;
    }

    return ans;
  }
}
// code provided by PROGIEZ

1744. Can You Eat Your Favorite Candy on Your Favorite Day? LeetCode Solution in Python

class Solution:
  def canEat(
      self,
      candiesCount: list[int],
      queries: list[list[int]]
  ) -> list[bool]:
    prefix = list(itertools.accumulate(candiesCount, initial=0))
    return [prefix[t] // c <= d < prefix[t + 1] for t, d, c in queries]
# code by PROGIEZ

Additional Resources

See also  2306. Naming a Company LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.