-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray1217.java
More file actions
54 lines (52 loc) · 1.57 KB
/
Array1217.java
File metadata and controls
54 lines (52 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package array;
/**
* @ProjectName: leetcode
* @Package: array
* @ClassName: Array1217
* @Author: markey
* @Description:
* 数轴上放置了一些筹码,每个筹码的位置存在数组 chips 当中。
*
* 你可以对 任何筹码 执行下面两种操作之一(不限操作次数,0 次也可以):
*
* 将第 i 个筹码向左或者右移动 2 个单位,代价为 0。
* 将第 i 个筹码向左或者右移动 1 个单位,代价为 1。
* 最开始的时候,同一位置上也可能放着两个或者更多的筹码。
*
* 返回将所有筹码移动到同一位置(任意位置)上所需要的最小代价。
*
*
*
* 示例 1:
*
* 输入:chips = [1,2,3]
* 输出:1
* 解释:第二个筹码移动到位置三的代价是 1,第一个筹码移动到位置三的代价是 0,总代价为 1。
* 示例 2:
*
* 输入:chips = [2,2,2,3,3]
* 输出:2
* 解释:第四和第五个筹码移动到位置二的代价都是 1,所以最小总代价为 2。
*
*
* 提示:
*
* 1 <= chips.length <= 100
* 1 <= chips[i] <= 10^9
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/play-with-chips
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2019/10/17 22:24
* @Version: 1.0
*/
public class Array1217 {
public int minCostToMoveChips(int[] chips) {
int count1 = 0, count2 = 0;
for (int i: chips) {
count1 += i%2;
count2 += (i+1)%2;
}
return Math.min(count1, count2);
}
}