Skip to content

Commit 156ed33

Browse files
add 2460
1 parent 807a903 commit 156ed33

File tree

3 files changed

+60
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+60
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
| 2487 | [Remove Nodes From Linked List](https://leetcode.com/problems/remove-nodes-from-linked-list/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2487.java) || Medium | LinkedList, Stack
4949
| 2485 | [Find the Pivot Integer](https://leetcode.com/problems/find-the-pivot-integer/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2485.java) || Easy ||
5050
| 2467 | [Convert the Temperature](https://leetcode.com/problems/convert-the-temperature/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2469.java) || Easy ||
51+
| 2460 | [Apply Operations to an Array](https://leetcode.com/problems/apply-operations-to-an-array/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2460.java) || Easy ||
5152
| 2455 | [Average Value of Even Numbers That Are Divisible by Three](https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2455.java) || Easy ||
5253
| 2451 | [Odd String Difference](https://leetcode.com/problems/odd-string-difference/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2451.java) || Easy |
5354
| 2446 | [Determine if Two Events Have Conflict](https://leetcode.com/problems/determine-if-two-events-have-conflict/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2446.java) || Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
public class _2460 {
4+
public static class Solution1 {
5+
public int[] applyOperations(int[] nums) {
6+
for (int i = 0; i < nums.length - 1; i++) {
7+
if (nums[i] == nums[i + 1]) {
8+
nums[i] *= 2;
9+
nums[i + 1] = 0;
10+
}
11+
}
12+
//i points at the first zero element, j keeps moving and bypassing zeroes to find the next non-zero element
13+
for (int i = 0, j = 0; i < nums.length && j < nums.length; ) {
14+
while (i < nums.length && nums[i] != 0) {
15+
i++;
16+
}
17+
if (j < i) {
18+
j = i;
19+
}
20+
while (j < nums.length && nums[j] == 0) {
21+
j++;
22+
}
23+
if (j < nums.length) {
24+
nums[i++] = nums[j];
25+
nums[j++] = 0;
26+
}
27+
}
28+
return nums;
29+
}
30+
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2460;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
8+
9+
public class _2460Test {
10+
private static _2460.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _2460.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[]{1, 4, 2, 0, 0, 0}, solution1.applyOperations(new int[]{1, 2, 2, 1, 1, 0}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertArrayEquals(new int[]{1, 0}, solution1.applyOperations(new int[]{0, 1}));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)