Skip to content

Commit 4310acb

Browse files
refactor 517
1 parent b167c2c commit 4310acb

File tree

1 file changed

+16
-26
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+16
-26
lines changed

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

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,26 @@ For each move, you could choose any m (1 ? m ? n) washing machines, and pass one
4242
4343
*/
4444
public class _517 {
45-
/**Reference: https://discuss.leetcode.com/topic/79938/super-short-easy-java-o-n-solution*/
46-
public int findMinMoves(int[] machines) {
47-
int total = 0;
48-
for (int i : machines) {
49-
total += i;
50-
}
51-
if (total % machines.length != 0) {
52-
return -1;
53-
}
54-
int avg = total / machines.length;
55-
int cnt = 0;
56-
int max = 0;
57-
for (int load : machines) {
58-
cnt += load - avg; //load-avg is "gain/lose"
59-
max = Math.max(Math.max(max, Math.abs(cnt)), load - avg);
60-
}
61-
return max;
62-
}
63-
64-
public static class Solution2 {
65-
/**TODO: finish it.*/
45+
public static class Solution1 {
46+
/**
47+
* Reference: https://discuss.leetcode.com/topic/79938/super-short-easy-java-o-n-solution
48+
*/
6649
public int findMinMoves(int[] machines) {
67-
int totalDresses = 0;
68-
for (int i = 0; i < machines.length; i++) {
69-
totalDresses += machines[i];
50+
int total = 0;
51+
for (int i : machines) {
52+
total += i;
7053
}
71-
if (totalDresses / machines.length == 0 || totalDresses % machines.length != 0) {
54+
if (total % machines.length != 0) {
7255
return -1;
7356
}
74-
return -1;
57+
int avg = total / machines.length;
58+
int cnt = 0;
59+
int max = 0;
60+
for (int load : machines) {
61+
cnt += load - avg; //load-avg is "gain/lose"
62+
max = Math.max(Math.max(max, Math.abs(cnt)), load - avg);
63+
}
64+
return max;
7565
}
7666
}
7767
}

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