Skip to content

Commit b7628c1

Browse files
committed
Add solution #517
1 parent 2107cfd commit b7628c1

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@
413413
514|[Freedom Trail](./0514-freedom-trail.js)|Hard|
414414
515|[Find Largest Value in Each Tree Row](./0515-find-largest-value-in-each-tree-row.js)|Medium|
415415
516|[Longest Palindromic Subsequence](./0516-longest-palindromic-subsequence.js)|Medium|
416+
517|[Super Washing Machines](./0517-super-washing-machines.js)|Hard|
416417
520|[Detect Capital](./0520-detect-capital.js)|Easy|
417418
521|[Longest Uncommon Subsequence I](./0521-longest-uncommon-subsequence-i.js)|Easy|
418419
530|[Minimum Absolute Difference in BST](./0530-minimum-absolute-difference-in-bst.js)|Easy|
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 517. Super Washing Machines
3+
* https://leetcode.com/problems/super-washing-machines/
4+
* Difficulty: Hard
5+
*
6+
* You have n super washing machines on a line. Initially, each washing machine has some
7+
* dresses or is empty.
8+
*
9+
* For each move, you could choose any m (1 <= m <= n) washing machines, and pass one dress
10+
* of each washing machine to one of its adjacent washing machines at the same time.
11+
*
12+
* Given an integer array machines representing the number of dresses in each washing machine
13+
* from left to right on the line, return the minimum number of moves to make all the washing
14+
* machines have the same number of dresses. If it is not possible to do it, return -1.
15+
*/
16+
17+
/**
18+
* @param {number[]} machines
19+
* @return {number}
20+
*/
21+
var findMinMoves = function(machines) {
22+
const total = machines.reduce((sum, num) => sum + num, 0);
23+
const target = total / machines.length;
24+
if (total % machines.length !== 0) return -1;
25+
26+
let result = 0;
27+
for (let i = 0, balance = 0; i < machines.length; i++) {
28+
balance += machines[i] - target;
29+
result = Math.max(result, Math.abs(balance), machines[i] - target);
30+
}
31+
return result;
32+
};

0 commit comments

Comments
 (0)