Skip to content

Commit 62b7690

Browse files
committed
auto commit
1 parent 2dee14d commit 62b7690

File tree

2 files changed

+178
-174
lines changed

2 files changed

+178
-174
lines changed

docs/notes/Leetcode 题解 - 贪心思想.md

Lines changed: 89 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<!-- GFM-TOC -->
2-
* [分配饼干](#分配饼干)
3-
* [不重叠的区间个数](#不重叠的区间个数)
4-
* [投飞镖刺破气球](#投飞镖刺破气球)
5-
* [根据身高和序号重组队列](#根据身高和序号重组队列)
6-
* [分隔字符串使同种字符出现在一起](#分隔字符串使同种字符出现在一起)
7-
* [种植花朵](#种植花朵)
8-
* [判断是否为子序列](#判断是否为子序列)
9-
* [修改一个数成为非递减数组](#修改一个数成为非递减数组)
10-
* [股票的最大收益](#股票的最大收益)
11-
* [子数组最大的和](#子数组最大的和)
12-
* [买入和售出股票最大的收益](#买入和售出股票最大的收益)
2+
* [1. 分配饼干](#1-分配饼干)
3+
* [2. 不重叠的区间个数](#2-不重叠的区间个数)
4+
* [3. 投飞镖刺破气球](#3-投飞镖刺破气球)
5+
* [3. 根据身高和序号重组队列](#3-根据身高和序号重组队列)
6+
* [4. 买卖股票最大的收益](#4-买卖股票最大的收益)
7+
* [5. 买卖股票的最大收益 II](#5-买卖股票的最大收益-ii)
8+
* [6. 种植花朵](#6-种植花朵)
9+
* [7. 判断是否为子序列](#7-判断是否为子序列)
10+
* [8. 修改一个数成为非递减数组](#8-修改一个数成为非递减数组)
11+
* [9. 子数组最大的和](#9-子数组最大的和)
12+
* [10. 分隔字符串使同种字符出现在一起](#10-分隔字符串使同种字符出现在一起)
1313
<!-- GFM-TOC -->
1414

1515

1616
保证每次操作都是局部最优的,并且最后得到的结果是全局最优的。
1717

18-
# 分配饼干
18+
# 1. 分配饼干
1919

2020
[455. Assign Cookies (Easy)](https://leetcode.com/problems/assign-cookies/description/)
2121

@@ -49,7 +49,7 @@ public int findContentChildren(int[] g, int[] s) {
4949
}
5050
```
5151

52-
# 不重叠的区间个数
52+
# 2. 不重叠的区间个数
5353

5454
[435. Non-overlapping Intervals (Medium)](https://leetcode.com/problems/non-overlapping-intervals/description/)
5555

@@ -107,7 +107,7 @@ Arrays.sort(intervals, new Comparator<Interval>() {
107107
});
108108
```
109109

110-
# 投飞镖刺破气球
110+
# 3. 投飞镖刺破气球
111111

112112
[452. Minimum Number of Arrows to Burst Balloons (Medium)](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
113113

@@ -119,7 +119,7 @@ Output:
119119
2
120120
```
121121

122-
题目描述:气球在一个水平数轴上摆放,可以重叠,飞镖垂直投向坐标轴,使得路径上的气球都会刺破。求解最小的投飞镖次数使所有气球都被刺破。
122+
题目描述:气球在一个水平数轴上摆放,可以重叠,飞镖垂直投向坐标轴,使得路径上的气球都被刺破。求解最小的投飞镖次数使所有气球都被刺破。
123123

124124
也是计算不重叠的区间个数,不过和 Non-overlapping Intervals 的区别在于,[1, 2][2, 3] 在本题中算是重叠区间。
125125

@@ -141,7 +141,7 @@ public int findMinArrowShots(int[][] points) {
141141
}
142142
```
143143

144-
# 根据身高和序号重组队列
144+
# 3. 根据身高和序号重组队列
145145

146146
[406. Queue Reconstruction by Height(Medium)](https://leetcode.com/problems/queue-reconstruction-by-height/description/)
147147

@@ -157,7 +157,7 @@ Output:
157157

158158
为了使插入操作不影响后续的操作,身高较高的学生应该先做插入操作,否则身高较小的学生原先正确插入的第 k 个位置可能会变成第 k+1 个位置。
159159

160-
身高降序、k 值升序,然后按排好序的顺序插入队列的第 k 个位置中。
160+
身高 h 降序、个数 k 值升序,然后将某个学生插入队列的第 k 个位置中。
161161

162162
```java
163163
public int[][] reconstructQueue(int[][] people) {
@@ -173,48 +173,51 @@ public int[][] reconstructQueue(int[][] people) {
173173
}
174174
```
175175

176-
# 分隔字符串使同种字符出现在一起
176+
# 4. 买卖股票最大的收益
177177

178-
[763. Partition Labels (Medium)](https://leetcode.com/problems/partition-labels/description/)
178+
[121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
179179

180-
```html
181-
Input: S = "ababcbacadefegdehijhklij"
182-
Output: [9,7,8]
183-
Explanation:
184-
The partition is "ababcbaca", "defegde", "hijhklij".
185-
This is a partition so that each letter appears in at most one part.
186-
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits S into less parts.
187-
```
180+
题目描述:一次股票交易包含买入和卖出,只进行一次交易,求最大收益。
181+
182+
只要记录前面的最小价格,将这个最小价格作为买入价格,然后将当前的价格作为售出价格,查看当前收益是不是最大收益。
188183

189184
```java
190-
public List<Integer> partitionLabels(String S) {
191-
int[] lastIndexsOfChar = new int[26];
192-
for (int i = 0; i < S.length(); i++) {
193-
lastIndexsOfChar[char2Index(S.charAt(i))] = i;
194-
}
195-
List<Integer> partitions = new ArrayList<>();
196-
int firstIndex = 0;
197-
while (firstIndex < S.length()) {
198-
int lastIndex = firstIndex;
199-
for (int i = firstIndex; i < S.length() && i <= lastIndex; i++) {
200-
int index = lastIndexsOfChar[char2Index(S.charAt(i))];
201-
if (index > lastIndex) {
202-
lastIndex = index;
203-
}
204-
}
205-
partitions.add(lastIndex - firstIndex + 1);
206-
firstIndex = lastIndex + 1;
185+
public int maxProfit(int[] prices) {
186+
int n = prices.length;
187+
if (n == 0) return 0;
188+
int soFarMin = prices[0];
189+
int max = 0;
190+
for (int i = 1; i < n; i++) {
191+
if (soFarMin > prices[i]) soFarMin = prices[i];
192+
else max = Math.max(max, prices[i] - soFarMin);
207193
}
208-
return partitions;
194+
return max;
209195
}
196+
```
210197

211-
private int char2Index(char c) {
212-
return c - 'a';
198+
199+
# 5. 买卖股票的最大收益 II
200+
201+
[122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
202+
203+
题目描述:可以进行多次交易,多次交易之间不能交叉进行,可以进行多次交易。
204+
205+
对于 [a, b, c, d],如果有 a <= b <= c <= d ,那么最大收益为 d - a。而 d - a = (d - c) + (c - b) + (b - a) ,因此当访问到一个 prices[i] 且 prices[i] - prices[i-1] > 0,那么就把 prices[i] - prices[i-1] 添加到收益中。
206+
207+
```java
208+
public int maxProfit(int[] prices) {
209+
int profit = 0;
210+
for (int i = 1; i < prices.length; i++) {
211+
if (prices[i] > prices[i - 1]) {
212+
profit += (prices[i] - prices[i - 1]);
213+
}
214+
}
215+
return profit;
213216
}
214217
```
215218

216219

217-
# 种植花朵
220+
# 6. 种植花朵
218221

219222
[605. Can Place Flowers (Easy)](https://leetcode.com/problems/can-place-flowers/description/)
220223

@@ -223,7 +226,7 @@ Input: flowerbed = [1,0,0,0,1], n = 1
223226
Output: True
224227
```
225228

226-
题目描述:花朵之间至少需要一个单位的间隔,求解是否能种下 n 朵花。
229+
题目描述:flowerbed 数组中 1 表示已经种下了花朵。花朵之间至少需要一个单位的间隔,求解是否能种下 n 朵花。
227230

228231
```java
229232
public boolean canPlaceFlowers(int[] flowerbed, int n) {
@@ -244,7 +247,7 @@ public boolean canPlaceFlowers(int[] flowerbed, int n) {
244247
}
245248
```
246249

247-
# 判断是否为子序列
250+
# 7. 判断是否为子序列
248251

249252
[392. Is Subsequence (Medium)](https://leetcode.com/problems/is-subsequence/description/)
250253

@@ -266,7 +269,7 @@ public boolean isSubsequence(String s, String t) {
266269
}
267270
```
268271

269-
# 修改一个数成为非递减数组
272+
# 8. 修改一个数成为非递减数组
270273

271274
[665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/)
272275

@@ -276,9 +279,9 @@ Output: True
276279
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
277280
```
278281

279-
题目描述:判断一个数组能不能只修改一个数就成为非递减数组
282+
题目描述:判断一个数组是否能只修改一个数就成为非递减数组
280283

281-
在出现 nums[i] < nums[i - 1] 时,需要考虑的是应该修改数组的哪个数,使得本次修改能使 i 之前的数组成为非递减数组,并且 **不影响后续的操作** 。优先考虑令 nums[i - 1] = nums[i],因为如果修改 nums[i] = nums[i - 1] 的话,那么 nums[i] 这个数会变大,就有可能比 nums[i + 1] 大,从而影响了后续操作。还有一个比较特别的情况就是 nums[i] < nums[i - 2]只修改 nums[i - 1] = nums[i] 不能使数组成为非递减数组,只能修改 nums[i] = nums[i - 1]
284+
在出现 nums[i] < nums[i - 1] 时,需要考虑的是应该修改数组的哪个数,使得本次修改能使 i 之前的数组成为非递减数组,并且 **不影响后续的操作** 。优先考虑令 nums[i - 1] = nums[i],因为如果修改 nums[i] = nums[i - 1] 的话,那么 nums[i] 这个数会变大,就有可能比 nums[i + 1] 大,从而影响了后续操作。还有一个比较特别的情况就是 nums[i] < nums[i - 2]修改 nums[i - 1] = nums[i] 不能使数组成为非递减数组,只能修改 nums[i] = nums[i - 1]
282285

283286
```java
284287
public boolean checkPossibility(int[] nums) {
@@ -298,27 +301,9 @@ public boolean checkPossibility(int[] nums) {
298301
}
299302
```
300303

301-
# 股票的最大收益
302-
303-
[122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
304304

305-
题目描述:一次股票交易包含买入和卖出,多个交易之间不能交叉进行。
306305

307-
对于 [a, b, c, d],如果有 a <= b <= c <= d ,那么最大收益为 d - a。而 d - a = (d - c) + (c - b) + (b - a) ,因此当访问到一个 prices[i] 且 prices[i] - prices[i-1] > 0,那么就把 prices[i] - prices[i-1] 添加到收益中,从而在局部最优的情况下也保证全局最优。
308-
309-
```java
310-
public int maxProfit(int[] prices) {
311-
int profit = 0;
312-
for (int i = 1; i < prices.length; i++) {
313-
if (prices[i] > prices[i - 1]) {
314-
profit += (prices[i] - prices[i - 1]);
315-
}
316-
}
317-
return profit;
318-
}
319-
```
320-
321-
# 子数组最大的和
306+
# 9. 子数组最大的和
322307

323308
[53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/)
324309

@@ -342,28 +327,45 @@ public int maxSubArray(int[] nums) {
342327
}
343328
```
344329

345-
# 买入和售出股票最大的收益
330+
# 10. 分隔字符串使同种字符出现在一起
346331

347-
[121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
348-
349-
题目描述:只进行一次交易。
332+
[763. Partition Labels (Medium)](https://leetcode.com/problems/partition-labels/description/)
350333

351-
只要记录前面的最小价格,将这个最小价格作为买入价格,然后将当前的价格作为售出价格,查看当前收益是不是最大收益。
334+
```html
335+
Input: S = "ababcbacadefegdehijhklij"
336+
Output: [9,7,8]
337+
Explanation:
338+
The partition is "ababcbaca", "defegde", "hijhklij".
339+
This is a partition so that each letter appears in at most one part.
340+
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits S into less parts.
341+
```
352342

353343
```java
354-
public int maxProfit(int[] prices) {
355-
int n = prices.length;
356-
if (n == 0) return 0;
357-
int soFarMin = prices[0];
358-
int max = 0;
359-
for (int i = 1; i < n; i++) {
360-
if (soFarMin > prices[i]) soFarMin = prices[i];
361-
else max = Math.max(max, prices[i] - soFarMin);
344+
public List<Integer> partitionLabels(String S) {
345+
int[] lastIndexsOfChar = new int[26];
346+
for (int i = 0; i < S.length(); i++) {
347+
lastIndexsOfChar[char2Index(S.charAt(i))] = i;
362348
}
363-
return max;
349+
List<Integer> partitions = new ArrayList<>();
350+
int firstIndex = 0;
351+
while (firstIndex < S.length()) {
352+
int lastIndex = firstIndex;
353+
for (int i = firstIndex; i < S.length() && i <= lastIndex; i++) {
354+
int index = lastIndexsOfChar[char2Index(S.charAt(i))];
355+
if (index > lastIndex) {
356+
lastIndex = index;
357+
}
358+
}
359+
partitions.add(lastIndex - firstIndex + 1);
360+
firstIndex = lastIndex + 1;
361+
}
362+
return partitions;
364363
}
365-
```
366364

365+
private int char2Index(char c) {
366+
return c - 'a';
367+
}
368+
```
367369

368370

369371

0 commit comments

Comments
 (0)