LeetCode1744(2021.6.1)

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

typedef long long LL;

class Solution {
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;
    }
};

发表评论