import kotlin.math.abs
import kotlin.math.min
class Solution {
fun minCostToMoveChips(positions: IntArray): Int {
var minCost = Int.MAX_VALUE
for (i in 0 until positions.size) {
var cost = 0
for (j in 0 until positions.size) {
if (i != j) {
cost += (abs(positions[i] - positions[j]) % 2)
}
}
minCost = min(minCost, cost)
}
return minCost
}
}
시간복잡도 : O(N^2)
공간복잡도 : O(N)
import kotlin.math.min
class Solution {
fun minCostToMoveChips(positions: IntArray): Int {
var oddCount = 0
var evenCount = 0
for (position in positions) {
if (position and 1 == 1) oddCount++
else evenCount++
}
return min(oddCount, evenCount)
}
}
시간복잡도 : O(N)
공간복잡도 : O(N)