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:

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.

 

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:

Solution:

calculate: 
1. how many days it takes to eat candies before the desired type, and check if this is smaller than the required day
2. number of candies including the desired type is > the required day

class Solution {
    public boolean[] canEat(int[] candiesCount, int[][] queries) {
        int n = candiesCount.length;
        long[] prefix = new long[n + 1];
        for (int i = 1; i <= n; i ++) {
            prefix[i] = prefix[i - 1] + candiesCount[i - 1];
        }
        boolean[] res = new boolean[queries.length];
        for (int i = 0; i < queries.length; i ++) {
            int[] query = queries[i];
            int type = query[0];
            int day = query[1];
            int limit = query[2];
            double daysNeeded = days(prefix, type, limit);
            // System.out.println(prefix[type]);
            // System.out.println(daysNeeded);
            if (daysNeeded < day + 1 && prefix[type + 1] > day) {
                res[i] = true;
            }
        }
        return res;
    }
    
    private double days(long[] prefix, int type, int limit) {
        long sum = prefix[type];
        return sum / (double) limit;
    }
}