返回介绍

solution / 1700-1799 / 1744.Can You Eat Your Favorite Candy on Your Favorite Day / README_EN

发布于 2024-06-17 01:03:15 字数 5388 浏览 0 评论 0 收藏 0

1744. Can You Eat Your Favorite Candy on Your Favorite Day

中文文档

Description

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:

  • You start eating candies on day 0.
  • You cannot eat any candy of type i unless you have eaten all candies of type i - 1.
  • You must eat at least one candy per day until you have eaten all the candies.

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:

  • 1 <= candiesCount.length <= 105
  • 1 <= candiesCount[i] <= 105
  • 1 <= queries.length <= 105
  • queries[i].length == 3
  • 0 <= favoriteTypei < candiesCount.length
  • 0 <= favoriteDayi <= 109
  • 1 <= dailyCapi <= 109

Solutions

Solution 1

class Solution:
  def canEat(self, candiesCount: List[int], queries: List[List[int]]) -> List[bool]:
    s = list(accumulate(candiesCount, initial=0))
    ans = []
    for t, day, mx in queries:
      least, most = day, (day + 1) * mx
      ans.append(least < s[t + 1] and most > s[t])
    return ans
class Solution {
  public boolean[] canEat(int[] candiesCount, int[][] queries) {
    int n = candiesCount.length;
    long[] s = new long[n + 1];
    for (int i = 0; i < n; ++i) {
      s[i + 1] = s[i] + candiesCount[i];
    }
    int m = queries.length;
    boolean[] ans = new boolean[m];
    for (int i = 0; i < m; ++i) {
      int t = queries[i][0], day = queries[i][1], mx = queries[i][2];
      long least = day, most = (long) (day + 1) * mx;
      ans[i] = least < s[t + 1] && most > s[t];
    }
    return ans;
  }
}
using ll = long long;

class Solution {
public:
  vector<bool> canEat(vector<int>& candiesCount, vector<vector<int>>& queries) {
    int n = candiesCount.size();
    vector<ll> s(n + 1);
    for (int i = 0; i < n; ++i) s[i + 1] = s[i] + candiesCount[i];
    vector<bool> ans;
    for (auto& q : queries) {
      int t = q[0], day = q[1], mx = q[2];
      ll least = day, most = 1ll * (day + 1) * mx;
      ans.emplace_back(least < s[t + 1] && most > s[t]);
    }
    return ans;
  }
};
func canEat(candiesCount []int, queries [][]int) (ans []bool) {
  n := len(candiesCount)
  s := make([]int, n+1)
  for i, v := range candiesCount {
    s[i+1] = s[i] + v
  }
  for _, q := range queries {
    t, day, mx := q[0], q[1], q[2]
    least, most := day, (day+1)*mx
    ans = append(ans, least < s[t+1] && most > s[t])
  }
  return
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文