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

Sign up
Appearance settings

Commit 8b663b3

Browse files
✨feat: add 1606 & 870
1 parent f1acc88 commit 8b663b3

File tree

6 files changed

+182
-2
lines changed

6 files changed

+182
-2
lines changed

‎Index/双指针.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
| [825. 适龄的朋友](https://leetcode-cn.com/problems/friends-of-appropriate-ages/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/friends-of-appropriate-ages/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-maa8/) | 中等 | 🤩🤩🤩🤩 |
3939
| [832. 翻转图像](https://leetcode-cn.com/problems/flipping-an-image/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/flipping-an-image/solution/shuang-zhi-zhen-yi-bian-chu-li-huan-you-ik0v1/) | 简单 | 🤩🤩 |
4040
| [838. 推多米诺](https://leetcode-cn.com/problems/push-dominoes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/push-dominoes/solution/gong-shui-san-xie-yi-ti-shuang-jie-bfs-y-z52w/) | 中等 | 🤩🤩🤩🤩 |
41+
| [870. 优势洗牌](https://leetcode.cn/problems/advantage-shuffle/) | [LeetCode 题解链接](https://leetcode.cn/problems/advantage-shuffle/solution/by-ac_oier-i01s/) | 中等 | 🤩🤩🤩🤩🤩 |
4142
| [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/) | 中等 | 🤩🤩🤩🤩 |
4243
| [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/) | 简单 | 🤩🤩🤩🤩 |
4344
| [917. 仅仅反转字母](https://leetcode-cn.com/problems/reverse-only-letters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-only-letters/solution/gong-shui-san-xie-jian-dan-shuang-zhi-zh-xrpt/) | 简单 | 🤩🤩🤩🤩 |

‎Index/哈希表.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
| [811. 子域名访问计数](https://leetcode.cn/problems/subdomain-visit-count/) | [LeetCode 题解链接](https://leetcode.cn/problems/subdomain-visit-count/solution/by-ac_oier-aex6/) | 中等 | 🤩🤩🤩🤩 |
5050
| [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/) | 中等 | 🤩🤩🤩 |
5151
| [869. 重新排序得到 2 的幂](https://leetcode-cn.com/problems/reordered-power-of-2/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reordered-power-of-2/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-c-3s1e/) | 中等 | 🤩🤩🤩🤩 |
52+
| [870. 优势洗牌](https://leetcode.cn/problems/advantage-shuffle/) | [LeetCode 题解链接](https://leetcode.cn/problems/advantage-shuffle/solution/by-ac_oier-i01s/) | 中等 | 🤩🤩🤩🤩🤩 |
5253
| [873. 最长的斐波那契子序列的长度](https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence/) | [LeetCode 题解链接](https://leetcode.cn/problems/length-of-longest-fibonacci-subsequence/solution/by-ac_oier-beo2/) | 中等 | 🤩🤩🤩 |
5354
| [884. 两句话中的不常见单词](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-wwam/) | 简单 | 🤩🤩🤩🤩🤩 |
5455
| [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/) | 简单 | 🤩🤩 |

‎Index/红黑树.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
| 题目 | 题解 | 难度 | 推荐指数 |
22
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
33
| [729. 我的日程安排表 I](https://leetcode-cn.com/problems/my-calendar-i/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/my-calendar-i/solution/by-ac_oier-1znx/) | 中等 | 🤩🤩🤩🤩🤩 |
4+
| [870. 优势洗牌](https://leetcode.cn/problems/advantage-shuffle/) | [LeetCode 题解链接](https://leetcode.cn/problems/advantage-shuffle/solution/by-ac_oier-i01s/) | 中等 | 🤩🤩🤩🤩🤩 |
45
| [1606. 找到处理最多请求的服务器](https://leetcode-cn.com/problems/find-servers-that-handled-most-number-of-requests/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-servers-that-handled-most-number-of-requests/solution/by-ac_oier-zgm6/) | 困难 | 🤩🤩🤩🤩 |
56
| [2034. 股票价格波动](https://leetcode-cn.com/problems/stock-price-fluctuation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/stock-price-fluctuation/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-u6f4/) | 中等 | 🤩🤩🤩🤩 |
67

‎Index/贪心算法.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
| [768. 最多能完成排序的块 II](https://leetcode.cn/problems/max-chunks-to-make-sorted-ii/) | [LeetCode 题解链接](https://leetcode.cn/problems/max-chunks-to-make-sorted-ii/solution/by-ac_oier-z4wt/) | 困难 | 🤩🤩🤩🤩🤩 |
2222
| [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/) | 中等 | 🤩🤩🤩🤩 |
2323
| [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/) | 中等 | 🤩🤩🤩🤩 |
24+
| [870. 优势洗牌](https://leetcode.cn/problems/advantage-shuffle/) | [LeetCode 题解链接](https://leetcode.cn/problems/advantage-shuffle/solution/by-ac_oier-i01s/) | 中等 | 🤩🤩🤩🤩🤩 |
2425
| [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/) | 困难 | 🤩🤩🤩🤩🤩 |
2526
| [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/) | 中等 | 🤩🤩🤩🤩 |
2627
| [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/) | 中等 | 🤩🤩🤩🤩 |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Tag : 「数据结构」、「优先队列(堆)」、「红黑树」、「
8585

8686
由于「任务分配规则」是优先取大于等于 `i % k` 的最小值,若取不到,再取大于等于 0ドル$ 的最小值。因此我们的「空闲池」最好是支持「二分」的有序集合,容易想到基于「红黑树」的 `TreeSet` 结构。
8787

88-
代码:
88+
Java 代码:
8989
```Java
9090
class Solution {
9191
static int N = 100010;
@@ -114,7 +114,7 @@ class Solution {
114114
}
115115
}
116116
```
117-
117+
Python 代码:
118118
```Python
119119
from sortedcontainers import SortedList
120120

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[870. 优势洗牌](https://leetcode.cn/problems/advantage-shuffle/solution/by-ac_oier-i01s/)** ,难度为 **中等**
4+
5+
Tag : 「红黑树」、「哈希表」、「排序」、「双指针」、「贪心」
6+
7+
8+
9+
给定两个大小相等的数组 `nums1``nums2`,`nums1` 相对于 `nums` 的优势可以用满足 `nums1[i] > nums2[i]` 的索引 `i` 的数目来描述。
10+
11+
返回 `nums1` 的任意排列,使其相对于 `nums2` 的优势最大化。
12+
13+
示例 1:
14+
```
15+
输入:nums1 = [2,7,11,15], nums2 = [1,10,4,11]
16+
17+
输出:[2,11,7,15]
18+
```
19+
示例 2:
20+
```
21+
输入:nums1 = [12,24,8,32], nums2 = [13,25,32,11]
22+
23+
输出:[24,32,8,12]
24+
```
25+
26+
提示:
27+
* 1ドル <= nums1.length <= 10^5$
28+
* $nums2.length = nums1.length$
29+
* 0ドル <= nums1[i], nums2[i] <= 10^9$
30+
31+
---
32+
33+
### 数据结构
34+
35+
显然,对于任意一个 $t = nums2[i]$ 而言,我们应当在候选集合中选择**比其大的最小数**,若不存在这样的数字,则选择候选集合中的**最小值**
36+
37+
同时,由于 $nums1$ 相同数会存在多个,我们还要对某个具体数字的可用次数进行记录。
38+
39+
也就是我们总共涉及两类操作:
40+
41+
1. 实时维护一个候选集合,该集合支持高效查询比某个数大的数值操作;
42+
2. 对候选集合中每个数值的可使用次数进行记录,当使用到了候选集合中的某个数后,要对其进行计数减一操作,若计数为 0ドル,ドル则将该数值从候选集合中移除。
43+
44+
计数操作容易想到哈希表,而实时维护候选集合并高效查询可以使用基于红黑树的 `TreeSet` 数据结构。
45+
46+
Java 代码:
47+
```Java
48+
class Solution {
49+
public int[] advantageCount(int[] nums1, int[] nums2) {
50+
int n = nums1.length;
51+
TreeSet<Integer> tset = new TreeSet<>();
52+
Map<Integer, Integer> map = new HashMap<>();
53+
for (int x : nums1) {
54+
map.put(x, map.getOrDefault(x, 0) + 1);
55+
if (map.get(x) == 1) tset.add(x);
56+
}
57+
int[] ans = new int[n];
58+
for (int i = 0; i < n; i++) {
59+
Integer cur = tset.ceiling(nums2[i] + 1);
60+
if (cur == null) cur = tset.ceiling(-1);
61+
ans[i] = cur;
62+
map.put(cur, map.get(cur) - 1);
63+
if (map.get(cur) == 0) tset.remove(cur);
64+
}
65+
return ans;
66+
}
67+
}
68+
```
69+
Python 代码:
70+
```Python
71+
from sortedcontainers import SortedList
72+
73+
class Solution:
74+
def advantageCount(self, nums1: List[int], nums2: List[int]) -> List[int]:
75+
n = len(nums1)
76+
cnts, tset = defaultdict(int), SortedList()
77+
for i in range(n):
78+
cnts[nums1[i]] += 1
79+
if cnts[nums1[i]] == 1:
80+
tset.add(nums1[i])
81+
ans = [0] * n
82+
for i in range(n):
83+
t = nums2[i]
84+
if (idx := tset.bisect_left(t + 1)) == len(tset):
85+
idx = tset.bisect_left(-1)
86+
ans[i] = tset[idx]
87+
cnts[ans[i]] -= 1
88+
if cnts[ans[i]] == 0:
89+
tset.remove(ans[i])
90+
return ans
91+
```
92+
* 时间复杂度:$O(n\log{n})$
93+
* 空间复杂度:$O(n)$
94+
95+
---
96+
97+
### 排序 + 双指针
98+
99+
在解法一中,我们是从每个 $nums2[i]$ 出发考虑,使用哪个 $nums1[i]$ 去匹配最为合适。
100+
101+
实际上,我们也能从 $nums1[i]$ 出发,考虑将其与哪个 $nums2[i]$ 进行匹配。
102+
103+
为了让每个决策回合具有独立性,我们需要对两数组进行排序,同时为了在构造答案时,能够对应回 `nums2` 的原下标,排序前我们需要使用「哈希表」记录每个 $nums2[i]$ 的下标为何值。
104+
105+
使用变量 `l1` 代表当前决策将 $nums1[l1]$ 分配到哪个 `nums2` 的位置,使用 `l2``r2` 代表当前 `nums2` 中还有 $[l2, r2]$ 位置还待填充。
106+
107+
可以证明我们在从前往后给每个 $nums1[l1]$ 分配具体位置时,分配的位置只会在 `l2``r2` 两者之间产生。
108+
109+
Java 代码:
110+
```Java
111+
class Solution {
112+
public int[] advantageCount(int[] nums1, int[] nums2) {
113+
int n = nums1.length;
114+
Map<Integer, List<Integer>> map = new HashMap<>();
115+
for (int i = 0; i < n; i++) {
116+
List<Integer> list = map.getOrDefault(nums2[i], new ArrayList<>());
117+
list.add(i);
118+
map.put(nums2[i], list);
119+
}
120+
Arrays.sort(nums1); Arrays.sort(nums2);
121+
int[] ans = new int[n];
122+
for (int l1 = 0, l2 = 0, r2 = n - 1; l1 < n; l1++) {
123+
int t = nums1[l1] > nums2[l2] ? l2 : r2;
124+
List<Integer> list = map.get(nums2[t]);
125+
int idx = list.remove(list.size() - 1);
126+
ans[idx] = nums1[l1];
127+
if (t == l2) l2++;
128+
else r2--;
129+
}
130+
return ans;
131+
}
132+
}
133+
```
134+
Python 代码:
135+
```Python
136+
class Solution:
137+
def advantageCount(self, nums1: List[int], nums2: List[int]) -> List[int]:
138+
n = len(nums1)
139+
mapping = defaultdict(list)
140+
for i in range(n):
141+
mapping[nums2[i]].append(i)
142+
nums1.sort()
143+
nums2.sort()
144+
ans = [0] * n
145+
l2, r2 = 0, n - 1
146+
for l1 in range(n):
147+
t = l2 if nums1[l1] > nums2[l2] else r2
148+
ans[mapping[nums2[t]].pop()] = nums1[l1]
149+
if t == l2:
150+
l2 += 1
151+
else:
152+
r2 -= 1
153+
return ans
154+
```
155+
* 时间复杂度:$O(n\log{n})$
156+
* 空间复杂度:$O(n)$
157+
158+
159+
---
160+
161+
### 加餐
162+
163+
**加餐一道同类型题目 : [难度 2.5/5,敲醒沉睡心灵的数据结构运用题](https://mp.weixin.qq.com/s?__biz=MzU4NDE3MTEyMA==&mid=2247493710&idx=1&sn=2d6d4ee9f4b1ff1ea766ddf7ea57bf55) 🎉🎉**
164+
165+
---
166+
167+
### 最后
168+
169+
这是我们「刷穿 LeetCode」系列文章的第 `No.870` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
170+
171+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
172+
173+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
174+
175+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
176+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /