1217:玩筹码
This commit is contained in:
parent
a22ae134a8
commit
51eb90bb37
@ -0,0 +1,66 @@
|
|||||||
|
//数轴上放置了一些筹码,每个筹码的位置存在数组 chips 当中。
|
||||||
|
//
|
||||||
|
// 你可以对 任何筹码 执行下面两种操作之一(不限操作次数,0 次也可以):
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 将第 i 个筹码向左或者右移动 2 个单位,代价为 0。
|
||||||
|
// 将第 i 个筹码向左或者右移动 1 个单位,代价为 1。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 最开始的时候,同一位置上也可能放着两个或者更多的筹码。
|
||||||
|
//
|
||||||
|
// 返回将所有筹码移动到同一位置(任意位置)上所需要的最小代价。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
// 输入:chips = [1,2,3]
|
||||||
|
//输出:1
|
||||||
|
//解释:第二个筹码移动到位置三的代价是 1,第一个筹码移动到位置三的代价是 0,总代价为 1。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
// 输入:chips = [2,2,2,3,3]
|
||||||
|
//输出:2
|
||||||
|
//解释:第四和第五个筹码移动到位置二的代价都是 1,所以最小总代价为 2。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= chips.length <= 100
|
||||||
|
// 1 <= chips[i] <= 10^9
|
||||||
|
//
|
||||||
|
// Related Topics 贪心 数组 数学 👍 95 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
//1217:玩筹码
|
||||||
|
class MinimumCostToMoveChipsToTheSamePosition{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new MinimumCostToMoveChipsToTheSamePosition().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int minCostToMoveChips(int[] position) {
|
||||||
|
int odd = 0;
|
||||||
|
int dou = 0;
|
||||||
|
for (int j : position) {
|
||||||
|
if (j % 2 == 0) {
|
||||||
|
dou++;
|
||||||
|
} else {
|
||||||
|
odd++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Math.min(dou,odd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
<p>数轴上放置了一些筹码,每个筹码的位置存在数组 <code>chips</code> 当中。</p>
|
||||||
|
|
||||||
|
<p>你可以对 <strong>任何筹码</strong> 执行下面两种操作之一(<strong>不限操作次数</strong>,0 次也可以):</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>将第 <code>i</code> 个筹码向左或者右移动 2 个单位,代价为 <strong>0</strong>。</li>
|
||||||
|
<li>将第 <code>i</code> 个筹码向左或者右移动 1 个单位,代价为 <strong>1</strong>。</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p>最开始的时候,同一位置上也可能放着两个或者更多的筹码。</p>
|
||||||
|
|
||||||
|
<p>返回将所有筹码移动到同一位置(任意位置)上所需要的最小代价。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>chips = [1,2,3]
|
||||||
|
<strong>输出:</strong>1
|
||||||
|
<strong>解释:</strong>第二个筹码移动到位置三的代价是 1,第一个筹码移动到位置三的代价是 0,总代价为 1。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>chips = [2,2,2,3,3]
|
||||||
|
<strong>输出:</strong>2
|
||||||
|
<strong>解释:</strong>第四和第五个筹码移动到位置二的代价都是 1,所以最小总代价为 2。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= chips.length <= 100</code></li>
|
||||||
|
<li><code>1 <= chips[i] <= 10^9</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>数学</li></div></div><br><div><li>👍 95</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user