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

Appearance settings

Commit 0bb2b07

Browse filesBrowse files
committed
Add greedy algorithm
1 parent 3f05c3b commit 0bb2b07
Copy full SHA for 0bb2b07

File tree

Expand file treeCollapse file tree

3 files changed

+99
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+99
-0
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
> 此处整理了一些特殊情况下适用的算法
3131
32+
- [贪心算法](./advanced_algorithm/greedy.md)
3233
- [快速选择](./advanced_algorithm/quick_select.md)
3334
- [三向切分快速排序](./advanced_algorithm/three_way_quick_sort.md)
3435
- [二进制运算](./advanced_algorithm/binary_op.md)

‎SUMMARY.md

Copy file name to clipboardExpand all lines: SUMMARY.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
## 进阶算法
1818

19+
- [贪心算法](advanced_algorithm/greedy.md)
1920
- [快速选择](advanced_algorithm/quick_select.md)
2021
- [三向切分快速排序](advanced_algorithm/three_way_quick_sort.md)
2122
- [二进制运算](advanced_algorithm/binary_op.md)

‎advanced_algorithm/greedy.md

Copy file name to clipboard
+97Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# 贪心算法
2+
3+
贪心算法在每一步选择中都采取在当前状态下最好或最优的选择,从而使得结果也是最优解,适合去解决具有最优子结构的问题。
4+
5+
## 应用场景
6+
7+
最优子结构的特征:
8+
9+
- 每一次操作对结果直接产生影响
10+
- 不依赖于之前的操作
11+
- 子问题的最优解是全局最优解的一部分
12+
13+
## 常见例题
14+
15+
### 跳跃游戏
16+
17+
> [55. 跳跃游戏](https://leetcode-cn.com/problems/jump-game/)
18+
>
19+
> 给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。
20+
>
21+
> 数组中的每个元素代表你在该位置可以跳跃的最大长度。
22+
>
23+
> 判断你是否能够到达最后一个下标。
24+
25+
思路:每个格子`i`作为起跳点最远能跳到`nums[i]`处,所以遍历数组得到最远距离并与数组长度进行比较即可。
26+
27+
这题只需要判断能否到达终点而无需给出具体路径,所以不需要用回溯的方法。
28+
29+
```java
30+
public boolean canJump(int[] nums) {
31+
int maxLen = 0;
32+
for (int i = 0; i < nums.length; i++) {
33+
// 当前格子已经无法跳到
34+
if (i > maxLen) return false;
35+
// 更新能跳到的最远距离
36+
maxLen = Math.max(maxLen, i + nums[i]);
37+
}
38+
return true;
39+
}
40+
```
41+
42+
### 跳跃游戏 II
43+
44+
> [45. 跳跃游戏 II](https://leetcode-cn.com/problems/jump-game-ii/)
45+
>
46+
>给定一个非负整数数组,你最初位于数组的第一个位置。
47+
>
48+
>数组中的每个元素代表你在该位置可以跳跃的最大长度。
49+
>
50+
>你的目标是使用最少的跳跃次数到达数组的最后一个位置。
51+
52+
思路:确保每次跳跃选择的格子都有最远的跳跃范围。
53+
54+
```java
55+
public int jump(int[] nums) {
56+
int steps = 0;
57+
int start = 0;
58+
int end = 1;
59+
while (end < nums.length) {
60+
// 确定最远的跳跃范围
61+
int maxPosition = 0;
62+
for (int i = start; i < end; i++) {
63+
maxPosition = Math.max(maxPosition, i + nums[i]);
64+
}
65+
start = end;
66+
end = maxPosition + 1;
67+
// 步数增加
68+
steps++;
69+
}
70+
return steps;
71+
}
72+
```
73+
74+
### 分发饼干
75+
76+
> [455. 分发饼干](https://leetcode-cn.com/problems/assign-cookies/)
77+
>
78+
> 对每个孩子 `i`,都有一个胃口值 `g[i]`,这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 `j`,都有一个尺寸 `s[j]` 。如果 `s[j] >= g[i]`,我们可以将这个饼干 `j` 分配给孩子 `i` ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。
79+
80+
思路:先排序,再贪心,让饼干优先满足胃口更多的孩子
81+
82+
```java
83+
public int findContentChildren(int[] g, int[] s) {
84+
Arrays.sort(g);
85+
Arrays.sort(s);
86+
int p = 0;
87+
int q = 0;
88+
while (p < g.length && q < s.length) {
89+
if (g[p] <= s[q]) {
90+
p++;
91+
}
92+
q++;
93+
}
94+
return p;
95+
}
96+
```
97+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.