Problem Statement

Pattern:


Solution

public int minCostToMoveChips(int[] position) {
	int evenCount = 0, oddCount = 0;
	for(int pos : position)
		if((pos & 1) == 0) evenCount++;
		else oddCount++;
	return Math.min(evenCount, oddCount);
}

Notes

  • cost of moving coins back over in 2s increments is 0.
    • Odd positions move all the way back to pos 1 for free
    • Even position move all the way back to 0 for free
    • Now depending on if no. of coins on 1 or 0 is less, you can shift the minimum no. of coins onto the other stack.
    • Simply find the no. of odd and even postions. and return the min of those 🤷‍♂️