Skip to content

Commit ed16c56

Browse files
committed
👽
1 parent a6fcef4 commit ed16c56

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Day93/index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} x
4+
* @return {number}
5+
*/
6+
var minOperations = function (n, x) {
7+
8+
let map = {};
9+
10+
let sum = 0;
11+
12+
for (let i = 0; i < n.length; i++) {
13+
sum += n[i];
14+
15+
map[sum] = i + 1
16+
}
17+
18+
sum = 0;
19+
20+
let res = Infinity;
21+
if (map[x] != undefined) res = map[x]
22+
23+
24+
for (let i = n.length - 1; i >= 0; i--) {
25+
26+
sum += n[i];
27+
// sum + y === x
28+
let y = x - sum;
29+
30+
if (y == 0) {
31+
res = Math.min(res, n.length - i)
32+
} else {
33+
34+
if (map[y] !== undefined && map[y] < i) {
35+
res = Math.min(res, map[y] + n.length - i)
36+
}
37+
}
38+
39+
}
40+
return res == Infinity ? -1 : res;
41+
};

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,7 @@ If you are loving solving problems in leetcode, please contact me to enjoy it to
204204

205205
|Day 91| [1296. Divide Array in Sets of K Consecutive Numbers](https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/) | [javascript]()|[:memo:](https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/)|Medium|
206206

207-
|Day 92| [881. Boats to Save People](https://leetcode.com/problems/boats-to-save-people/) | [javascript]()|[:memo:](https://leetcode.com/problems/boats-to-save-people/)|Medium|
207+
|Day 92| [881. Boats to Save People](https://leetcode.com/problems/boats-to-save-people/) | [javascript]()|[:memo:](https://leetcode.com/problems/boats-to-save-people/)|Medium|
208+
209+
210+
|Day 93| [1658. Minimum Operations to Reduce X to Zero](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/) | [javascript]()|[:memo:](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/)|Medium|

0 commit comments

Comments
 (0)