Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a0f1a0

Browse files
authored
Create 2460-apply-operations-to-an-array.js
Solved apply-operations-to-an-array
1 parent 3a66f08 commit 5a0f1a0

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Array | Simulation
3+
* Time O(n) | Space O(n)
4+
* https://leetcode.com/problems/apply-operations-to-an-array
5+
* @param {number[]} nums
6+
* @return {number[]}
7+
*/
8+
var applyOperations = function(nums) {
9+
10+
for (let i = 1; i < nums.length; i++) {
11+
if (nums[i] === nums[i-1]) {
12+
nums[i-1] = nums[i-1] * 2;
13+
nums[i] = 0;
14+
}
15+
}
16+
17+
const nonZeros = nums.filter((num) => num !== 0);
18+
return [...nonZeros, ...new Array(nums.length - nonZeros.length).fill(0)];
19+
};

0 commit comments

Comments
 (0)