Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【每日一题】- 2019-10-14 - 1217. 玩筹码 #216

Closed
azl397985856 opened this issue Oct 28, 2019 · 5 comments
Closed

【每日一题】- 2019-10-14 - 1217. 玩筹码 #216

azl397985856 opened this issue Oct 28, 2019 · 5 comments

Comments

@azl397985856
Copy link
Owner

@azl397985856 azl397985856 commented Oct 28, 2019

数轴上放置了一些筹码,每个筹码的位置存在数组 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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

@azl397985856 azl397985856 changed the title 每日一题】- 2019-10-14 - 1217. 玩筹码 【每日一题】- 2019-10-14 - 1217. 玩筹码 Oct 28, 2019
@azl397985856 azl397985856 added the Greedy label Oct 28, 2019
@maninbule
Copy link

@maninbule maninbule commented Oct 28, 2019

【c++】【 简单思维题】
假如最后移动到的位置为x,若x是奇数,那么代价就是所有偶数的个数,若x是偶数,则代价就是所有奇数的位置,所以然后奇数个数与偶数个数的最小值即可

class Solution {
public:
    int minCostToMoveChips(vector<int>& chips) {
        int res[2] = {0,0};
        for(const auto &v:chips) res[v&1]++;
        return min(res[0],res[1]);
    }
};

image

@azl397985856 azl397985856 removed the Greedy label Oct 28, 2019
@azl397985856
Copy link
Owner Author

@azl397985856 azl397985856 commented Oct 28, 2019

Intuition

由于我们可以无代价的再奇偶性相同的位置进行移动,因此我们可以先将所有奇偶性相同的筹码放到一起,最后我们无法无代价地移动了。 但是我们可以通过无代价移动使得这两堆筹码(当然也有可能是其中一堆是0,也就是一共一堆)处于相邻的位置,最后我们的代价就是移动较少个数的筹码到较多的上面即可

因此我们只需要分别统计奇偶位置的筹码个数,然后取较小的即可。

时间复杂度: O(N)
空间复杂度: O(1)

Python3 Code

class 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
Copy link

@Evolver-bug Evolver-bug commented Oct 29, 2019

数轴上放置了一些筹码,每个筹码的位置存在数组 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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
您好,我刚刚接触这个平台。想请教一下
def minCostToMoveChips(self, chips: List[int]) -> int:
这条代码中第二个参数chips: List[int]和->int是什么意思呀
如果去掉,直接写成:
class Solution:
def minCostToMoveChips(self, chips) :
odd = 0
even = 0

    for n in chips:
        if n % 2 == 0:
            even += 1
        else:
            odd += 1
    return min(odd, even)

这样不能实现吗?
class Solution:
def minCostToMoveChips(self, chips) :
a=int(len(chips)/2)

@azl397985856
Copy link
Owner Author

@azl397985856 azl397985856 commented Oct 29, 2019

@Evolver-bug 可以的,这个Python语法,规定了返回值而已

@stale
Copy link

@stale stale bot commented Dec 28, 2019

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.

@stale stale bot added the stale label Dec 28, 2019
@stale stale bot closed this Jan 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.