Skip to content

Commit 2d22a26

Browse files
authored
Create 2422-merge-operations-to-turn-array-into-a-palindrome.js
1 parent 4608d03 commit 2d22a26

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const minimumOperations = function(nums) {
6+
const n = nums.length
7+
let left = nums[0], right = nums[n - 1]
8+
let cnt = 0;
9+
for (let i = 0, j = n - 1; i < j;) {
10+
if (left === right) {
11+
i++;
12+
j--;
13+
left = nums[i];
14+
right = nums[j];
15+
} else if (left < right) {
16+
i++;
17+
left += nums[i];
18+
cnt++;
19+
} else if (left > right) {
20+
j--;
21+
right += nums[j];
22+
cnt++;
23+
}
24+
}
25+
return cnt;
26+
};

0 commit comments

Comments
 (0)