Skip to content

Commit cba0670

Browse files
refactor 453
1 parent ae32d87 commit cba0670

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

src/main/java/com/fishercoder/solutions/_453.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,26 @@ Only three moves are needed (remember each move increments two elements):
2020
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]*/
2121

2222
public class _453 {
23-
/**Looked at this solution: https://discuss.leetcode.com/topic/66557/java-o-n-solution-short
24-
* i.e. Add 1 to n-1 elements basically equals to subtracting 1 from one element. So the easiest way
25-
* to make all elements in this array equal is to make all of them equal to the minimum element.*/
26-
public static int minMoves(int[] nums) {
27-
if (nums.length == 0) {
28-
return 0;
23+
public static class Solution1 {
24+
/**
25+
* Credit: https://discuss.leetcode.com/topic/66557/java-o-n-solution-short
26+
* i.e. Add 1 to n-1 elements basically equals to subtracting 1 from one element. So the easiest way
27+
* to make all elements in this array equal is to make all of them equal to the minimum element.
28+
*/
29+
public int minMoves(int[] nums) {
30+
if (nums.length == 0) {
31+
return 0;
32+
}
33+
int min = nums[0];
34+
for (int n : nums) {
35+
min = Math.min(min, n);
36+
}
37+
int res = 0;
38+
for (int n : nums) {
39+
res += n - min;
40+
}
41+
return res;
2942
}
30-
int min = nums[0];
31-
for (int n : nums) {
32-
min = Math.min(min, n);
33-
}
34-
int res = 0;
35-
for (int n : nums) {
36-
res += n - min;
37-
}
38-
return res;
39-
}
40-
41-
public static void main(String... args) {
42-
int[] nums = new int[]{1, 2, 3};
43-
System.out.println(minMoves(nums));
4443
}
4544

4645
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._453;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
9+
10+
public class _453Test {
11+
private static _453.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _453.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(3, solution1.minMoves(new int[]{1, 2, 3}));
21+
}
22+
23+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy