Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2755ed4

Browse files
committed
Add solution #1744
1 parent aa29d7e commit 2755ed4

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,516 LeetCode solutions in JavaScript
1+
# 1,517 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1343,6 +1343,7 @@
13431343
1739|[Building Boxes](./solutions/1739-building-boxes.js)|Hard|
13441344
1742|[Maximum Number of Balls in a Box](./solutions/1742-maximum-number-of-balls-in-a-box.js)|Easy|
13451345
1743|[Restore the Array From Adjacent Pairs](./solutions/1743-restore-the-array-from-adjacent-pairs.js)|Medium|
1346+
1744|[Can You Eat Your Favorite Candy on Your Favorite Day?](./solutions/1744-can-you-eat-your-favorite-candy-on-your-favorite-day.js)|Medium|
13461347
1748|[Sum of Unique Elements](./solutions/1748-sum-of-unique-elements.js)|Easy|
13471348
1749|[Maximum Absolute Sum of Any Subarray](./solutions/1749-maximum-absolute-sum-of-any-subarray.js)|Medium|
13481349
1752|[Check if Array Is Sorted and Rotated](./solutions/1752-check-if-array-is-sorted-and-rotated.js)|Easy|
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* 1744. Can You Eat Your Favorite Candy on Your Favorite Day?
3+
* https://leetcode.com/problems/can-you-eat-your-favorite-candy-on-your-favorite-day/
4+
* Difficulty: Medium
5+
*
6+
* You are given a (0-indexed) array of positive integers candiesCount where candiesCount[i]
7+
* represents the number of candies of the ith type you have. You are also given a 2D array
8+
* queries where queries[i] = [favoriteTypei, favoriteDayi, dailyCapi].
9+
*
10+
* You play a game with the following rules:
11+
* - You start eating candies on day 0.
12+
* - You cannot eat any candy of type i unless you have eaten all candies of type i - 1.
13+
* - You must eat at least one candy per day until you have eaten all the candies.
14+
*
15+
* Construct a boolean array answer such that answer.length == queries.length and answer[i] is
16+
* true if you can eat a candy of type favoriteTypei on day favoriteDayi without eating more
17+
* than dailyCapi candies on any day, and false otherwise. Note that you can eat different
18+
* types of candy on the same day, provided that you follow rule 2.
19+
*
20+
* Return the constructed array answer.
21+
*/
22+
23+
/**
24+
* @param {number[]} candiesCount
25+
* @param {number[][]} queries
26+
* @return {boolean[]}
27+
*/
28+
var canEat = function(candiesCount, queries) {
29+
const prefixSums = [0];
30+
for (const count of candiesCount) {
31+
prefixSums.push(prefixSums.at(-1) + count);
32+
}
33+
34+
const result = new Array(queries.length);
35+
for (let i = 0; i < queries.length; i++) {
36+
const [type, day, cap] = queries[i];
37+
const minCandies = day;
38+
const maxCandies = (day + 1) * cap;
39+
const typeStart = prefixSums[type];
40+
const typeEnd = prefixSums[type + 1] - 1;
41+
result[i] = maxCandies > typeStart && minCandies <= typeEnd;
42+
}
43+
44+
return result;
45+
};

0 commit comments

Comments
 (0)