Leetcode 题解 - 贪心思想.md 12.0 KB
Newer Older
C
CyC2018 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<!-- GFM-TOC -->
* [分配饼干](#分配饼干)
* [不重叠的区间个数](#不重叠的区间个数)
* [投飞镖刺破气球](#投飞镖刺破气球)
* [根据身高和序号重组队列](#根据身高和序号重组队列)
* [分隔字符串使同种字符出现在一起](#分隔字符串使同种字符出现在一起)
* [种植花朵](#种植花朵)
* [判断是否为子序列](#判断是否为子序列)
* [修改一个数成为非递减数组](#修改一个数成为非递减数组)
* [股票的最大收益](#股票的最大收益)
* [子数组最大的和](#子数组最大的和)
* [买入和售出股票最大的收益](#买入和售出股票最大的收益)
<!-- GFM-TOC -->


C
CyC2018 已提交
16 17
保证每次操作都是局部最优的,并且最后得到的结果是全局最优的。

C
CyC2018 已提交
18
# 分配饼干
C
CyC2018 已提交
19

C
CyC2018 已提交
20
[455. Assign Cookies (Easy)](https://leetcode.com/problems/assign-cookies/description/)
C
CyC2018 已提交
21 22

```html
C
CyC2018 已提交
23 24
Input: [1,2], [1,2,3]
Output: 2
C
CyC2018 已提交
25

C
CyC2018 已提交
26 27 28
Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
You have 3 cookies and their sizes are big enough to gratify all of the children,
You need to output 2.
C
CyC2018 已提交
29 30 31 32 33 34
```

题目描述:每个孩子都有一个满足度,每个饼干都有一个大小,只有饼干的大小大于等于一个孩子的满足度,该孩子才会获得满足。求解最多可以获得满足的孩子数量。

给一个孩子的饼干应当尽量小又能满足该孩子,这样大饼干就能拿来给满足度比较大的孩子。因为最小的孩子最容易得到满足,所以先满足最小的孩子。

C
CyC2018 已提交
35
证明:假设在某次选择中,贪心策略选择给当前满足度最小的孩子分配第 m 个饼干,第 m 个饼干为可以满足该孩子的最小饼干。假设存在一种最优策略,给该孩子分配第 n 个饼干,并且 m < n。我们可以发现,经过这一轮分配,贪心策略分配后剩下的饼干一定有一个比最优策略来得大。因此在后续的分配中,贪心策略一定能满足更多的孩子。也就是说不存在比贪心策略更优的策略,即贪心策略就是最优策略。
C
CyC2018 已提交
36 37

```java
C
CyC2018 已提交
38 39 40 41 42 43 44 45 46 47 48
public int findContentChildren(int[] g, int[] s) {
    Arrays.sort(g);
    Arrays.sort(s);
    int gi = 0, si = 0;
    while (gi < g.length && si < s.length) {
        if (g[gi] <= s[si]) {
            gi++;
        }
        si++;
    }
    return gi;
C
CyC2018 已提交
49 50 51
}
```

C
CyC2018 已提交
52
# 不重叠的区间个数
C
CyC2018 已提交
53

C
CyC2018 已提交
54
[435. Non-overlapping Intervals (Medium)](https://leetcode.com/problems/non-overlapping-intervals/description/)
C
CyC2018 已提交
55 56

```html
C
CyC2018 已提交
57
Input: [ [1,2], [1,2], [1,2] ]
C
CyC2018 已提交
58

C
CyC2018 已提交
59
Output: 2
C
CyC2018 已提交
60

C
CyC2018 已提交
61
Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping.
C
CyC2018 已提交
62 63 64
```

```html
C
CyC2018 已提交
65
Input: [ [1,2], [2,3] ]
C
CyC2018 已提交
66

C
CyC2018 已提交
67
Output: 0
C
CyC2018 已提交
68

C
CyC2018 已提交
69
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
C
CyC2018 已提交
70 71 72 73 74 75 76 77 78 79 80
```

题目描述:计算让一组区间不重叠所需要移除的区间个数。

先计算最多能组成的不重叠区间个数,然后用区间总个数减去不重叠区间的个数。

在每次选择中,区间的结尾最为重要,选择的区间结尾越小,留给后面的区间的空间越大,那么后面能够选择的区间个数也就越大。

按区间的结尾进行排序,每次选择结尾最小,并且和前一个区间不重叠的区间。

```java
C
CyC2018 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
public int eraseOverlapIntervals(Interval[] intervals) {
    if (intervals.length == 0) {
        return 0;
    }
    Arrays.sort(intervals, Comparator.comparingInt(o -> o.end));
    int cnt = 1;
    int end = intervals[0].end;
    for (int i = 1; i < intervals.length; i++) {
        if (intervals[i].start < end) {
            continue;
        }
        end = intervals[i].end;
        cnt++;
    }
    return intervals.length - cnt;
C
CyC2018 已提交
96 97 98
}
```

C
CyC2018 已提交
99
使用 lambda 表示式创建 Comparator 会导致算法运行时间过长,如果注重运行时间,可以修改为普通创建 Comparator 语句:
C
CyC2018 已提交
100 101

```java
C
CyC2018 已提交
102 103 104 105 106
Arrays.sort(intervals, new Comparator<Interval>() {
    @Override
    public int compare(Interval o1, Interval o2) {
        return o1.end - o2.end;
    }
C
CyC2018 已提交
107 108 109
});
```

C
CyC2018 已提交
110
# 投飞镖刺破气球
C
CyC2018 已提交
111

C
CyC2018 已提交
112
[452. Minimum Number of Arrows to Burst Balloons (Medium)](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
C
CyC2018 已提交
113 114 115

```
Input:
C
CyC2018 已提交
116
[[10,16], [2,8], [1,6], [7,12]]
C
CyC2018 已提交
117 118 119 120 121 122 123

Output:
2
```

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

C
CyC2018 已提交
124
也是计算不重叠的区间个数,不过和 Non-overlapping Intervals 的区别在于,[1, 2] 和 [2, 3] 在本题中算是重叠区间。
C
CyC2018 已提交
125 126

```java
C
CyC2018 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140
public int findMinArrowShots(int[][] points) {
    if (points.length == 0) {
        return 0;
    }
    Arrays.sort(points, Comparator.comparingInt(o -> o[1]));
    int cnt = 1, end = points[0][1];
    for (int i = 1; i < points.length; i++) {
        if (points[i][0] <= end) {
            continue;
        }
        cnt++;
        end = points[i][1];
    }
    return cnt;
C
CyC2018 已提交
141 142 143
}
```

C
CyC2018 已提交
144
# 根据身高和序号重组队列
C
CyC2018 已提交
145

C
CyC2018 已提交
146
[406. Queue Reconstruction by Height(Medium)](https://leetcode.com/problems/queue-reconstruction-by-height/description/)
C
CyC2018 已提交
147 148 149

```html
Input:
C
CyC2018 已提交
150
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
C
CyC2018 已提交
151 152

Output:
C
CyC2018 已提交
153
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
C
CyC2018 已提交
154 155
```

C
CyC2018 已提交
156
题目描述:一个学生用两个分量 (h, k) 描述,h 表示身高,k 表示排在前面的有 k 个学生的身高比他高或者和他一样高。
C
CyC2018 已提交
157

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

C
CyC2018 已提交
160
身高降序、k 值升序,然后按排好序的顺序插入队列的第 k 个位置中。
C
CyC2018 已提交
161 162

```java
C
CyC2018 已提交
163 164 165 166 167 168 169 170 171 172
public int[][] reconstructQueue(int[][] people) {
    if (people == null || people.length == 0 || people[0].length == 0) {
        return new int[0][0];
    }
    Arrays.sort(people, (a, b) -> (a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]));
    List<int[]> queue = new ArrayList<>();
    for (int[] p : people) {
        queue.add(p[1], p);
    }
    return queue.toArray(new int[queue.size()][]);
C
CyC2018 已提交
173 174 175
}
```

C
CyC2018 已提交
176
# 分隔字符串使同种字符出现在一起
C
CyC2018 已提交
177

C
CyC2018 已提交
178
[763. Partition Labels (Medium)](https://leetcode.com/problems/partition-labels/description/)
C
CyC2018 已提交
179 180

```html
C
CyC2018 已提交
181 182
Input: S = "ababcbacadefegdehijhklij"
Output: [9,7,8]
C
CyC2018 已提交
183
Explanation:
C
CyC2018 已提交
184 185 186
The partition is "ababcbaca", "defegde", "hijhklij".
This is a partition so that each letter appears in at most one part.
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits S into less parts.
C
CyC2018 已提交
187 188 189
```

```java
C
CyC2018 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
public List<Integer> partitionLabels(String S) {
    int[] lastIndexsOfChar = new int[26];
    for (int i = 0; i < S.length(); i++) {
        lastIndexsOfChar[char2Index(S.charAt(i))] = i;
    }
    List<Integer> partitions = new ArrayList<>();
    int firstIndex = 0;
    while (firstIndex < S.length()) {
        int lastIndex = firstIndex;
        for (int i = firstIndex; i < S.length() && i <= lastIndex; i++) {
            int index = lastIndexsOfChar[char2Index(S.charAt(i))];
            if (index > lastIndex) {
                lastIndex = index;
            }
        }
        partitions.add(lastIndex - firstIndex + 1);
        firstIndex = lastIndex + 1;
    }
    return partitions;
C
CyC2018 已提交
209 210
}

C
CyC2018 已提交
211 212
private int char2Index(char c) {
    return c - 'a';
C
CyC2018 已提交
213 214 215 216
}
```


C
CyC2018 已提交
217
# 种植花朵
C
CyC2018 已提交
218

C
CyC2018 已提交
219
[605. Can Place Flowers (Easy)](https://leetcode.com/problems/can-place-flowers/description/)
C
CyC2018 已提交
220 221

```html
C
CyC2018 已提交
222 223
Input: flowerbed = [1,0,0,0,1], n = 1
Output: True
C
CyC2018 已提交
224 225
```

C
CyC2018 已提交
226
题目描述:花朵之间至少需要一个单位的间隔,求解是否能种下 n 朵花。
C
CyC2018 已提交
227 228

```java
C
CyC2018 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
public boolean canPlaceFlowers(int[] flowerbed, int n) {
    int len = flowerbed.length;
    int cnt = 0;
    for (int i = 0; i < len && cnt < n; i++) {
        if (flowerbed[i] == 1) {
            continue;
        }
        int pre = i == 0 ? 0 : flowerbed[i - 1];
        int next = i == len - 1 ? 0 : flowerbed[i + 1];
        if (pre == 0 && next == 0) {
            cnt++;
            flowerbed[i] = 1;
        }
    }
    return cnt >= n;
C
CyC2018 已提交
244 245 246
}
```

C
CyC2018 已提交
247
# 判断是否为子序列
C
CyC2018 已提交
248

C
CyC2018 已提交
249
[392. Is Subsequence (Medium)](https://leetcode.com/problems/is-subsequence/description/)
C
CyC2018 已提交
250 251

```html
C
CyC2018 已提交
252 253
s = "abc", t = "ahbgdc"
Return true.
C
CyC2018 已提交
254 255 256
```

```java
C
CyC2018 已提交
257 258 259 260 261 262 263 264 265
public boolean isSubsequence(String s, String t) {
    int index = -1;
    for (char c : s.toCharArray()) {
        index = t.indexOf(c, index + 1);
        if (index == -1) {
            return false;
        }
    }
    return true;
C
CyC2018 已提交
266 267 268
}
```

C
CyC2018 已提交
269
# 修改一个数成为非递减数组
C
CyC2018 已提交
270

C
CyC2018 已提交
271
[665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/)
C
CyC2018 已提交
272 273

```html
C
CyC2018 已提交
274 275 276
Input: [4,2,3]
Output: True
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
C
CyC2018 已提交
277 278 279 280
```

题目描述:判断一个数组能不能只修改一个数就成为非递减数组。

C
CyC2018 已提交
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]。
C
CyC2018 已提交
282 283

```java
C
CyC2018 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297
public boolean checkPossibility(int[] nums) {
    int cnt = 0;
    for (int i = 1; i < nums.length && cnt < 2; i++) {
        if (nums[i] >= nums[i - 1]) {
            continue;
        }
        cnt++;
        if (i - 2 >= 0 && nums[i - 2] > nums[i]) {
            nums[i] = nums[i - 1];
        } else {
            nums[i - 1] = nums[i];
        }
    }
    return cnt <= 1;
C
CyC2018 已提交
298 299 300
}
```

C
CyC2018 已提交
301
# 股票的最大收益
C
CyC2018 已提交
302

C
CyC2018 已提交
303
[122. Best Time to Buy and Sell Stock II (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
C
CyC2018 已提交
304 305 306

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

C
CyC2018 已提交
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] 添加到收益中,从而在局部最优的情况下也保证全局最优。
C
CyC2018 已提交
308 309

```java
C
CyC2018 已提交
310 311 312 313 314 315 316 317
public int maxProfit(int[] prices) {
    int profit = 0;
    for (int i = 1; i < prices.length; i++) {
        if (prices[i] > prices[i - 1]) {
            profit += (prices[i] - prices[i - 1]);
        }
    }
    return profit;
C
CyC2018 已提交
318 319 320
}
```

C
CyC2018 已提交
321
# 子数组最大的和
C
CyC2018 已提交
322

C
CyC2018 已提交
323
[53. Maximum Subarray (Easy)](https://leetcode.com/problems/maximum-subarray/description/)
C
CyC2018 已提交
324 325

```html
C
CyC2018 已提交
326 327
For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray [4,-1,2,1] has the largest sum = 6.
C
CyC2018 已提交
328 329 330
```

```java
C
CyC2018 已提交
331 332 333 334 335 336 337 338 339 340 341
public int maxSubArray(int[] nums) {
    if (nums == null || nums.length == 0) {
        return 0;
    }
    int preSum = nums[0];
    int maxSum = preSum;
    for (int i = 1; i < nums.length; i++) {
        preSum = preSum > 0 ? preSum + nums[i] : nums[i];
        maxSum = Math.max(maxSum, preSum);
    }
    return maxSum;
C
CyC2018 已提交
342 343 344
}
```

C
CyC2018 已提交
345
# 买入和售出股票最大的收益
C
CyC2018 已提交
346

C
CyC2018 已提交
347
[121. Best Time to Buy and Sell Stock (Easy)](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
C
CyC2018 已提交
348 349 350 351 352 353

题目描述:只进行一次交易。

只要记录前面的最小价格,将这个最小价格作为买入价格,然后将当前的价格作为售出价格,查看当前收益是不是最大收益。

```java
C
CyC2018 已提交
354 355 356 357 358 359 360 361 362 363
public int maxProfit(int[] prices) {
    int n = prices.length;
    if (n == 0) return 0;
    int soFarMin = prices[0];
    int max = 0;
    for (int i = 1; i < n; i++) {
        if (soFarMin > prices[i]) soFarMin = prices[i];
        else max = Math.max(max, prices[i] - soFarMin);
    }
    return max;
C
CyC2018 已提交
364 365 366
}
```

C
CyC2018 已提交
367 368 369 370




C
CyC2018 已提交
371
</br><div align="center">公众号 CyC2018,专注于核心基础知识分享、求职指导、技术成长。在公众号后台回复 ziliao 可领取复习大纲,帮你理清复习重点。</div></br>
C
CyC2018 已提交
372
<div align="center"><img width="180px" src="https://cyc-1256109796.cos.ap-guangzhou.myqcloud.com/%E5%85%AC%E4%BC%97%E5%8F%B7.jpg"></img></div>