{"id":5712,"date":"2019-10-05T21:25:08","date_gmt":"2019-10-06T04:25:08","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5712"},"modified":"2019-10-05T21:25:18","modified_gmt":"2019-10-06T04:25:18","slug":"leetcode-1217-play-with-chips","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1217-play-with-chips\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1217. Play with Chips"},"content":{"rendered":"\n<p>There are some chips, and the i-th chip is at position&nbsp;<code>chips[i]<\/code>.<\/p>\n\n\n\n<p>You can perform any of the two following types of moves&nbsp;<strong>any number of times<\/strong>&nbsp;(possibly&nbsp;zero)&nbsp;<strong>on any chip<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Move the&nbsp;<code>i<\/code>-th chip&nbsp;by&nbsp;2 units to the left or to the right with a cost of&nbsp;<strong>0<\/strong>.<\/li><li>Move&nbsp;the&nbsp;<code>i<\/code>-th chip&nbsp;by&nbsp;1 unit to the left or to the right with a cost of&nbsp;<strong>1<\/strong>.<\/li><\/ul>\n\n\n\n<p>There can be two or more chips&nbsp;at the same position initially.<\/p>\n\n\n\n<p>Return the&nbsp;minimum cost needed to move all the chips to the same position (any position).<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> chips = [1,2,3]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> Second chip will be moved to positon 3 with cost 1. First chip will be moved to position 3 with cost 0. Total cost is 1.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> chips = [2,2,2,3,3]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> Both fourth and fifth chip will be moved to position two with cost 1. Total minimum cost will be 2.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= chips.length &lt;= 100<\/code><\/li><li><code>1 &lt;= chips[i] &lt;= 10^9<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>We can choose either: <br>1. move all odd positions to an arbitrary even position and move the rest to the same position<br>2. move all even positions to an arbitrary odd position and move  the rest to the same position<br>ans = min(# of odd pos, # of even pos)<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minCostToMoveChips(vector<int>& chips) {\n    int odd = 0;\n    int even = 0;\n    for (int p : chips) {\n      if (p % 2) {\n        ++odd;\n      } else {\n        ++even;\n      }\n    }\n    return min(odd, even);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are some chips, and the i-th chip is at position&nbsp;chips[i]. You can perform any of the two following types of moves&nbsp;any number of times&nbsp;(possibly&nbsp;zero)&nbsp;on&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[20,222,31],"class_list":["post-5712","post","type-post","status-publish","format-standard","hentry","category-math","tag-array","tag-easy","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5712","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5712"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5712\/revisions"}],"predecessor-version":[{"id":5714,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5712\/revisions\/5714"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}