LeetCode-1744. Can You Eat Your Favorite Candy on Your...-Analysis and Code (Java)

LeetCode-1744. Can you eat your favorite candy on your favorite day [Can You Eat Your Favorite Candy on Your Favorite Day]-analysis and code [Java]

1. Topic

Give you a positive integer array candiesCount whose subscript starts from 0, where candiesCount[i] represents the number of candies of the i type you have. At the same time, give you a two-dimensional array queries, where queries[i] = [favoriteTypei, favoriteDayi, dailyCapi].
You play a game according to the following rules:

  • You start eating candy from day 0.
  • You cannot eat any i-th candy until you have eaten all the i--1 candy.
  • You must eat at least one candy every day before eating all the candies.

Please construct a boolean array answer that satisfies answer.length == queries.length. The condition for answer[i] to be true is: on the premise that you can eat no more than dailyCapi candy per day, you can eat the favoriteTypei candy on the favoriteDayi day; otherwise answer[i] is false. Note that as long as you meet the second rule of the 3 rules above, you can eat different types of candies on the same day.
Please return the resulting array answer.

Example 1:

输入:candiesCount = [7,4,5,3,8], queries = [[0,2,2],[4,2,4],[2,13,1000000000]]
输出:[true,false,true]
提示:
1- 在第 0 天吃 2 颗糖果(类型 0),第 1 天吃 2 颗糖果(类型 0),第 2 天你可以吃到类型 0 的糖果。
2- 每天你最多吃 4 颗糖果。即使第 0 天吃 4 颗糖果(类型 0),第 1 天吃 4 颗糖果(类型 0 和类型 1),你也没办法在第 2 天吃到类型 4 的糖果。换言之,你没法在每天吃 4 颗糖果的限制下在第 2 天吃到第 4 类糖果。
3- 如果你每天吃 1 颗糖果,你可以在第 13 天吃到类型 2 的糖果。

Example 2:

输入:candiesCount = [5,2,6,4,1], queries = [[3,1,2],[4,10,3],[3,10,100],[4,100,30],[1,3,1]]
输出:[false,true,true,false,false]

prompt:

  • 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

Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/can-you-eat-your-favorite-candy-on-your-favorite-day The
copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Two, analysis and code

1. Calculate the boundary

(1) Thinking

According to the meaning of the question, the number of candies that can be eaten on the i day is [days + 1, (days + 1) * dailyCap], and since the candies must be consumed in order, the index of the jth candies in the eaten sequence is ( candiesSum[j-1], candiesSum[j]], so you only need to judge whether there is an intersection between the two intervals to get the current answer.
This question is not difficult, but you need to carefully understand the meaning of the question, deal with the boundary conditions, and pay attention to the calculation process The data in may exceed the range of int.

(2) Code

class Solution {
    
    
    public boolean[] canEat(int[] candiesCount, int[][] queries) {
    
    
        int n = candiesCount.length, len = queries.length;
        long [] candiesSum = new long[n];
        candiesSum[0] = candiesCount[0];
        for (int i = 1; i < n; i++)
            candiesSum[i] = (long)candiesSum[i - 1] + candiesCount[i];      
        
        boolean[] ans = new boolean[len];
        for (int i = 0; i < len; i++) {
    
    
            int type = queries[i][0], days = queries[i][1], dailyCap = queries[i][2];
            long minCandy = (type > 0) ? candiesSum[type - 1] : 0;
            long maxCandy = candiesSum[type];
            if (days + 1 <= maxCandy && (long)(days + 1) * dailyCap > minCandy)
                ans[i] = true;
            else
                ans[i] = false;
        }
        return ans;      
    }
}

(3) Results

Execution time: 8 ms, beating 94.96% of users
in all Java submissions ; memory consumption: 76.1 MB, beating 40.72% of users in all Java submissions.

Three, other

Nothing.

Guess you like

Origin blog.csdn.net/zml66666/article/details/113817668