LeetCode——1217. Playing Chips

Title description:

Some chips are placed on the number line, and the position of each chip is stored in the array chips .

You can perform one of the following two operations on any chip (there is no limit to the number of operations, 0 times is fine):

  • Method 1: Move the i-th chip to the left or right by 2 units, and the cost is 0.
  • Method 2: Move the i-th chip to the left or right by 1 unit at a cost of 1.

At the beginning, there may be two or more chips in the same position. Returns the minimum cost required to move all chips to the same position (any position).

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

Example 1:
Input: chips = [1,2,3]
Output: 1
Explanation: The cost of moving the second chip to position three is 1, the cost of moving the first chip to position three is 0, and the total cost is 1.

Example 2:
Input: chips = [2,2,2,3,3]
Output: 2
Explanation: The cost of moving the fourth and fifth chips to position two is 1, so the minimum total cost is 2.

Greedy Algorithm: Ideas for Solving Problems

In order to ensure that the total cost value is the smallest, for each move selection, if we can choose a movement mode with a cost of 0 (mode 1), we will never choose a movement mode with a cost of 1 (mode 2). Therefore, we try to move as many chips together as possible through the first method, and when the remaining chips can no longer be moved to the same position through the first method, try to use the least number of times to move the second.
Law : We can move all odd-numbered chips to the same position (denoted as position A), and at the same time we can move all even-numbered chips to the same position (denoted as position B), and position A and position B can be on the number axis Any two adjacent positions (that is |AB|=1, they are adjacent, because the adjacent positions move the least number of times. For convenience, we will uniformly move the odd-numbered chips to position[1] ,Move even-numbered chips to Postion[2]).
All odd-numbered chips can be moved to position[1] in N times, and all even-numbered chips can be moved to position[1] in M ​​times. [2]. Here, the total cost is 0, and then use K times method two to move the pile with the smaller number of chips to the other pile. K is min. (position[1], position[2]). The number of chips in the position[1] and the position[2] is actually the number of chips in the odd position and the number of chips in the even position in the position array.

code show as below:

class Solution {
    
    
    public int minCostToMoveChips(int[] position) {
    
    
        int n = position.length;
        int a = 0, b = 0;
        for (int m : position) {
    
    
            if (m % 2 == 0) {
    
    
                a++;
            } else {
    
    
                b++;
            }
        }
        return a > b ? b : a;
    }   
}

Results of the:
Insert picture description here

Guess you like

Origin blog.csdn.net/FYPPPP/article/details/114302657