Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9d21d12

Browse files
✨feat: add 1217
1 parent 64afc95 commit 9d21d12

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

‎Index/并查集.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
33
| [417. 太平洋大西洋水流问题](https://leetcode-cn.com/problems/pacific-atlantic-water-flow/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/pacific-atlantic-water-flow/solution/by-ac_oier-do7d/) | 中等 | 🤩🤩🤩🤩🤩 |
44
| [675. 为高尔夫比赛砍树](https://leetcode.cn/problems/cut-off-trees-for-golf-event/) | [LeetCode 题解链接](https://leetcode.cn/problems/cut-off-trees-for-golf-event/solution/by-ac_oier-ksth/) | 困难 | 🤩🤩🤩 |
5-
| [765. 情侣牵手](https://leetcode-cn.com/problems/couples-holding-hands/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/couples-holding-hands/solution/liang-chong-100-de-jie-fa-bing-cha-ji-ta-26a6/) | 困难 | 🤩🤩🤩 |
5+
| [765. 情侣牵手](https://leetcode-cn.com/problems/couples-holding-hands/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/couples-holding-hands/solution/liang-chong-100-de-jie-fa-bing-cha-ji-ta-26a6/) | 困难 | 🤩🤩🤩🤩 |
66
| [778. 水位上升的泳池中游泳](https://leetcode-cn.com/problems/swim-in-rising-water/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/swim-in-rising-water/solution/gong-shui-san-xie-yi-ti-shuang-jie-krusk-7c6o/) | 困难 | 🤩🤩🤩 |
7+
| [815. 公交路线](https://leetcode-cn.com/problems/bus-routes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/bus-routes/solution/gong-shui-san-xie-yi-ti-shuang-jie-po-su-1roh/) | 困难 | 🤩🤩🤩🤩 |
78
| [1020. 飞地的数量](https://leetcode-cn.com/problems/number-of-enclaves/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-enclaves/solution/gong-shui-san-xie-bing-cha-ji-dfs-yun-yo-oyh1/) | 中等 | 🤩🤩🤩 |
89
| [1631. 最小体力消耗路径](https://leetcode-cn.com/problems/path-with-minimum-effort/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-with-minimum-effort/solution/fan-zheng-fa-zheng-ming-si-lu-de-he-fa-x-ohby/) | 中等 | 🤩🤩🤩 |
910

‎Index/贪心算法.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
| [942. 增减字符串匹配](https://leetcode.cn/problems/di-string-match/) | [LeetCode 题解链接](https://leetcode.cn/problems/di-string-match/solution/by-ac_oier-pvjk/) | 简单 | 🤩🤩🤩🤩🤩 |
2424
| [995. K 连续位的最小翻转次数](https://leetcode-cn.com/problems/minimum-number-of-k-consecutive-bit-flips/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-number-of-k-consecutive-bit-flips/solution/po-su-tan-xin-jie-fa-yu-tan-xin-chai-fen-4lyy/) | 困难 | 🤩🤩🤩 |
2525
| [1005. K 次取反后最大化的数组和](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/solution/gong-shui-san-xie-jian-dan-fen-qing-kuan-6qwu/) | 简单 | 🤩🤩🤩🤩 |
26+
| [1217. 玩筹码](https://leetcode.cn/problems/minimum-cost-to-move-chips-to-the-same-position/) | [LeetCode 题解链接](https://leetcode.cn/problems/minimum-cost-to-move-chips-to-the-same-position/solution/by-ac_oier-j6js/) | 简单 | 🤩🤩🤩🤩 |
2627
| [1218. 最长定差子序列](https://leetcode-cn.com/problems/longest-arithmetic-subsequence-of-given-difference/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-arithmetic-subsequence-of-given-difference/solution/gong-shui-san-xie-jie-he-tan-xin-de-zhua-dj1k/) | 中等 | 🤩🤩🤩🤩🤩 |
2728
| [1221. 分割平衡字符串](https://leetcode-cn.com/problems/split-a-string-in-balanced-strings/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/split-a-string-in-balanced-strings/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-wumnk/) | 简单 | 🤩🤩🤩🤩 |
2829
| [1405. 最长快乐字符串](https://leetcode-cn.com/problems/longest-happy-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-happy-string/solution/gong-shui-san-xie-jie-he-you-xian-dui-li-q6fd/) | 中等 | 🤩🤩🤩🤩 |
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1217. 玩筹码](https://leetcode.cn/problems/minimum-cost-to-move-chips-to-the-same-position/solution/by-ac_oier-j6js/)** ,难度为 **简单**
4+
5+
Tag : 「贪心」、「枚举」
6+
7+
8+
9+
`n` 个筹码。第 $i$ 个筹码的位置是 `position[i]`
10+
11+
我们需要把所有筹码移到同一个位置。在一步中,我们可以将第 $i$ 个筹码的位置从 $position[i]$ 改变为:
12+
13+
* `position[i] + 2 或 position[i] - 2`,此时 `cost = 0`
14+
* `position[i] + 1 或 position[i] - 1`,此时 `cost = 1`
15+
16+
返回将所有筹码移动到同一位置上所需要的 最小代价 。
17+
18+
示例 1:
19+
![](https://assets.leetcode.com/uploads/2020/08/15/chips_e1.jpg)
20+
```
21+
输入:position = [1,2,3]
22+
23+
输出:1
24+
25+
解释:第一步:将位置3的筹码移动到位置1,成本为0。
26+
第二步:将位置2的筹码移动到位置1,成本= 1。
27+
总成本是1。
28+
```
29+
示例 2:
30+
![](https://assets.leetcode.com/uploads/2020/08/15/chip_e2.jpg)
31+
```
32+
输入:position = [2,2,2,3,3]
33+
34+
输出:2
35+
36+
解释:我们可以把位置3的两个筹码移到位置2。每一步的成本为1。总成本= 2。
37+
```
38+
示例 3:
39+
```
40+
输入:position = [1,1000000000]
41+
42+
输出:1
43+
```
44+
45+
提示:
46+
* 1ドル <= chips.length <= 100$
47+
* 1ドル <= chips[i] <= 10^9$
48+
49+
---
50+
51+
### 贪心 + 枚举目标位置
52+
53+
假设移动的目标位置是 $a,ドル当前所在位置是 $b,ドル将小球从 $b$ 移动到 $a$ 的成本取决于两位置距离的「奇偶性」,距离为偶数时成本固定为 0ドル,ドル距离为奇数时成本固定为 1ドル$。
54+
55+
同时我们可以通过「分情况讨论」来证明,所有小球移动到一个全新位置(起始没有小球的位置),结果不会变好,假设所选择的最终(全新)位置为 $t$:
56+
57+
* 假设选择的位置 $t$ 导致所有数到位置 $t$ 距离均为偶数,此时总成本为 0ドル,ドル同时可知所有数的位置奇偶性相同,此时选择所有数中的任意一个的位置,同样可得总成本为 0ドル$ 的结果,因此选全新的位置不会让结果变好;
58+
* 假设选择的位置 $t$ 导致所有数到位置 $t$ 距离均为奇数,此时总成本为 $n,ドル同时可知所有数的位置奇偶性相同,此时选择所有数中的任意一个的位置,可得总成本为 0ドル$ 的结果,因此选全新的位置会让结果变差;
59+
* 假设选择的位置 $t$ 导致所有数到位置 $t$ 距离奇数结果为 $c1,ドル偶数结果为 $c2,ドル可知 $n = c1 + c2,ドル同时我们通过调整 $t$ 的奇偶性来确保 $c1 <= c2$。此时总的成本为 $c1,ドル同时可知所有与 $t$ 距离为奇数的数所在位置奇偶性相同,所有与 $t$ 距离为偶数的数所在位置奇偶性也相同,此时将 $t$ 调整为与 $t$ 奇偶性相同的原有数的位置,同样能够得到总成本为 $c1$ 的结果,因此选全新的位置不会让结果变好。
60+
61+
综上,我们可以枚举所有已有的位置为目标位置,并通过奇偶性统计其余位置到目标位置的成本,在所有已有位置中取最小的总成本即是答案。
62+
63+
代码:
64+
```Java
65+
class Solution {
66+
public int minCostToMoveChips(int[] ps) {
67+
int n = ps.length, ans = Integer.MAX_VALUE;
68+
for (int i = 0; i < n; i++) {
69+
int a = ps[i], cur = 0;
70+
for (int j = 0; j < n; j++) {
71+
int b = ps[j];
72+
cur += Math.abs(a - b) % 2;
73+
}
74+
ans = Math.min(ans, cur);
75+
}
76+
return ans;
77+
}
78+
}
79+
```
80+
* 时间复杂度:$O(n^2)$
81+
* 空间复杂度:$O(1)$
82+
83+
---
84+
85+
### 贪心 + 统计奇偶性
86+
87+
更进一步,我们可以发现要使得「总的移动成本最优」的目标位置有无数个,只要目标位置的奇偶性不变,即可确保总成本不变。
88+
89+
因此我们可以省去枚举具体位置的操作,转而统计原有数的奇偶位置个数,假设偶数位置有 $a$ 个,奇数位置有 $b$ 个,最终目标位置选为偶数的成本为 $b,ドル最终目标位置选为奇数的成本为 $a,ドル即两者中的最小值即是答案。
90+
91+
代码:
92+
```Java
93+
class Solution {
94+
public int minCostToMoveChips(int[] ps) {
95+
int n = ps.length, a = 0;
96+
for (int i : ps) a += i % 2;
97+
return Math.min(a, n - a);
98+
}
99+
}
100+
```
101+
* 时间复杂度:$O(n)$
102+
* 空间复杂度:$O(1)$
103+
104+
---
105+
106+
### 最后
107+
108+
这是我们「刷穿 LeetCode」系列文章的第 `No.1217` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
109+
110+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
111+
112+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
113+
114+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
115+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /