It is the first algorithm in weekly contest 157. I spend almost 20 minutes to read and think, and then finally I learn that odd or even position should be counted, and sum of odd position and sum of even position can be used to calculate the minimum cost.
time complexity: O(N), N is the size of the array
public class Solution {
public int MinCostToMoveChips(int[] chips) {
var even = 0;
var odd = 0;
foreach(var item in chips)
{
if(item % 2 == 0)
even++;
else
odd++;
}
return even >= odd? odd : even;
}
}
No comments:
Post a Comment