517:超级洗衣机
This commit is contained in:
parent
4d4140d65f
commit
592c7d9006
85
src/main/java/leetcode/editor/cn/SuperWashingMachines.java
Normal file
85
src/main/java/leetcode/editor/cn/SuperWashingMachines.java
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
//假设有 n 台超级洗衣机放在同一排上。开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的。
|
||||||
|
//
|
||||||
|
// 在每一步操作中,你可以选择任意 m (1 <= m <= n) 台洗衣机,与此同时将每台洗衣机的一件衣服送到相邻的一台洗衣机。
|
||||||
|
//
|
||||||
|
// 给定一个整数数组 machines 代表从左至右每台洗衣机中的衣物数量,请给出能让所有洗衣机中剩下的衣物的数量相等的 最少的操作步数 。如果不能使每台洗衣
|
||||||
|
//机中衣物的数量相等,则返回 -1 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:machines = [1,0,5]
|
||||||
|
//输出:3
|
||||||
|
//解释:
|
||||||
|
//第一步: 1 0 <-- 5 => 1 1 4
|
||||||
|
//第二步: 1 <-- 1 <-- 4 => 2 1 3
|
||||||
|
//第三步: 2 1 <-- 3 => 2 2 2
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:machines = [0,3,0]
|
||||||
|
//输出:2
|
||||||
|
//解释:
|
||||||
|
//第一步: 0 <-- 3 0 => 1 2 0
|
||||||
|
//第二步: 1 2 --> 0 => 1 1 1
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:machines = [0,2,0]
|
||||||
|
//输出:-1
|
||||||
|
//解释:
|
||||||
|
//不可能让所有三个洗衣机同时剩下相同数量的衣物。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// n == machines.length
|
||||||
|
// 1 <= n <= 10⁴
|
||||||
|
// 0 <= machines[i] <= 10⁵
|
||||||
|
//
|
||||||
|
// Related Topics 贪心 数组 👍 174 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import javax.swing.plaf.IconUIResource;
|
||||||
|
|
||||||
|
//517:超级洗衣机
|
||||||
|
class SuperWashingMachines {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new SuperWashingMachines().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int findMinMoves(int[] machines) {
|
||||||
|
int sum = 0;
|
||||||
|
for (int num : machines) {
|
||||||
|
sum += num;
|
||||||
|
}
|
||||||
|
if (sum % machines.length > 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int avg = sum / machines.length;
|
||||||
|
int bef = 0;
|
||||||
|
int result = 0;
|
||||||
|
for (int machine : machines) {
|
||||||
|
bef += machine - avg;
|
||||||
|
result = Math.max(result, Math.max(machine - avg, Math.abs(bef)));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
33
src/main/java/leetcode/editor/cn/doc/content/PathSumIii.md
Normal file
33
src/main/java/leetcode/editor/cn/doc/content/PathSumIii.md
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<p>给定一个二叉树的根节点 <code>root</code> ,和一个整数 <code>targetSum</code> ,求该二叉树里节点值之和等于 <code>targetSum</code> 的 <strong>路径</strong> 的数目。</p>
|
||||||
|
|
||||||
|
<p><strong>路径</strong> 不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<p><img src="https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg" style="width: 452px; " /></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
|
||||||
|
<strong>输出:</strong>3
|
||||||
|
<strong>解释:</strong>和等于 8 的路径有 3 条,如图所示。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
|
||||||
|
<strong>输出:</strong>3
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>二叉树的节点个数的范围是 <code>[0,1000]</code></li>
|
||||||
|
<li><meta charset="UTF-8" /><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code> </li>
|
||||||
|
<li><code>-1000 <= targetSum <= 1000</code> </li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>二叉树</li></div></div><br><div><li>👍 1054</li><li>👎 0</li></div>
|
@ -0,0 +1,48 @@
|
|||||||
|
<p>假设有 <code>n</code><strong> </strong>台超级洗衣机放在同一排上。开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的。</p>
|
||||||
|
|
||||||
|
<p>在每一步操作中,你可以选择任意 <code>m</code> (<code>1 <= m <= n</code>) 台洗衣机,与此同时将每台洗衣机的一件衣服送到相邻的一台洗衣机。</p>
|
||||||
|
|
||||||
|
<p>给定一个整数数组 <code>machines</code> 代表从左至右每台洗衣机中的衣物数量,请给出能让所有洗衣机中剩下的衣物的数量相等的 <strong>最少的操作步数 </strong>。如果不能使每台洗衣机中衣物的数量相等,则返回 <code>-1</code> 。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>machines = [1,0,5]
|
||||||
|
<strong>输出:</strong>3
|
||||||
|
<strong>解释:</strong>
|
||||||
|
第一步: 1 0 <-- 5 => 1 1 4
|
||||||
|
第二步: 1 <-- 1 <-- 4 => 2 1 3
|
||||||
|
第三步: 2 1 <-- 3 => 2 2 2
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>machines = [0,3,0]
|
||||||
|
<strong>输出:</strong>2
|
||||||
|
<strong>解释:</strong>
|
||||||
|
第一步: 0 <-- 3 0 => 1 2 0
|
||||||
|
第二步: 1 2 --> 0 => 1 1 1
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>machines = [0,2,0]
|
||||||
|
<strong>输出:</strong>-1
|
||||||
|
<strong>解释:</strong>
|
||||||
|
不可能让所有三个洗衣机同时剩下相同数量的衣物。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>n == machines.length</code></li>
|
||||||
|
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||||
|
<li><code>0 <= machines[i] <= 10<sup>5</sup></code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li></div></div><br><div><li>👍 174</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user