Skip to content

Commit fdef2d9

Browse files
committed
添加 problem 1217
1 parent 3ec36c7 commit fdef2d9

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode
2+
3+
func minCostToMoveChips(chips []int) int {
4+
odd, even := 0, 0
5+
for _, c := range chips {
6+
if c%2 == 0 {
7+
even++
8+
} else {
9+
odd++
10+
}
11+
}
12+
return min(odd, even)
13+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1217 struct {
9+
para1217
10+
ans1217
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1217 struct {
16+
arr []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1217 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1217(t *testing.T) {
26+
27+
qs := []question1217{
28+
29+
question1217{
30+
para1217{[]int{1, 2, 3}},
31+
ans1217{1},
32+
},
33+
34+
question1217{
35+
para1217{[]int{2, 2, 2, 3, 3}},
36+
ans1217{2},
37+
},
38+
}
39+
40+
fmt.Printf("------------------------Leetcode Problem 1217------------------------\n")
41+
42+
for _, q := range qs {
43+
_, p := q.ans1217, q.para1217
44+
fmt.Printf("【input】:%v 【output】:%v\n", p, minCostToMoveChips(p.arr))
45+
}
46+
fmt.Printf("\n\n\n")
47+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# [1217. Play with Chips](https://leetcode.com/problems/play-with-chips/)
2+
3+
4+
## 题目:
5+
6+
There are some chips, and the i-th chip is at position `chips[i]`.
7+
8+
You can perform any of the two following types of moves **any number of times** (possibly zero) **on any chip**:
9+
10+
- Move the `i`-th chip by 2 units to the left or to the right with a cost of **0**.
11+
- Move the `i`-th chip by 1 unit to the left or to the right with a cost of **1**.
12+
13+
There can be two or more chips at the same position initially.
14+
15+
Return the minimum cost needed to move all the chips to the same position (any position).
16+
17+
**Example 1:**
18+
19+
Input: chips = [1,2,3]
20+
Output: 1
21+
Explanation: Second chip will be moved to positon 3 with cost 1. First chip will be moved to position 3 with cost 0. Total cost is 1.
22+
23+
**Example 2:**
24+
25+
Input: chips = [2,2,2,3,3]
26+
Output: 2
27+
Explanation: Both fourth and fifth chip will be moved to position two with cost 1. Total minimum cost will be 2.
28+
29+
**Constraints:**
30+
31+
- `1 <= chips.length <= 100`
32+
- `1 <= chips[i] <= 10^9`
33+
34+
35+
## 题目大意
36+
37+
38+
数轴上放置了一些筹码,每个筹码的位置存在数组 chips 当中。你可以对 任何筹码 执行下面两种操作之一(不限操作次数,0 次也可以):
39+
40+
- 将第 i 个筹码向左或者右移动 2 个单位,代价为 0。
41+
- 将第 i 个筹码向左或者右移动 1 个单位,代价为 1。
42+
43+
最开始的时候,同一位置上也可能放着两个或者更多的筹码。返回将所有筹码移动到同一位置(任意位置)上所需要的最小代价。
44+
45+
46+
提示:
47+
48+
- 1 <= chips.length <= 100
49+
- 1 <= chips[i] <= 10^9
50+
51+
52+
## 解题思路
53+
54+
- 给出一个数组,数组的下标代表的是数轴上的坐标点,数组的元素代表的是砝码大小。砝码移动规则,左右移动 2 格,没有代价,左右移动 1 个,代价是 1 。问最终把砝码都移动到一个格子上,最小代价是多少。
55+
- 先解读砝码移动规则:偶数位置的到偶数位置的没有代价,奇数到奇数位置的没有代价。利用这个规则,我们可以把所有的砝码**无代价**的摞在一个奇数的位置上和一个偶数的位置上。这样我们只用关心这两个位置了。并且这两个位置可以连续在一起。最后一步即将相邻的这两摞砝码合并到一起。由于左右移动一个代价是 1,所以最小代价的操作是移动最少砝码的那一边。奇数位置上砝码少就移动奇数位置上的,偶数位置上砝码少就移动偶数位置上的。所以这道题解法变的异常简单,遍历一次数组,找到其中有多少个奇数和偶数位置的砝码,取其中比较少的,就是最终答案。

0 commit comments

Comments
 (0)