Skip to content

Commit e7b0c0d

Browse files
committedMar 1, 2025
Add solution #2460
1 parent efb40d9 commit e7b0c0d

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@
662662
2425|[Bitwise XOR of All Pairings](./2425-bitwise-xor-of-all-pairings.js)|Medium|
663663
2427|[Number of Common Factors](./2427-number-of-common-factors.js)|Easy|
664664
2429|[Minimize XOR](./2429-minimize-xor.js)|Medium|
665+
2460|[Apply Operations to an Array](./2460-apply-operations-to-an-array.js)|Easy|
665666
2462|[Total Cost to Hire K Workers](./2462-total-cost-to-hire-k-workers.js)|Medium|
666667
2467|[Most Profitable Path in a Tree](./2467-most-profitable-path-in-a-tree.js)|Medium|
667668
2469|[Convert the Temperature](./2469-convert-the-temperature.js)|Easy|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 2460. Apply Operations to an Array
3+
* https://leetcode.com/problems/apply-operations-to-an-array/
4+
* Difficulty: Easy
5+
*
6+
* You are given a 0-indexed array nums of size n consisting of non-negative integers.
7+
*
8+
* You need to apply n - 1 operations to this array where, in the ith operation (0-indexed),
9+
* you will apply the following on the ith element of nums:
10+
* - If nums[i] == nums[i + 1], then multiply nums[i] by 2 and set nums[i + 1] to 0.
11+
* Otherwise, you skip this operation.
12+
*
13+
* After performing all the operations, shift all the 0's to the end of the array.
14+
* - For example, the array [1,0,2,0,0,1] after shifting all its 0's to the end, is [1,2,1,0,0,0].
15+
*
16+
* Return the resulting array.
17+
*
18+
* Note that the operations are applied sequentially, not all at once.
19+
*/
20+
21+
/**
22+
* @param {number[]} nums
23+
* @return {number[]}
24+
*/
25+
var applyOperations = function(nums) {
26+
for (let i = 0; i < nums.length - 1; i++) {
27+
if (nums[i] === nums[i + 1]) {
28+
nums[i] *= 2;
29+
nums[i + 1] = 0;
30+
}
31+
}
32+
return [...nums.filter(n => n), ...nums.filter(n => !n)];
33+
};

0 commit comments

Comments
 (0)
Please sign in to comment.