Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ec08bcf

Browse files
committed
Add solution #1217
1 parent 6fe5d3e commit ec08bcf

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 1217. Minimum Cost to Move Chips to The Same Position
3+
* https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position/
4+
* Difficulty: Easy
5+
*
6+
* We have n chips, where the position of the ith chip is position[i].
7+
*
8+
* We need to move all the chips to the same position. In one step, we can change the position
9+
* of the ith chip from position[i] to:
10+
* - position[i] + 2 or position[i] - 2 with cost = 0.
11+
* - position[i] + 1 or position[i] - 1 with cost = 1.
12+
*
13+
* Return the minimum cost needed to move all the chips to the same position.
14+
*/
15+
16+
/**
17+
* @param {number[]} position
18+
* @return {number}
19+
*/
20+
var minCostToMoveChips = function(position) {
21+
const count = position.reduce((result, n) => n % 2 ? ++result : result, 0);
22+
return count >= position.length / 2 ? position.length - count : count;
23+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@
323323
1122|[Relative Sort Array](./1122-relative-sort-array.js)|Easy|
324324
1189|[Maximum Number of Balloons](./1189-maximum-number-of-balloons.js)|Easy|
325325
1207|[Unique Number of Occurrences](./1207-unique-number-of-occurrences.js)|Easy|
326+
1217|[Minimum Cost to Move Chips to The Same Position](./1217-minimum-cost-to-move-chips-to-the-same-position.js)|Easy|
326327
1232|[Check If It Is a Straight Line](./1232-check-if-it-is-a-straight-line.js)|Easy|
327328
1233|[Remove Sub-Folders from the Filesystem](./1233-remove-sub-folders-from-the-filesystem.js)|Medium|
328329
1249|[Minimum Remove to Make Valid Parentheses](./1249-minimum-remove-to-make-valid-parentheses.js)|Medium|

0 commit comments

Comments
 (0)