Skip to content

Commit 52bc948

Browse files
committed
✨update: Modify articles
1 parent 8445b3b commit 52bc948

15 files changed

+138
-33
lines changed

Index/堆.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
| [726. 原子的数量](https://leetcode-cn.com/problems/number-of-atoms/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-atoms/solution/gong-shui-san-xie-shi-yong-xiao-ji-qiao-l5ak4/) | 困难 | 🤩🤩🤩🤩 |
1717
| [786. 第 K 个最小的素数分数](https://leetcode-cn.com/problems/k-th-smallest-prime-fraction/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/k-th-smallest-prime-fraction/solution/gong-shui-san-xie-yi-ti-shuang-jie-you-x-8ymk/) | 中等 | 🤩🤩🤩 |
1818
| [846. 一手顺子](https://leetcode-cn.com/problems/hand-of-straights/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/hand-of-straights/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-4hxw/) | 中等 | 🤩🤩🤩 |
19+
| [871. 最低加油次数](https://leetcode.cn/problems/minimum-number-of-refueling-stops/) | [LeetCode 题解链接](https://leetcode.cn/problems/minimum-number-of-refueling-stops/solution/by-ac_oier-q2mk/) | 困难 | 🤩🤩🤩🤩🤩 |
1920
| [954. 二倍数对数组](https://leetcode-cn.com/problems/array-of-doubled-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/array-of-doubled-pairs/solution/by-ac_oier-d1z7/) | 中等 | 🤩🤩🤩🤩 |
2021
| [987. 二叉树的垂序遍历](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-h-wfm3/) | 困难 | 🤩🤩🤩🤩🤩 |
2122
| [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/) | 简单 | 🤩🤩🤩🤩 |

Index/贪心算法.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
| [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/) | 困难 | 🤩🤩🤩 |
1818
| [781. 森林中的兔子](https://leetcode-cn.com/problems/rabbits-in-forest/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/rabbits-in-forest/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-v17p5/) | 中等 | 🤩🤩🤩🤩 |
1919
| [807. 保持城市天际线](https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/solution/gong-shui-san-xie-jian-dan-tan-xin-yun-y-2f47/) | 中等 | 🤩🤩🤩🤩 |
20+
| [871. 最低加油次数](https://leetcode.cn/problems/minimum-number-of-refueling-stops/) | [LeetCode 题解链接](https://leetcode.cn/problems/minimum-number-of-refueling-stops/solution/by-ac_oier-q2mk/) | 困难 | 🤩🤩🤩🤩🤩 |
2021
| [881. 救生艇](https://leetcode-cn.com/problems/boats-to-save-people/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/boats-to-save-people/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-hosg8/) | 中等 | 🤩🤩🤩🤩 |
2122
| [926. 将字符串翻转到单调递增](https://leetcode.cn/problems/flip-string-to-monotone-increasing/) | [LeetCode 题解链接](https://leetcode.cn/problems/flip-string-to-monotone-increasing/solution/by-ac_oier-h0we/) | 中等 | 🤩🤩🤩🤩 |
2223
| [942. 增减字符串匹配](https://leetcode.cn/problems/di-string-match/) | [LeetCode 题解链接](https://leetcode.cn/problems/di-string-match/solution/by-ac_oier-pvjk/) | 简单 | 🤩🤩🤩🤩🤩 |

LeetCode/1601-1610/1606. 找到处理最多请求的服务器(困难).md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
这是 LeetCode 上的 **[1606. 找到处理最多请求的服务器](https://leetcode-cn.com/problems/find-servers-that-handled-most-number-of-requests/solution/by-ac_oier-zgm6/)** ,难度为 **困难**
44

5-
Tag : 「数据结构」、「优先队列」、「堆」、「红黑树」、「二分」
5+
Tag : 「数据结构」、「优先队列(堆)」、「红黑树」、「二分」
66

77

88

LeetCode/1701-1710/1705. 吃苹果的最大数目(中等).md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
这是 LeetCode 上的 **[1705. 吃苹果的最大数目](https://leetcode-cn.com/problems/maximum-number-of-eaten-apples/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-hfdy0/)** ,难度为 **中等**
44

5-
Tag : 「贪心」、「优先队列」、「堆
5+
Tag : 「贪心」、「优先队列(堆)
66

77

88

LeetCode/1781-1790/1786. 从第一个节点出发到最后一个节点的受限路径数(中等).md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
这是 LeetCode 上的 **[1786. 从第一个节点出发到最后一个节点的受限路径数](https://leetcode-cn.com/problems/number-of-restricted-paths-from-first-to-last-node/solution/xiang-jie-dui-you-hua-dijkstra-dong-tai-i6j0d/)** ,难度为 **中等**
44

5-
Tag : 「图论最短路」、「线性 DP」
5+
Tag : 「最短路」、「线性 DP」
66

77

88

LeetCode/21-30/23. 合并K个升序链表(困难).md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
这是 LeetCode 上的 **[23. 合并K个升序链表](https://leetcode-cn.com/problems/merge-k-sorted-lists/solution/shua-chuan-lc-you-xian-dui-lie-jie-fa-sh-3flb/)** ,难度为 **中等**
44

5-
Tag : 「优先队列」、「堆」、「链表」
5+
Tag : 「优先队列(堆)」、「链表」
66

77

88

LeetCode/261-270/264. 丑数 II(中等).md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
这是 LeetCode 上的 **[264. 丑数 II](https://leetcode-cn.com/problems/ugly-number-ii/solution/gong-shui-san-xie-yi-ti-shuang-jie-you-x-3nvs/)** ,难度为 **中等**
44

5-
Tag : 「多路归并」、「堆」、「优先队列」
5+
Tag : 「多路归并」、「优先队列(堆)
66

77

88

LeetCode/291-300/295. 数据流的中位数(困难).md

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
这是 LeetCode 上的 **[295. 数据流的中位数](https://leetcode-cn.com/problems/find-median-from-data-stream/solution/gong-shui-san-xie-jing-dian-shu-ju-jie-g-pqy8/)** ,难度为 **困难**
44

5-
Tag : 「优先队列」、「堆
5+
Tag : 「优先队列(堆)
66

77

88

99
中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。
1010

1111
例如,
1212

13-
[2,3,4] 的中位数是 3
14-
15-
[2,3] 的中位数是 (2 + 3) / 2 = 2.5
13+
* `[2,3,4]` 的中位数是 $3$
14+
* `[2,3]` 的中位数是 $(2 + 3) / 2 = 2.5$
1615

1716
设计一个支持以下两种操作的数据结构:
18-
* void addNum(int num) - 从数据流中添加一个整数到数据结构中。
19-
* double findMedian() - 返回目前所有元素的中位数。
17+
* `void addNum(int num)` - 从数据流中添加一个整数到数据结构中。
18+
* `double findMedian()` - 返回目前所有元素的中位数。
19+
2020
示例:
2121
```
2222
addNum(1)
@@ -27,8 +27,8 @@ findMedian() -> 2
2727
```
2828

2929
进阶:
30-
* 如果数据流中所有整数都在 0 到 100 范围内,你将如何优化你的算法?
31-
* 如果数据流中 99% 的整数都在 0 到 100 范围内,你将如何优化你的算法?
30+
* 如果数据流中所有整数都在 $0$$100$ 范围内,你将如何优化你的算法?
31+
* 如果数据流中 `99%` 的整数都在 $0$$100$ 范围内,你将如何优化你的算法?
3232

3333
---
3434

LeetCode/401-410/407. 接雨水 II(困难).md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
这是 LeetCode 上的 **[407. 接雨水 II](https://leetcode-cn.com/problems/trapping-rain-water-ii/solution/gong-shui-san-xie-jing-dian-dijkstra-yun-13ik/)** ,难度为 **困难**
44

5-
Tag : 「图论最短路」、「优先队列(堆)」
5+
Tag : 「最短路」、「优先队列(堆)」
66

77

88

LeetCode/471-480/480. 滑动窗口中位数(困难).md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
这是 LeetCode 上的 **[480. 滑动窗口中位数](https://leetcode-cn.com/problems/sliding-window-median/solution/xiang-jie-po-su-jie-fa-you-xian-dui-lie-mo397/)** ,难度为 **困难**
44

5-
Tag : 「滑动窗口」、「堆」、「优先队列」
5+
Tag : 「滑动窗口」、「堆」、「优先队列(堆)」
6+
7+
68

79
中位数是有序序列最中间的那个数。如果序列的长度是偶数,则没有最中间的数;此时中位数是最中间的两个数的平均数。
810

LeetCode/501-510/502. IPO(困难).md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
这是 LeetCode 上的 **[502. IPO](https://leetcode-cn.com/problems/next-greater-element-ii/solution/cong-po-su-jie-fa-de-jiao-du-qu-li-jie-d-trht/)** ,难度为 **困难**
44

5-
Tag : 「贪心」、「优先队列」、「堆
5+
Tag : 「贪心」、「优先队列(堆)
66

77

88

LeetCode/701-710/703. 数据流中的第 K 大元素(简单).md

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
这是 LeetCode 上的 **[703. 数据流中的第 K 大元素](https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/solution/jian-da-ti-de-duo-chong-jie-fa-mou-pao-p-d1qi/)** ,难度为 **简单**
44

5-
Tag : 「Top K」、「排序」、「堆」、「优先队列」
5+
Tag : 「Top K」、「排序」、「优先队列(堆)
66

77

88

9-
设计一个找到数据流中第 k 大元素的类(class)。注意是排序后的第 k 大元素,不是第 k 个不同的元素。
9+
设计一个找到数据流中第 `k` 大元素的类(class)。注意是排序后的第 `k` 大元素,不是第 `k` 个不同的元素。
1010

11-
请实现 KthLargest 类:
12-
* KthLargest(int k, int[] nums) 使用整数 k 和整数流 nums 初始化对象。
13-
* int add(int val) 将 val 插入数据流 nums 后,返回当前数据流中第 k 大的元素。
11+
请实现 `KthLargest` 类:
12+
* `KthLargest(int k, int[] nums)` 使用整数 `k` 和整数流 `nums` 初始化对象。
13+
* `int add(int val)``val` 插入数据流 `nums` 后,返回当前数据流中第 `k` 大的元素。
1414

1515

1616
示例:
@@ -31,12 +31,12 @@ kthLargest.add(4); // return 8
3131
```
3232

3333
提示:
34-
* 1 <= k <= $10^4$
35-
* 0 <= nums.length <= $10^4$
36-
* -$10^4$ <= nums[i] <= $10^4$
37-
* -$10^4$ <= val <= $10^4$
38-
* 最多调用 add 方法 $10^4$ 次
39-
* 题目数据保证,在查找第 k 大元素时,数组中至少有 k 个元素
34+
* $1 <= k <= 10^4$
35+
* $0 <= nums.length <= 10^4$
36+
* $-10^4 <= nums[i] <= 10^4$
37+
* $-10^4 <= val <= 10^4$
38+
* 最多调用 `add` 方法 $10^4$ 次
39+
* 题目数据保证,在查找第 `k` 大元素时,数组中至少有 `k` 个元素
4040

4141
---
4242

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[871. 最低加油次数](https://leetcode.cn/problems/minimum-number-of-refueling-stops/solution/by-ac_oier-q2mk/)** ,难度为 **困难**
4+
5+
Tag : 「贪心」、「优先队列(堆)」、「模拟」
6+
7+
8+
9+
汽车从起点出发驶向目的地,该目的地位于出发位置东面 `target` 英里处。
10+
11+
沿途有加油站,每个 `station[i]` 代表一个加油站,它位于出发位置东面 `station[i][0]` 英里处,并且有 `station[i][1]` 升汽油。
12+
13+
假设汽车油箱的容量是无限的,其中最初有 `startFuel` 升燃料。它每行驶 $1$ 英里就会用掉 $1$ 升汽油。
14+
15+
当汽车到达加油站时,它可能停下来加油,将所有汽油从加油站转移到汽车中。
16+
17+
为了到达目的地,汽车所必要的最低加油次数是多少?如果无法到达目的地,则返回 $-1$ 。
18+
19+
注意:如果汽车到达加油站时剩余燃料为 $0$,它仍然可以在那里加油。如果汽车到达目的地时剩余燃料为 $0$,仍然认为它已经到达目的地。
20+
21+
示例 1:
22+
```
23+
输入:target = 1, startFuel = 1, stations = []
24+
25+
输出:0
26+
27+
解释:我们可以在不加油的情况下到达目的地。
28+
```
29+
示例 2:
30+
```
31+
输入:target = 100, startFuel = 1, stations = [[10,100]]
32+
33+
输出:-1
34+
35+
解释:我们无法抵达目的地,甚至无法到达第一个加油站。
36+
```
37+
示例 3:
38+
```
39+
输入:target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]
40+
41+
输出:2
42+
43+
解释:
44+
我们出发时有 10 升燃料。
45+
我们开车来到距起点 10 英里处的加油站,消耗 10 升燃料。将汽油从 0 升加到 60 升。
46+
然后,我们从 10 英里处的加油站开到 60 英里处的加油站(消耗 50 升燃料),
47+
并将汽油从 10 升加到 50 升。然后我们开车抵达目的地。
48+
我们沿途在1两个加油站停靠,所以返回 2 。
49+
```
50+
51+
提示:
52+
* $1 <= target, startFuel, stations[i][1] <= 10^9$
53+
* $0 <= stations.length <= 500$
54+
* $0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target$
55+
56+
---
57+
58+
### 贪心 + 优先队列(堆)
59+
60+
为了方便,我们记 `target``t`,记 `startFuel``start`,记 `stations``ss`
61+
62+
我们可以模拟行进过程,使用 `n` 代表加油站总个数,`idx` 代表经过的加油站下标,使用 `remain` 代表当前有多少油(起始有 `remain = start`),`loc` 代表走了多远,`ans` 代表答案(至少需要的加油次数)。
63+
64+
只要 `loc < t`,代表还没到达(经过)目标位置,我们可以继续模拟行进过程,每次将 `remain` 累加到 `loc` 中,含义为使用完剩余的油量,可以去到的最远距离,同时将所在位置 `ss[idx][0] <= loc` 的加油站数量加入优先队列(大根堆,根据油量排倒序)中,再次检查是否满足 `loc < t`(下次循环),此时由于清空了剩余油量 `remain`,我们尝试从优先队列(大根堆)中取出过往油量最大的加油站并进行加油(同时对加油次数 `ans` 进行加一操作),使用新的剩余油量 `remain` 重复上述过程,直到满足 `loc >= t` 或无油可加。
65+
66+
容易证明该做法的正确性:同样是消耗一次加油次数,始终选择油量最大的加油站进行加油,可以确保不存在更优的结果。
67+
68+
代码:
69+
```Java
70+
class Solution {
71+
public int minRefuelStops(int t, int start, int[][] ss) {
72+
PriorityQueue<Integer> q = new PriorityQueue<>((a,b)->b-a);
73+
int n = ss.length, idx = 0;
74+
int remain = start, loc = 0, ans = 0;
75+
while (loc < t) {
76+
if (remain == 0) {
77+
if (!q.isEmpty() && ++ans >= 0) remain += q.poll();
78+
else return -1;
79+
}
80+
loc += remain; remain = 0;
81+
while (idx < n && ss[idx][0] <= loc) q.add(ss[idx++][1]);
82+
}
83+
return ans;
84+
}
85+
}
86+
```
87+
* 时间复杂度:$O(n\log{n})$
88+
* 空间复杂度:$O(n)$
89+
90+
---
91+
92+
### 最后
93+
94+
这是我们「刷穿 LeetCode」系列文章的第 `No.871` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
95+
96+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
97+
98+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
99+
100+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
101+

LeetCode/951-960/954. 二倍数对数组(中等).md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
这是 LeetCode 上的 **[954. 二倍数对数组](https://leetcode-cn.com/problems/array-of-doubled-pairs/solution/by-ac_oier-d1z7/)** ,难度为 **中等**
44

5-
Tag : 「优先队列」、「堆」、「构造」、「哈希表」、「拓扑排序」
5+
Tag : 「优先队列(堆)」、「构造」、「哈希表」、「拓扑排序」
66

77

88

LeetCode/面试题/面试题 17.14. 最小K个数(中等).md

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
这是 LeetCode 上的 **[面试题 17.14. 最小K个数](https://leetcode-cn.com/problems/smallest-k-lcci/solution/gong-shui-san-xie-yi-ti-si-jie-you-xian-yy5k5/)** ,难度为 **中等**
44

5-
Tag : 「优先队列」、「」、「排序」
5+
Tag : 「优先队列」、「优先队列(堆)」、「排序」
66

77

88

9-
设计一个算法,找出数组中最小的k个数。以任意顺序返回这k个数均可。
9+
设计一个算法,找出数组中最小的 $k$ 个数。以任意顺序返回这k个数均可。
1010

1111
示例:
1212
```
@@ -16,8 +16,8 @@ Tag : 「优先队列」、「堆」、「排序」
1616
```
1717

1818
提示:
19-
* 0 <= len(arr) <= 100000
20-
* 0 <= k <= min(100000, len(arr))
19+
* $0 <= len(arr) <= 100000$
20+
* $0 <= k <= \min(100000, len(arr))$
2121

2222
---
2323

@@ -160,7 +160,7 @@ class Solution {
160160
}
161161
}
162162
```
163-
* 时间复杂度:由于每次都会随机选择基准值,因此每次递归的数组平均长度为 $n / 2$,那么划分数组操作的次数不会超过 $2 * n$。整体复杂度为 $O(n)$
163+
* 时间复杂度:由于每次都会随机选择基准值,因此每次递归的数组平均长度为 $n / 2$,那么划分数组操作的次数不会超过 $2 \times n$。整体复杂度为 $O(n)$
164164
* 空间复杂度:$O(\log{n})$
165165

166166
---

0 commit comments

Comments
 (0)