Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
【每日一题】- 2019-10-14 - 1217. 玩筹码 #216
Comments
Intuition由于我们可以无代价的再奇偶性相同的位置进行移动,因此我们可以先将所有奇偶性相同的筹码放到一起,最后我们无法 因此我们只需要分别统计奇偶位置的筹码个数,然后取较小的即可。 时间复杂度: O(N) Python3 Codeclass Solution:
def minCostToMoveChips(self, chips: List[int]) -> int:
odd = 0
even = 0
for n in chips:
if n % 2 == 0:
even += 1
else:
odd += 1
return min(odd, even) |
这样不能实现吗? |
|
@Evolver-bug 可以的,这个Python语法,规定了返回值而已 |
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |

数轴上放置了一些筹码,每个筹码的位置存在数组 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
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/play-with-chips
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。