#leetCode brush title documentary Day13

https://leetcode-cn.com/problems/play-with-chips/

Some chips placed on the number line, the present position of each array chips among the chips.

You can do one of the following two operations on any chips (unlimited number of operations 0 times may be):

The i-th chip to the left or right movement of two units, the cost is zero.
The i-th chip 1 is moved to the left or the right unit, the cost is 1.
The very beginning, the same location may also be placed two or more chips.

Returns the minimum cost to move all the chips on the same position (any position) required.

 

Example 1:

Input: chips = [1,2,3]
Output: 1
Explanation: the cost of the second chip is moved to position III is 1, the cost of the first chip is moved to position III is 0, 1 of total consideration.
Example 2:

Input: chips = [2,2,2,3,3]
Output: 2
Explanation: fourth and fifth chips moved to two positions are a consideration, the minimum total cost of two.
 

prompt:

1 <= chips.length <= 100
1 <= chips[i] <= 10^9

 

Chicken dishes to try:

See this title feel a little ignorant, so quiet Mimi went to see the hint (probably meaning odd to even and odd to even the cost of conversion is not required, so we have to count the numbers are all the parity status unified minimum number of steps)

 

 

Then feel about this idea can achieve this by: traversing the vector data, the number of odd and even number of statistics, and returns the smaller of the two parts.

 

 

. 1  class Solution {
 2  public :
 . 3      int minCostToMoveChips (Vector < int > & Chips) {
 . 4          int size = chips.size ();
 . 5          int A = 0 ; // even number 
. 6          int B = 0 ; // odd number 
. 7          for ( int I = 0 ; I <size; I ++ ) {
 . 8              IF (Chips [I]% 2 == 0 ) {
 . 9                  A ++ ;
10             } else {
11                 b ++;
12             }
13         }
14         return a > b ? b : a;
15     }
16 };

 

With wide, steady! (Hope you can look after little hint of it 555, quickly become strong!)

 

 

 

 

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/play-with-chips
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/xyy999/p/11831766.html