1217:玩筹码

This commit is contained in:
huangge1199 2021-09-08 15:32:15 +08:00
parent a22ae134a8
commit 51eb90bb37
2 changed files with 104 additions and 0 deletions

View File

@ -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)
}

View File

@ -0,0 +1,38 @@
<p>数轴上放置了一些筹码,每个筹码的位置存在数组&nbsp;<code>chips</code>&nbsp;当中。</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>&nbsp;</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>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= chips.length &lt;= 100</code></li>
<li><code>1 &lt;= chips[i] &lt;= 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>