{"id":8052,"date":"2021-01-30T21:33:27","date_gmt":"2021-01-31T05:33:27","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8052"},"modified":"2021-01-30T21:35:18","modified_gmt":"2021-01-31T05:35:18","slug":"leetcode-1744-can-you-eat-your-favorite-candy-on-your-favorite-day","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1744-can-you-eat-your-favorite-candy-on-your-favorite-day\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1744. Can You Eat Your Favorite Candy on Your Favorite Day?"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>(0-indexed)<\/strong>&nbsp;array of positive integers&nbsp;<code>candiesCount<\/code>&nbsp;where&nbsp;<code>candiesCount[i]<\/code>&nbsp;represents the number of candies of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;type you have. You are also given a 2D array&nbsp;<code>queries<\/code>&nbsp;where&nbsp;<code>queries[i] = [favoriteType<sub>i<\/sub>, favoriteDay<sub>i<\/sub>, dailyCap<sub>i<\/sub>]<\/code>.<\/p>\n\n\n\n<p>You play a game with the following rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You start eating candies on day&nbsp;<code><strong>0<\/strong><\/code>.<\/li><li>You&nbsp;<strong>cannot<\/strong>&nbsp;eat&nbsp;<strong>any<\/strong>&nbsp;candy of type&nbsp;<code>i<\/code>&nbsp;unless you have eaten&nbsp;<strong>all<\/strong>&nbsp;candies of type&nbsp;<code>i - 1<\/code>.<\/li><li>You must eat&nbsp;<strong>at least<\/strong>&nbsp;<strong>one<\/strong>&nbsp;candy per day until you have eaten all the candies.<\/li><\/ul>\n\n\n\n<p>Construct a boolean array&nbsp;<code>answer<\/code>&nbsp;such that&nbsp;<code>answer.length == queries.length<\/code>&nbsp;and&nbsp;<code>answer[i]<\/code>&nbsp;is&nbsp;<code>true<\/code>&nbsp;if you can eat a candy of type&nbsp;<code>favoriteType<sub>i<\/sub><\/code>&nbsp;on day&nbsp;<code>favoriteDay<sub>i<\/sub><\/code>&nbsp;without eating&nbsp;<strong>more than<\/strong>&nbsp;<code>dailyCap<sub>i<\/sub><\/code>&nbsp;candies on&nbsp;<strong>any<\/strong>&nbsp;day, and&nbsp;<code>false<\/code>&nbsp;otherwise. Note that you can eat different types of candy on the same day, provided that you follow rule 2.<\/p>\n\n\n\n<p>Return&nbsp;<em>the constructed array&nbsp;<\/em><code>answer<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> candiesCount = [7,4,5,3,8], queries = [[0,2,2],[4,2,4],[2,13,1000000000]]\n<strong>Output:<\/strong> [true,false,true]\n<strong>Explanation:<\/strong>\n1- 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.\n2- You can eat at most 4 candies each day.\n   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.\n   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.\n3- If you eat 1 candy each day, you will eat a candy of type 2 on day 13.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> candiesCount = [5,2,6,4,1], queries = [[3,1,2],[4,10,3],[3,10,100],[4,100,30],[1,3,1]]\n<strong>Output:<\/strong> [false,true,true,false,false]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= candiesCount.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= candiesCount[i] &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= queries.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>queries[i].length == 3<\/code><\/li><li><code>0 &lt;= favoriteType<sub>i<\/sub>&nbsp;&lt; candiesCount.length<\/code><\/li><li><code>0 &lt;= favoriteDay<sub>i<\/sub>&nbsp;&lt;= 10<sup>9<\/sup><\/code><\/li><li><code>1 &lt;= dailyCap<sub>i<\/sub>&nbsp;&lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Prefix Sum<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>We must have enough capacity to eat all candies before the current type.<\/li><li>We must have at least prefix sum candies than days, since we have to eat at least one each day.<\/li><\/ol>\n\n\n\n<p>sum[i] = sum(candyCount[0~i])<br>ans = {days * cap &gt; sum[type &#8211; 1] &amp;&amp; days &lt;= sum[type])<\/p>\n\n\n\n<p>Time complexity:O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huhua\nclass Solution {\npublic:\n  vector<bool> canEat(vector<int>& candiesCount, vector<vector<int>>& queries) {\n    const int n = candiesCount.size();\n    vector<long> sums(n + 1);    \n    for (int i = 1; i <= n; ++i)\n      sums[i] += sums[i - 1] + candiesCount[i - 1];\n    vector<bool> ans;\n    for (const auto& q : queries) {\n      const long type = q[0], days = q[1] + 1, cap = q[2];\n      ans.push_back(days * cap > sums[type] && days <= sums[type + 1]);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;(0-indexed)&nbsp;array of positive integers&nbsp;candiesCount&nbsp;where&nbsp;candiesCount[i]&nbsp;represents the number of candies of the&nbsp;ith&nbsp;type you have. You are also given a 2D array&nbsp;queries&nbsp;where&nbsp;queries[i] = [favoriteTypei, favoriteDayi,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,177,97],"class_list":["post-8052","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-medium","tag-prefix","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8052"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8052\/revisions"}],"predecessor-version":[{"id":8054,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8052\/revisions\/8054"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}