1217. Minimum Cost to Move Chips to The Same Position

LeetCode easy original: C# #array #csharp #easy #leetcode #math
Il testo del problema è tradotto dal russo per la lingua selezionata. Il codice resta invariato.

У нас есть n фишек, где позиция i-й фишки равна position[i].

Нам нужно переместить все фишки в одну и ту же позицию. За один шаг мы можем изменить позицию i-й фишки с position[i] на:

position[i] + 2 или position[i] - 2 с затратами = 0.

position[i] + 1 или position[i] - 1 с затратами = 1.

return минимальные затраты, необходимые для перемещения всех фишек в одну и ту же позицию.

Esempio:

Input: position = [2,2,2,3,3]

Output: 2

Explanation: We can move the two chips at position 3 to position 2. Each move has cost = 1. The total cost = 2.

C# soluzione

abbinato/originale
public class Solution {
    public int MinCostToMoveChips(int[] position) {
        int evenCount = 0;
        int oddCount = 0;
        
        foreach (int pos in position) {
            if (pos % 2 == 0) {
                evenCount++;
            } else {
                oddCount++;
            }
        }
        
        return Math.Min(evenCount, oddCount);
    }
}

C++ soluzione

bozza automatica, rivedere prima dell'invio
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    public int MinCostToMoveChips(vector<int>& position) {
        int evenCount = 0;
        int oddCount = 0;
        
        foreach (int pos in position) {
            if (pos % 2 == 0) {
                evenCount++;
            } else {
                oddCount++;
            }
        }
        
        return min(evenCount, oddCount);
    }
}

Java soluzione

abbinato/originale
class Solution {
    public int minCostToMoveChips(int[] position) {
        int even_cnt = 0;
        int odd_cnt = 0;
        for (int i : position) {
            if (i % 2 == 0) {
                even_cnt++;
            } else {
                odd_cnt++;
            }
        }
        return Math.min(odd_cnt, even_cnt);
    }
}

JavaScript soluzione

abbinato/originale
var minCostToMoveChips = function(position) {
    let evenCount = 0;
    let oddCount = 0;
    
    for (let pos of position) {
        if (pos % 2 === 0) {
            evenCount++;
        } else {
            oddCount++;
        }
    }
    
    return Math.min(evenCount, oddCount);
};

Python soluzione

abbinato/originale
class Solution:
    def minCostToMoveChips(self, position: List[int]) -> int:
        even_count = sum(1 for pos in position if pos % 2 == 0)
        odd_count = len(position) - even_count
        return min(even_count, odd_count)

Go soluzione

abbinato/originale
func minCostToMoveChips(position []int) int {
    evenCount, oddCount := 0, 0
    
    for _, pos := range position {
        if pos % 2 == 0 {
            evenCount++
        } else {
            oddCount++
        }
    }
    
    if evenCount < oddCount {
        return evenCount
    }
    return oddCount
}

Algorithm

Посчитать количество фишек на четных и нечетных позициях.

Сравнить количество фишек на четных и нечетных позициях.

Вернуть минимальное количество фишек как минимальную стоимость для перемещения всех фишек в одну позицию.

😎

Vacancies for this task

offerte attive with overlapping task tags are mostrati.

Tutte le offerte
Non ci sono ancora offerte attive.