ðŸ¥‰

# Minimum Cost to Move Chips to The Same Position

Created
2021/12/06 07:31
ë¬¸ì œ ë²ˆí˜¸
1217
ì¹´í…Œê³ ë¦¬
Array
Math
Greedy

### Code

 .css-15tnwsa{max-width:100%;width:100%;white-space:pre-wrap;word-break:break-word;padding:7px 9px;background-color:transparent;font-size:14px;line-height:20px;min-height:1em;}.css-15tnwsa:empty::after{content:" ";}ì œì¶œ ë‚ ì§œ ì‹œê°„ ë©”ëª¨ë¦¬ 2021/12/06 0 ms 2.2 MB
// 1217. Minimum Cost to Move Chips to The Same Position // // https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position/ // // Moving 2 steps == 0 cost // Moving 1 step == 1 cost // +, - direction possible // What is the minimum cost? // minCostToMoveChips function finds the minimum cost to make the chips the same position. // // Main logic is based on the parity of the position due to same parity has no cost. // // Thus, smaller value of the count for the parity will be the answer. func minCostToMoveChips(position []int) int { var cnt int for _, number := range position { if number%2 == 1 { cnt++ } } if cnt > len(position)-cnt { return len(position) - cnt } else { return cnt } }
Go