Skip to content

Commit 113a987

Browse files
committedDec 1, 2024·
[LEET-3353] add 3353
1 parent 6e4e51a commit 113a987

File tree

3 files changed

+150
-104
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+150
-104
lines changed
 

‎paginated_contents/algorithms/4th_thousand/README.md

Lines changed: 105 additions & 104 deletions
Large diffs are not rendered by default.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
public class _3353 {
4+
public static class Solution1 {
5+
public int minOperations(int[] nums) {
6+
int minOps = 0;
7+
int delta = 0;
8+
int target = nums[nums.length - 1];
9+
for (int i = nums.length - 2; i >= 0; i--) {
10+
nums[i] += delta;
11+
if (nums[i] != target) {
12+
delta += target - nums[i];
13+
minOps++;
14+
}
15+
}
16+
return minOps;
17+
}
18+
}
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.fishercoder.solutions.fourththousand._3353;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class _3353Test {
10+
private _3353.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3353.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(2, solution1.minOperations(new int[] {1, 4, 2}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(0, solution1.minOperations(new int[] {10, 10, 10}));
25+
}
26+
}

0 commit comments

Comments
 (0)
Please sign in to comment.