Skip to content

Commit 056f4b2

Browse files
committed
commit
Change-Id: I27c3e15333fd8ff3d48b1da2e86af88e20ea2b99
1 parent c00675d commit 056f4b2

File tree

7 files changed

+140
-33
lines changed

7 files changed

+140
-33
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ out
2626

2727
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2828
hs_err_pid*
29+
*/*.iml
2930

.idea/leetcode/editor.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 46 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MODULE_DIR$/../lib/junit-4.12.jar!/" />
14+
<root url="jar://$MODULE_DIR$/../lib/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
20+
</component>
21+
</module>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Author: 王俊超
3+
* Time: 2020-06-28 21:38
4+
* CSDN: http://blog.csdn.net/derrantcm
5+
* Github: https://github.com/Wang-Jun-Chao
6+
* Declaration: All Rights Reserved !!!
7+
**/
8+
public class Solution {
9+
/**
10+
* 解题思路:
11+
* 假设数组最小的值是xmin,一共加了k次,加完后的值是W,元素个数是n
12+
* 则:W <= xmin + k
13+
* k(n-1) + sum(nums) = W*n
14+
* ==>
15+
* k(n-1) + sum(nums) <= (xmin + k)n
16+
* ==>
17+
* sum(nums) - n*xmin <= k
18+
*
19+
* @param nums
20+
* @return
21+
*/
22+
public int minMoves(int[] nums) {
23+
24+
int sum = 0;
25+
int min = Integer.MAX_VALUE;
26+
27+
for (int i : nums) {
28+
sum += i;
29+
if (i < min) {
30+
min = i;
31+
}
32+
}
33+
34+
return sum - min * nums.length;
35+
}
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
import java.lang.annotation.Target;
5+
6+
/**
7+
* Author: 王俊超
8+
* Time: 2020-06-28 22:09
9+
* CSDN: http://blog.csdn.net/derrantcm
10+
* Github: https://github.com/Wang-Jun-Chao
11+
* Declaration: All Rights Reserved !!!
12+
**/
13+
public class SolutionTest {
14+
@Test
15+
public void test1() {
16+
Solution s = new Solution();
17+
Object[][] data = {
18+
{new int[]{1, 2, 3}, 3},
19+
{new int[]{1, 1, 1}, 0},
20+
{new int[]{1, 1, 2}, 1},
21+
};
22+
23+
for (Object[] d : data) {
24+
Assert.assertEquals(s.minMoves((int[]) d[0]), d[1]);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)