Skip to content

Commit c02bc66

Browse files
authored
Update 1744-can-you-eat-your-favorite-candy-on-your-favorite-day.js
1 parent d32dda5 commit c02bc66

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed
Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
function canEat(candiesCount: number[], queries: number[][]): boolean[] {
2-
const acc = Array(candiesCount.length);
3-
acc[0] = candiesCount[0];
4-
for (let i = 1; i < candiesCount.length; i++) {
5-
acc[i] = acc[i - 1] + candiesCount[i]
6-
}
1+
/**
2+
* @param {number[]} candiesCount
3+
* @param {number[][]} queries
4+
* @return {boolean[]}
5+
*/
6+
const canEat = function(candiesCount, queries) {
7+
const n = candiesCount.length
8+
const pre = Array(n).fill(0)
9+
for(let i = 1; i < n; i++) {
10+
pre[i] = pre[i - 1] + candiesCount[i - 1]
11+
}
12+
return queries.map((e, i) => {
13+
const [t, d, c] = e
14+
const num = candiesCount[t]
15+
const min = d, max = (d + 1) * c
716

8-
return queries.map(([typ, day, cap]) => {
9-
let candyMin = typ === 0 ? 1 : acc[typ - 1] + 1;
10-
let candyMax = acc[typ];
11-
let eatMin = day + 1;
12-
let eatMax = (day + 1) * cap;
13-
return (candyMax < eatMin || eatMax < candyMin) ? false : true;
14-
})
17+
if(pre[t] + num > min && pre[t] < max) {
18+
return true
19+
} else {
20+
return false
21+
}
22+
})
1523
};

0 commit comments

Comments
 (0)