Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1744.html

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

Level

Medium

Description

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.

Construct a boolean array answer such that answer.length == queries.length and answer[i] is true if you can eat a candy of type favoriteType_i on day favoriteDay_i without eating more than dailyCap_i 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.

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 <= 10^5
  • 1 <= candiesCount[i] <= 10^5
  • 1 <= queries.length <= 10^5
  • queries[i].length == 3
  • 0 <= favoriteType_i < candiesCount.length
  • 0 <= favoriteDay_i <= 10^9
  • 1 <= dailyCap_i <= 10^9

Solution

Calculate array prefixSums, where prefixSums[i + 1] = prefixSums[i] + candiesCount[i + 1] for i >= 0.

For each query that contains tuple (type, day, capacity), the allowed range of number of candies is [prefixSums[type] + 1, prefixSums[type + 1]], and the possible range of number of candies is [day + 1, (day + 1) * capacity]. The query is true if and only if the intersection of the two ranges is non-empty.

Finally, return the array of query results.

  • class Solution {
        public boolean[] canEat(int[] candiesCount, int[][] queries) {
            int length = candiesCount.length;
            long[] prefixSums = new long[length + 1];
            for (int i = 0; i < length; i++)
                prefixSums[i + 1] = prefixSums[i] + candiesCount[i];
            int queriesCount = queries.length;
            boolean[] answer = new boolean[queriesCount];
            for (int i = 0; i < queriesCount; i++) {
                int type = queries[i][0], day = queries[i][1], capacity = queries[i][2];
                long minCandies = prefixSums[type] + 1, maxCandies = prefixSums[type + 1];
                long minPossible = day + 1;
                long maxPossible = (long) (day + 1) * capacity;
                answer[i] = minCandies <= maxPossible && maxCandies >= minPossible;
            }
            return answer;
        }
    }
    
    ############
    
    class Solution {
        public boolean[] canEat(int[] candiesCount, int[][] queries) {
            int n = candiesCount.length;
            long[] s = new long[n + 1];
            for (int i = 0; i < n; ++i) {
                s[i + 1] = s[i] + candiesCount[i];
            }
            int m = queries.length;
            boolean[] ans = new boolean[m];
            for (int i = 0; i < m; ++i) {
                int t = queries[i][0], day = queries[i][1], mx = queries[i][2];
                long least = day, most = (long) (day + 1) * mx;
                ans[i] = least < s[t + 1] && most > s[t];
            }
            return ans;
        }
    }
    
  • // OJ: https://leetcode.com/problems/can-you-eat-your-favorite-candy-on-your-favorite-day/
    // Time: O(N + Q)
    // Space: O(N)
    class Solution {
    public:
        vector<bool> canEat(vector<int>& cnt, vector<vector<int>>& Q) {
            vector<bool> ans(Q.size());
            vector<long long> pre(cnt.size() + 1);
            for (int i = 0; i < cnt.size(); ++i) pre[i + 1] = pre[i] + cnt[i];
            for (int i = 0; i < Q.size(); ++i) {
                long long type = Q[i][0], day = Q[i][1], cap = Q[i][2];
                long long prev = pre[type], cur = pre[type + 1];
                long long mx = cur - 1, mn = prev / cap;
                ans[i] = day >= mn && day <= mx;
            }
            return ans;
        }
    };
    
  • class Solution:
        def canEat(self, candiesCount: List[int], queries: List[List[int]]) -> List[bool]:
            s = list(accumulate(candiesCount, initial=0))
            ans = []
            for t, day, mx in queries:
                least, most = day, (day + 1) * mx
                ans.append(least < s[t + 1] and most > s[t])
            return ans
    
    ############
    
    # 1744. Can You Eat Your Favorite Candy on Your Favorite Day?
    # https://leetcode.com/problems/can-you-eat-your-favorite-candy-on-your-favorite-day
    
    class Solution:
        def canEat(self, candiesCount: List[int], queries: List[List[int]]) -> List[bool]:
            
            res = []
            prefix = [0] + list(accumulate(candiesCount))
                
            for favType, day, cap in queries:
                if (day+1) * cap > prefix[favType] and prefix[favType+1] >= (day+1) * 1:
                    res.append(True)
                else:
                    res.append(False)
                
            return res
    
    
  • func canEat(candiesCount []int, queries [][]int) (ans []bool) {
    	n := len(candiesCount)
    	s := make([]int, n+1)
    	for i, v := range candiesCount {
    		s[i+1] = s[i] + v
    	}
    	for _, q := range queries {
    		t, day, mx := q[0], q[1], q[2]
    		least, most := day, (day+1)*mx
    		ans = append(ans, least < s[t+1] && most > s[t])
    	}
    	return
    }
    

All Problems

All Solutions