[力扣] 算法 1744 (C++)

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

class Solution {
public:
	vector<bool> canEat(vector<int>& candiesCount, vector<vector<int>>& queries) {
		vector<long long> tmp(candiesCount.size());
		tmp[0] = candiesCount[0];
		for (size_t i = 1, size = tmp.size(); i < size; ++i)
		{
			tmp[i] += tmp[i - 1] + candiesCount[i];
		}
		vector<bool> ret(queries.size());
		for (size_t i = 0, size = queries.size(); i < size; ++i)
		{
			auto&& query = queries[i];
			int favoriteType = query[0];
			int favoriteDay = query[1];
			long dailyCap = query[2];

			// 往死里吃,必须保证最喜欢的糖果之前的糖果已经全部吃完
			int atLeast = favoriteType > 0 ? tmp[favoriteType - 1] : 0;
			// 一颗一颗吃,必须保证当天至少剩一颗最喜欢的糖
			int limit = tmp[favoriteType];
			// 总共花费的天数
			long day = favoriteDay + 1;

			ret[i] = dailyCap * day > atLeast && 1 * day <= limit;
		}
		return ret;
	}
};

发表评论