zoukankan      html  css  js  c++  java
  • 1217. Play with Chips

    There are some chips, and the i-th chip is at position chips[i].

    You can perform any of the two following types of moves any number of times (possibly zero) on any chip:

    • Move the i-th chip by 2 units to the left or to the right with a cost of 0.
    • Move the i-th chip by 1 unit to the left or to the right with a cost of 1.

    There can be two or more chips at the same position initially.

    Return the minimum cost needed to move all the chips to the same position (any position).

    移动一格cost1,移动2格cost0,本质上偶数位置的可以免费移动到任意偶数位,奇数位可以免费移动到任意奇数位,那实际上就可以把奇的都放到一个位置上,偶的放到一个位置上,然后哪边少就把少的移动到多的去。

    class Solution(object):
        def minCostToMoveChips(self, chips):
            """
            :type chips: List[int]
            :rtype: int
            """
            odd = 0
            even = 0
            for chip in chips:
                if chip % 2 == 0:
                    even += 1
                else:
                    odd += 1
            return min(even, odd)
  • 相关阅读:
    JavaScript数组方法--includes、indexOf、lastIndexOf
    JavaScript数组方法--flat、forEach、map
    JavaScript数组方法--filter、find、findIndex
    bootstrap-14
    bootstrap-13
    bootstrap-12
    bootstrap-11
    bootstrap-10
    bootstrap-9
    bootstrap-8
  • 原文地址:https://www.cnblogs.com/whatyouthink/p/13217923.html
Copyright © 2011-2022 走看看