列表

详情


1744. 你能在你最喜欢的那天吃到你最喜欢的糖果吗?

给你一个下标从 0 开始的正整数数组 candiesCount ,其中 candiesCount[i] 表示你拥有的第 i 类糖果的数目。同时给你一个二维数组 queries ,其中 queries[i] = [favoriteTypei, favoriteDayi, dailyCapi] 。

你按照如下规则进行一场游戏:

请你构建一个布尔型数组 answer ,用以给出 queries 中每一项的对应答案。此数组满足:

注意,只要满足上面 3 条规则中的第二条规则,你就可以在同一天吃不同类型的糖果。

请你返回得到的数组 answer 。

 

示例 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 的糖果。

示例 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]

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: vector<bool> canEat(vector<int>& candiesCount, vector<vector<int>>& queries) { } };

javascript 解法, 执行用时: 176 ms, 内存消耗: 79.4 MB, 提交时间: 2023-09-06 23:50:03

/**
 * @param {number[]} candiesCount
 * @param {number[][]} queries
 * @return {boolean[]}
 */
var canEat = function(candiesCount, queries) {
    const n = candiesCount.length;
    
    // 前缀和
    const sum = new Array(n).fill(0);
    sum[0] = candiesCount[0];
    for (let i = 1; i < n; ++i) {
        sum[i] = sum[i - 1] + candiesCount[i];
    }
    
    const q = queries.length;
    const ans = new Array(q).fill(0);
    for (let i = 0; i < q; ++i) {
        const query = queries[i];
        const favoriteType = query[0], favoriteDay = query[1], dailyCap = query[2];
        
        const x1 = favoriteDay + 1;
        const y1 = (favoriteDay + 1) * dailyCap;
        const x2 = favoriteType == 0 ? 1 : sum[favoriteType - 1] + 1;
        const y2 = sum[favoriteType];
        
        ans[i] = !(x1 > y2 || y1 < x2);
    }
    return ans;
};

golang 解法, 执行用时: 188 ms, 内存消耗: 23.6 MB, 提交时间: 2023-09-06 23:49:40

func canEat(candiesCount []int, queries [][]int) []bool {
    n := len(candiesCount)

    // 前缀和
    sum := make([]int, n)
    sum[0] = candiesCount[0]
    for i := 1; i < n; i++ {
        sum[i] = sum[i-1] + candiesCount[i]
    }

    ans := make([]bool, len(queries))
    for i, q := range queries {
        favoriteType, favoriteDay, dailyCap := q[0], q[1], q[2]

        x1 := favoriteDay + 1
        y1 := (favoriteDay + 1) * dailyCap
        x2 := 1
        if favoriteType > 0 {
            x2 = sum[favoriteType-1] + 1
        }
        y2 := sum[favoriteType]

        ans[i] = !(x1 > y2 || y1 < x2)
    }
    return ans
}

python3 解法, 执行用时: 156 ms, 内存消耗: 64.7 MB, 提交时间: 2023-09-06 23:49:26

class Solution:
    def canEat(self, candiesCount: List[int], queries: List[List[int]]) -> List[bool]:
        # 前缀和
        total = list(accumulate(candiesCount))
        
        ans = list()
        for favoriteType, favoriteDay, dailyCap in queries:
            x1 = favoriteDay + 1
            y1 = (favoriteDay + 1) * dailyCap
            x2 = 1 if favoriteType == 0 else total[favoriteType - 1] + 1
            y2 = total[favoriteType]
            
            ans.append(not(x1 > y2 or y1 < x2))
        
        return ans

java 解法, 执行用时: 6 ms, 内存消耗: 88.4 MB, 提交时间: 2023-09-06 23:49:13

class Solution {
    public boolean[] canEat(int[] candiesCount, int[][] queries) {
        int n = candiesCount.length;
        
        // 前缀和
        long[] sum = new long[n];
        sum[0] = candiesCount[0];
        for (int i = 1; i < n; ++i) {
            sum[i] = sum[i - 1] + candiesCount[i];
        }
        
        int q = queries.length;
        boolean[] ans = new boolean[q];
        for (int i = 0; i < q; ++i) {
            int[] query = queries[i];
            int favoriteType = query[0], favoriteDay = query[1], dailyCap = query[2];
            
            long x1 = favoriteDay + 1;
            long y1 = (long) (favoriteDay + 1) * dailyCap;
            long x2 = favoriteType == 0 ? 1 : sum[favoriteType - 1] + 1;
            long y2 = sum[favoriteType];
            
            ans[i] = !(x1 > y2 || y1 < x2);
        }
        return ans;
    }
}

cpp 解法, 执行用时: 404 ms, 内存消耗: 118.1 MB, 提交时间: 2023-09-06 23:49:01

class Solution {
private:
    using LL = long long;

public:
    vector<bool> canEat(vector<int>& candiesCount, vector<vector<int>>& queries) {
        int n = candiesCount.size();
        
        // 前缀和
        vector<LL> sum(n);
        sum[0] = candiesCount[0];
        for (int i = 1; i < n; ++i) {
            sum[i] = sum[i - 1] + candiesCount[i];
        }
        
        vector<bool> ans;
        for (const auto& q: queries) {
            int favoriteType = q[0], favoriteDay = q[1], dailyCap = q[2];
            
            LL x1 = favoriteDay + 1;
            LL y1 = (LL)(favoriteDay + 1) * dailyCap;
            LL x2 = (favoriteType == 0 ? 1 : sum[favoriteType - 1] + 1);
            LL y2 = sum[favoriteType];
            
            ans.push_back(!(x1 > y2 || y1 < x2));
        }
        return ans;
    }
};

上一题