Skip to content

Commit 73de4ab

Browse files
Merge pull request SharingSource#524 from SharingSource/ac_oier
✨feat: Add 436、961
2 parents e46eb1d + e6807e1 commit 73de4ab

9 files changed

+258
-4
lines changed

Index/二分.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
| [367. 有效的完全平方数](https://leetcode-cn.com/problems/valid-perfect-square/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/valid-perfect-square/solution/gong-shui-san-xie-yi-ti-shuang-jie-er-fe-g5el/) | 简单 | 🤩🤩🤩🤩🤩 |
2323
| [373. 查找和最小的K对数字](https://leetcode-cn.com/problems/find-k-pairs-with-smallest-sums/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-k-pairs-with-smallest-sums/solution/gong-shui-san-xie-duo-lu-gui-bing-yun-yo-pgw5/) | 中等 | 🤩🤩🤩🤩🤩 |
2424
| [374. 猜数字大小](https://leetcode-cn.com/problems/guess-number-higher-or-lower/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/guess-number-higher-or-lower/solution/gong-shui-san-xie-shi-yong-jiao-hu-han-s-tocm/) | 简单 | 🤩🤩🤩 |
25+
| [436. 寻找右区间](https://leetcode.cn/problems/find-right-interval/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-right-interval/solution/by-ac_oier-sijp/) | 中等 | 🤩🤩🤩🤩 |
2526
| [441. 排列硬币](https://leetcode-cn.com/problems/arranging-coins/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/arranging-coins/solution/gong-shui-san-xie-yi-ti-shuang-jie-shu-x-sv9o/) | 简单 | 🤩🤩🤩 |
2627
| [449. 序列化和反序列化二叉搜索树](https://leetcode.cn/problems/serialize-and-deserialize-bst/) | [LeetCode 题解链接](https://leetcode.cn/problems/serialize-and-deserialize-bst/solution/by-ac_oier-ncwn/) | 中等 | 🤩🤩🤩🤩 |
2728
| [475. 供暖器](https://leetcode-cn.com/problems/heaters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/heaters/solution/gong-shui-san-xie-er-fen-shuang-zhi-zhen-mys4/) | 中等 | 🤩🤩🤩🤩 |

Index/双指针.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
| [395. 至少有 K 个重复字符的最长子串](https://leetcode-cn.com/problems/longest-substring-with-at-least-k-repeating-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-substring-with-at-least-k-repeating-characters/solution/xiang-jie-mei-ju-shuang-zhi-zhen-jie-fa-50ri1/) | 中等 | 🤩🤩🤩 |
1515
| [413. 等差数列划分](https://leetcode-cn.com/problems/arithmetic-slices/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/arithmetic-slices/solution/gong-shui-san-xie-shuang-zhi-zhen-qiu-ji-ef1q/) | 中等 | 🤩🤩🤩🤩 |
1616
| [424. 替换后的最长重复字符](https://leetcode-cn.com/problems/longest-repeating-character-replacement/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-repeating-character-replacement/solution/ping-ping-wu-qi-shuang-zhi-zhen-da-bai-h-fgif/) | 中等 | 🤩🤩🤩🤩 |
17+
| [436. 寻找右区间](https://leetcode.cn/problems/find-right-interval/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-right-interval/solution/by-ac_oier-sijp/) | 中等 | 🤩🤩🤩🤩 |
1718
| [438. 找到字符串中所有字母异位词](https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/solution/gong-shui-san-xie-shuang-zhi-zhen-shi-xi-t5hc/) | 中等 | 🤩🤩🤩🤩 |
1819
| [443. 压缩字符串](https://leetcode-cn.com/problems/string-compression/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/string-compression/solution/gong-shui-san-xie-shuang-zhi-zhen-yuan-d-bppu/) | 中等 | 🤩🤩🤩🤩 |
1920
| [475. 供暖器](https://leetcode-cn.com/problems/heaters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/heaters/solution/gong-shui-san-xie-er-fen-shuang-zhi-zhen-mys4/) | 中等 | 🤩🤩🤩🤩 |

Index/哈希表.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
| [888. 公平的糖果棒交换](https://leetcode-cn.com/problems/fair-candy-swap/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/fair-candy-swap/solution/gong-shui-san-xie-yi-ti-shuang-jie-po-su-uant/) | 简单 | 🤩🤩 |
4545
| [930. 和相同的二元子数组](https://leetcode-cn.com/problems/binary-subarrays-with-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-subarrays-with-sum/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-hfoc0/) | 中等 | 🤩🤩🤩 |
4646
| [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/) | 中等 | 🤩🤩🤩 |
47+
| [961. 在长度 2N 的数组中找出重复 N 次的元素](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solution/by-ac_oier-bslq/) | 简单 | 🤩🤩🤩🤩 |
4748
| [981. 基于时间的键值存储](https://leetcode-cn.com/problems/time-based-key-value-store/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/time-based-key-value-store/solution/gong-shui-san-xie-yi-ti-shuang-jie-ha-xi-h5et/) | 中等 | 🤩🤩🤩🤩 |
4849
| [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/) | 困难 | 🤩🤩🤩 |
4950
| [1001. 网格照明](https://leetcode-cn.com/problems/grid-illumination/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/grid-illumination/solution/gong-shui-san-xie-ha-xi-biao-xian-ying-s-s48d/) | 困难 | 🤩🤩🤩🤩 |

Index/构造.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
33
| [406. 根据身高重建队列](https://leetcode.cn/problems/queue-reconstruction-by-height/) | [LeetCode 题解链接](https://leetcode.cn/problems/queue-reconstruction-by-height/solution/by-ac_oier-fda2/) | 中等 | 🤩🤩🤩🤩 |
44
| [942. 增减字符串匹配](https://leetcode.cn/problems/di-string-match/) | [LeetCode 题解链接](https://leetcode.cn/problems/di-string-match/solution/by-ac_oier-pvjk/) | 简单 | 🤩🤩🤩🤩🤩 |
5+
| [961. 在长度 2N 的数组中找出重复 N 次的元素](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solution/by-ac_oier-bslq/) | 简单 | 🤩🤩🤩🤩 |
56
| [1719. 重构一棵树的方案数](https://leetcode-cn.com/problems/number-of-ways-to-reconstruct-a-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-ways-to-reconstruct-a-tree/solution/gong-shui-san-xie-gou-zao-yan-zheng-he-f-q6fc/) | 困难 | 🤩🤩 |
67
| [2028. 找出缺失的观测数据](https://leetcode-cn.com/problems/find-missing-observations/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-missing-observations/solution/by-ac_oier-x22k/) | 中等 | 🤩🤩🤩🤩🤩 |
78

Index/模拟.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
| [905. 按奇偶排序数组](https://leetcode-cn.com/problems/sort-array-by-parity/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sort-array-by-parity/solution/by-ac_oier-nuz7/) | 简单 | 🤩🤩🤩 |
107107
| [944. 删列造序](https://leetcode.cn/problems/delete-columns-to-make-sorted/) | [LeetCode 题解链接](https://leetcode.cn/problems/delete-columns-to-make-sorted/solution/by-ac_oier-smoz/) | 简单 | 🤩🤩🤩 |
108108
| [953. 验证外星语词典](https://leetcode.cn/problems/verifying-an-alien-dictionary/) | [LeetCode 题解链接](https://leetcode.cn/problems/verifying-an-alien-dictionary/solution/by-ac_oier-sxf1/) | 简单 | 🤩🤩🤩🤩 |
109+
| [961. 在长度 2N 的数组中找出重复 N 次的元素](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solution/by-ac_oier-bslq/) | 简单 | 🤩🤩🤩🤩 |
109110
| [997. 找到小镇的法官](https://leetcode-cn.com/problems/find-the-town-judge/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-town-judge/solution/gong-shui-san-xie-jian-dan-chu-du-ru-du-5ms57/) | 简单 | 🤩🤩🤩🤩 |
110111
| [1001. 网格照明](https://leetcode-cn.com/problems/grid-illumination/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/grid-illumination/solution/gong-shui-san-xie-ha-xi-biao-xian-ying-s-s48d/) | 困难 | 🤩🤩🤩🤩 |
111112
| [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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| 题目 | 题解 | 难度 | 推荐指数 |
2+
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
3+
| [436. 寻找右区间](https://leetcode.cn/problems/find-right-interval/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-right-interval/solution/by-ac_oier-sijp/) | 中等 | 🤩🤩🤩🤩 |
4+

LeetCode/401-410/406. 根据身高重建队列(中等).md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,21 @@ Tag : 「排序」、「构造」、「二分」、「树状数组」
5050

5151
关于「空位序列」如图所示(黄色代表已被占用,白色代表尚未占用):
5252

53-
![image.png](https://pic.leetcode-cn.com/1652506774-WisEUD-image.png)
53+
![](https://pic.leetcode-cn.com/1652506774-WisEUD-image.png)
5454

5555
具体的,我们按照构造的合理性来解释双关键字排序的合理性,假设当前处理的是 $people[i]$:
5656

5757
根据「高度」排升序,根据「编号」排降序:由于首先是根据「高度」排升序,因此当 $people[i]$ 被放置在「当前空位序列」的第 $people[i][1] + 1$ 之后,无论后面的 $people[j]$ 如何放置,都不会影响 $people[i]$ 的合法性:后面的数的高度都不低于 $people[i][0]$,无论放在 $people[i][1] + 1$ 前面还是后面都不会影响 $people[i]$ 的合法性。
5858

59-
同时对于高度(第一维)相同,编号(第二维)不同的情况,我们进行了「降序」处理,因此「每次将 $people[i]$ 放置在空白序列的 $people[i][1] + 1$ 位置的」的逻辑能够沿用。即 **对于「高度」相同「编号」不同的情况,会被按照「从右到左」依次放置,导致了每个 $people[i]$ 被放置时,都不会受到「高度」相同的其他 $people[j]$ 所影响。换句话说,当 $people[i]$ 放置时,其左边必然不存在其他高度为 $people[i][0]$ 的成员。**
59+
同时对于高度(第一维)相同,编号(第二维)不同的情况,我们进行了「降序」处理,因此「每次将 $people[i]$ 放置在空白序列的 $people[i][1] + 1$ 位置的」的逻辑能够沿用
6060

61-
剩下的在于,如何快速找到「空白序列中的第 $k$ 个位置」,这可以通过「二分 + 树状数组」来做。
61+
**对于「高度」相同「编号」不同的情况,会被按照「从右到左」依次放置,导致了每个 $people[i]$ 被放置时,都不会受到「高度」相同的其他 $people[j]$ 所影响。换句话说,当 $people[i]$ 放置时,其左边必然不存在其他高度为 $people[i][0]$ 的成员。**
6262

63-
对于已被使用的位置标记为 $1$,未使用的位置为 $0$,那么第一个满足「$0$ 的个数大于等于 $k + 1$」的位置即是目标位置,在长度明确的情况下,求 $0$ 的个数和求 $1$ 的个数等同,对于位置 $x$ 而言(下标从 $1$ 开始,总个数为 $x$),如果在 $[1, x]$ 范围内有 $k + 1$ 个 $0$,等价于有 $x - (k + 1)$ 个 $1$,求解 $[1, x]$ 范围内 $1$ 的个数等价于求前缀和,即区间查询,同时我们每次使用一个新的位置后 ,需要对其进行标记,涉及单点修改,使用「树状数组」求解。
63+
剩下的在于,如何快速找到「空白序列中的第 $k$ 个位置」,这可以通过「二分 + 树状数组」来做:
64+
65+
对于已被使用的位置标记为 $1$,未使用的位置为 $0$,那么第一个满足「$0$ 的个数大于等于 $k + 1$」的位置即是目标位置,在长度明确的情况下,求 $0$ 的个数和求 $1$ 的个数等同,对于位置 $x$ 而言(下标从 $1$ 开始,总个数为 $x$),如果在 $[1, x]$ 范围内有 $k + 1$ 个 $0$,等价于有 $x - (k + 1)$ 个 $1$。
66+
67+
求解 $[1, x]$ 范围内 $1$ 的个数等价于求前缀和,即「区间查询」,同时我们每次使用一个新的位置后 ,需要对其进行标记,涉及「单点修改」,因此使用「树状数组」进行求解。
6468

6569
代码:
6670
```Java
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[436. 寻找右区间](https://leetcode.cn/problems/find-right-interval/solution/by-ac_oier-sijp/)** ,难度为 **中等**
4+
5+
Tag : 「排序」、「二分」、「双指针」、「莫队算法」
6+
7+
8+
9+
给你一个区间数组 `intervals` ,其中 $intervals[i] = [start_i, end_i]$,且每个 $start_i$ 都 不同 。
10+
11+
区间 $i$ 的 右侧区间 可以记作区间 $j$ ,并满足 $start_j >= end_i$ ,且 $start_j$ 最小化 。
12+
13+
返回一个由每个区间 $i$ 的 右侧区间 的最小起始位置组成的数组。如果某个区间 $i$ 不存在对应的 右侧区间 ,则下标 $i$ 处的值设为 $-1$ 。
14+
15+
示例 1:
16+
```
17+
输入:intervals = [[1,2]]
18+
19+
输出:[-1]
20+
21+
解释:集合中只有一个区间,所以输出-1。
22+
```
23+
示例 2:
24+
```
25+
输入:intervals = [[3,4],[2,3],[1,2]]
26+
27+
输出:[-1,0,1]
28+
29+
解释:对于 [3,4] ,没有满足条件的“右侧”区间。
30+
对于 [2,3] ,区间[3,4]具有最小的“右”起点;
31+
对于 [1,2] ,区间[2,3]具有最小的“右”起点。
32+
```
33+
示例 3:
34+
```
35+
输入:intervals = [[1,4],[2,3],[3,4]]
36+
37+
输出:[-1,2,-1]
38+
39+
解释:对于区间 [1,4] 和 [3,4] ,没有满足条件的“右侧”区间。
40+
对于 [2,3] ,区间 [3,4] 有最小的“右”起点。
41+
```
42+
43+
提示:
44+
* $1 <= intervals.length <= 2 \times 10^4$
45+
* $intervals[i].length == 2$
46+
* $-10^6 <= start_i <= end_i <= 10^6$
47+
* 每个间隔的起点都 不相同
48+
49+
---
50+
51+
### 排序 + 二分
52+
53+
为了方便,我们称 $intervals$ 为 $its$。
54+
55+
对于每个 $its[i]$ 而言,我们需要在所有满足「$its[j][0] \geqslant its[i][1]$」中找到 $its[j][0]$ 值最小的下标 $j$,并将其记为 $ans[i]$。
56+
57+
对于一个特定的 $its[i]$ 而言,其右端点固定,并且我们只关心目标位置的左端点。
58+
59+
因此我们可以构造一个记录区间左端点的数组 $clone$,并将其进行排序,同时为了记录每个左端点来自于原序列中的那个下标,还需要额外记录原序列下标,即以 $(start, idx)$ 二元组的形式进行转存,并根据 $start$ 排序。
60+
61+
然后从前往后处理每个 $its[i]$,运用「二分」在 $clone$ 中找到第一个满足左端点 $start$ 大于等于 $its[i][1]$ 的成员 $clone[j]$,将其 $clone[j][1]$ 即是 $its[i]$ 的最右区间。
62+
63+
代码:
64+
```Java
65+
class Solution {
66+
public int[] findRightInterval(int[][] its) {
67+
int n = its.length;
68+
int[][] clone = new int[n][2];
69+
for (int i = 0; i < n; i++) clone[i] = new int[]{its[i][0], i};
70+
Arrays.sort(clone, (a,b)->a[0]-b[0]);
71+
int[] ans = new int[n];
72+
for (int i = 0; i < n; i++) {
73+
int l = 0, r = n - 1;
74+
while (l < r) {
75+
int mid = l + r >> 1;
76+
if (clone[mid][0] >= its[i][1]) r = mid;
77+
else l = mid + 1;
78+
}
79+
ans[i] = clone[r][0] >= its[i][1] ? clone[r][1] : -1;
80+
}
81+
return ans;
82+
}
83+
}
84+
```
85+
* 时间复杂度:排序复杂度为 $O(n\log{n})$;对于每个 $its[i]$ 找到最右区间需要进行二分,复杂度为 $O(n\log{n})$。整体复杂度为 $O(n\log{n})$
86+
* 空间复杂度:$O(n)$
87+
88+
---
89+
90+
### 双指针(莫队思想)
91+
92+
更进一步,在解法一中我们并没有对求解询问的顺序进行调整,这导致了我们不得不每次都在整个左端点序列中进行二分。
93+
94+
朴素处理询问的方式,需要每次对整个序列进行扫描,复杂度为 $O(n^2)$。
95+
96+
实际上,如果我们按照「右端点从小到大」的顺序处理询问,其每个询问对应的「最右区间的左端点」也具有单调特性。
97+
98+
因此,我们可以运用莫队思想:**通过调整询问的处理顺序,来减少扫描目标位置的指针移动次数。将其从「必然进行 $n^2$ 次移动」优化为「最多不超过 $n$ 次移动」,从而将 构造答案 的复杂度从 $O(n^2)$ 优化为 $O(n)$。**
99+
100+
最后,由于每个 $its[i]$ 只关心目标位置的「左端点」,因此我们无须对某一段进行分块,而直接使用双指针实现即可。
101+
102+
代码:
103+
```Java
104+
class Solution {
105+
public int[] findRightInterval(int[][] its) {
106+
int n = its.length;
107+
int[][] ss = new int[n][2], es = new int[n][2];
108+
for (int i = 0; i < n; i++) {
109+
ss[i] = new int[]{its[i][0], i};
110+
es[i] = new int[]{its[i][1], i};
111+
}
112+
Arrays.sort(ss, (a,b)->a[0]-b[0]);
113+
Arrays.sort(es, (a,b)->a[0]-b[0]);
114+
int[] ans = new int[n];
115+
for (int i = 0, j = 0; i < n; i++) {
116+
int[] cur = es[i];
117+
int loc = cur[0], idx = cur[1];
118+
while (j < n && ss[j][0] < loc) j++;
119+
ans[idx] = j == n ? -1 : ss[j][1];
120+
}
121+
return ans;
122+
}
123+
}
124+
```
125+
* 时间复杂度:排序复杂度为 $O(n\log{n})$;双指针构造答案的复杂度为 $O(n)$。整体复杂度为 $O(n\log{n})$
126+
* 空间复杂度:$O(n)$
127+
128+
---
129+
130+
### 最后
131+
132+
这是我们「刷穿 LeetCode」系列文章的第 `No.436` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
133+
134+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
135+
136+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
137+
138+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
139+

0 commit comments

Comments
 (0)