Skip to content

Commit 256fa92

Browse files
authored
Merge pull request #950 from LetMeFly666/3362
update: 添加问题“3362.零数组变换III”的代码和题解(#950)
2 parents 8dafaeb + 031c720 commit 256fa92

14 files changed

+487
-34
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-23 23:35:45
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-23 23:45:02
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int maxRemoval(vector<int>& nums, vector<vector<int>>& queries) {
14+
sort(queries.begin(), queries.end());
15+
vector<int> diff(nums.size() + 1);
16+
priority_queue<int> pq;
17+
for (int in = 0, iq = 0, cnt = 0; in < nums.size(); in++) {
18+
cnt += diff[in];
19+
while (iq < queries.size() && queries[iq][0] <= in) {
20+
pq.push(queries[iq++][1]);
21+
}
22+
while (cnt < nums[in] && pq.size() && pq.top() >= in) {
23+
cnt++;
24+
diff[pq.top() + 1]--;
25+
pq.pop();
26+
}
27+
if (cnt < nums[in]) {
28+
return -1;
29+
}
30+
}
31+
return pq.size();
32+
}
33+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-23 23:35:45
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-24 00:22:47
6+
*/
7+
package main
8+
9+
import (
10+
"slices"
11+
"container/heap"
12+
)
13+
14+
func maxRemoval(nums []int, queries [][]int) int {
15+
slices.SortFunc(queries, func(a, b []int) int {
16+
return a[0] - b[0]
17+
})
18+
diff := make([]int, len(nums) + 1)
19+
pq := &pq3362{}
20+
for in, iq, cnt := 0, 0, 0; in < len(nums); in++ {
21+
cnt += diff[in]
22+
for iq < len(queries) && queries[iq][0] <= in {
23+
heap.Push(pq, queries[iq][1])
24+
iq++
25+
}
26+
for cnt < nums[in] && len(*pq) > 0 && (*pq)[0] >= in {
27+
cnt++
28+
diff[heap.Pop(pq).(int) + 1]--
29+
}
30+
if cnt < nums[in] {
31+
return -1
32+
}
33+
}
34+
return len(*pq)
35+
}
36+
37+
type pq3362 []int
38+
func (pq *pq3362) Push(a any) {(*pq) = append((*pq), a.(int))}
39+
func (pq pq3362) Len() int {return len(pq)}
40+
func (pq pq3362) Less(i, j int) bool {return pq[i] > pq[j]}
41+
func (pq pq3362) Swap(i, j int) {pq[i], pq[j] = pq[j], pq[i]}
42+
func (pq *pq3362) Pop() any {a := (*pq)[len(*pq)-1]; (*pq) = (*pq)[:len(*pq)-1]; return a}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-23 23:35:45
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-23 23:57:57
6+
*/
7+
import java.util.Arrays;
8+
import java.util.PriorityQueue;
9+
10+
class Solution {
11+
public int maxRemoval(int[] nums, int[][] queries) {
12+
Arrays.sort(queries, (a, b) -> a[0] - b[0]);
13+
int[] diff = new int[nums.length + 1];
14+
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
15+
for (int in = 0, iq = 0, cnt = 0; in < nums.length; in++) {
16+
cnt += diff[in];
17+
while (iq < queries.length && queries[iq][0] <= in) {
18+
pq.add(queries[iq++][1]);
19+
}
20+
while (cnt < nums[in] && !pq.isEmpty() && pq.peek() >= in) {
21+
cnt++;
22+
diff[pq.poll() + 1]--;
23+
}
24+
if (cnt < nums[in]) {
25+
return -1;
26+
}
27+
}
28+
return pq.size();
29+
}
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-05-23 23:35:45
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-05-23 23:52:09
6+
'''
7+
from typing import List
8+
import heapq
9+
10+
class Solution:
11+
def maxRemoval(self, nums: List[int], queries: List[List[int]]) -> int:
12+
queries.sort()
13+
diff = [0] * (len(nums) + 1)
14+
cnt = iq = 0
15+
pq = []
16+
for inum in range(len(nums)):
17+
cnt += diff[inum]
18+
while iq < len(queries) and queries[iq][0] <= inum:
19+
heapq.heappush(pq, -queries[iq][1])
20+
iq += 1
21+
while cnt < nums[inum] and len(pq) and -pq[0] >= inum:
22+
cnt += 1
23+
diff[-heapq.heappop(pq) + 1] -= 1
24+
if cnt < nums[inum]:
25+
return -1
26+
return len(pq)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-24 16:04:04
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-24 16:06:59
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int maxRemoval(vector<int>& nums, vector<vector<int>>& queries) {
14+
sort(queries.begin(), queries.end());
15+
vector<int> diff(nums.size() + 1);
16+
priority_queue<int> pq;
17+
for (int in = 0, iq = 0, cnt = 0; in < nums.size(); in++) {
18+
cnt += diff[in];
19+
while (iq < queries.size() && queries[iq][0] == in) {
20+
pq.push(queries[iq++][1]);
21+
}
22+
while (cnt < nums[in] && pq.size() && pq.top() >= in) {
23+
cnt++;
24+
diff[pq.top() + 1]--;
25+
pq.pop();
26+
}
27+
if (cnt < nums[in]) {
28+
return -1;
29+
}
30+
}
31+
return pq.size();
32+
}
33+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,7 @@
973973
|3342.到达最后一个房间的最少时间II|中等|<a href="https://leetcode.cn/problems/find-minimum-time-to-reach-last-room-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/09/LeetCode%203342.%E5%88%B0%E8%BE%BE%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E6%88%BF%E9%97%B4%E7%9A%84%E6%9C%80%E5%B0%91%E6%97%B6%E9%97%B4II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147835524" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-minimum-time-to-reach-last-room-ii/solutions/3672695/letmefly-3342dao-da-zui-hou-yi-ge-fang-j-qrh6/" target="_blank">LeetCode题解</a>|
974974
|3355.零数组变换I|中等|<a href="https://leetcode.cn/problems/zero-array-transformation-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/21/LeetCode%203355.%E9%9B%B6%E6%95%B0%E7%BB%84%E5%8F%98%E6%8D%A2I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148126611" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/zero-array-transformation-i/solutions/3683113/letmefly-3355ling-shu-zu-bian-huan-ichai-njzr/" target="_blank">LeetCode题解</a>|
975975
|3356.零数组变换II|中等|<a href="https://leetcode.cn/problems/zero-array-transformation-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/22/LeetCode%203356.%E9%9B%B6%E6%95%B0%E7%BB%84%E5%8F%98%E6%8D%A2II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148151470" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/zero-array-transformation-ii/solutions/3683924/letmefly-3356ling-shu-zu-bian-huan-iier-zrey9/" target="_blank">LeetCode题解</a>|
976+
|3362.零数组变换III|中等|<a href="https://leetcode.cn/problems/zero-array-transformation-iii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/24/LeetCode%203362.%E9%9B%B6%E6%95%B0%E7%BB%84%E5%8F%98%E6%8D%A2III/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148192450" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/zero-array-transformation-iii/solutions/3684942/letmefly-3362ling-shu-zu-bian-huan-iiita-qfve/" target="_blank">LeetCode题解</a>|
976977
|3375.使数组的值全部为K的最少操作次数|简单|<a href="https://leetcode.cn/problems/minimum-operations-to-make-array-values-equal-to-k/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/09/LeetCode%203375.%E4%BD%BF%E6%95%B0%E7%BB%84%E7%9A%84%E5%80%BC%E5%85%A8%E9%83%A8%E4%B8%BAK%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147104288" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-operations-to-make-array-values-equal-to-k/solutions/3646138/letmefly-3375shi-shu-zu-de-zhi-quan-bu-w-i71e/" target="_blank">LeetCode题解</a>|
977978
|3392.统计符合条件长度为3的子数组数目|简单|<a href="https://leetcode.cn/problems/count-subarrays-of-length-three-with-a-condition/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/29/LeetCode%203392.%E7%BB%9F%E8%AE%A1%E7%AC%A6%E5%90%88%E6%9D%A1%E4%BB%B6%E9%95%BF%E5%BA%A6%E4%B8%BA3%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147603015" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-subarrays-of-length-three-with-a-condition/solutions/3665081/letmefly-3392tong-ji-fu-he-tiao-jian-cha-h935/" target="_blank">LeetCode题解</a>|
978979
|3396.使数组元素互不相同所需的最少操作次数|简单|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/08/LeetCode%203396.%E4%BD%BF%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E4%BA%92%E4%B8%8D%E7%9B%B8%E5%90%8C%E6%89%80%E9%9C%80%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147080178" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/solutions/3644951/letmefly-3396shi-shu-zu-yuan-su-hu-bu-xi-glg4/" target="_blank">LeetCode题解</a>|

Solutions/LeetCode 3337.字符串转换后的长度II.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,30 @@ categories: [题解, LeetCode]
3636
<li>
3737
<p><strong>第一次转换 (t = 1)</strong></p>
3838

39-
<ul>
40-
<li><code>'a'</code> 变为 <code>'b'</code> 因为 <code>nums[0] == 1</code></li>
41-
<li><code>'b'</code> 变为 <code>'c'</code> 因为 <code>nums[1] == 1</code></li>
42-
<li><code>'c'</code> 变为 <code>'d'</code> 因为 <code>nums[2] == 1</code></li>
43-
<li><code>'y'</code> 变为 <code>'z'</code> 因为 <code>nums[24] == 1</code></li>
44-
<li><code>'y'</code> 变为 <code>'z'</code> 因为 <code>nums[24] == 1</code></li>
45-
<li>第一次转换后的字符串为: <code>"bcdzz"</code></li>
46-
</ul>
47-
</li>
48-
<li>
49-
<p><strong>第二次转换 (t = 2)</strong></p>
39+
<ul>
40+
<li><code>'a'</code> 变为 <code>'b'</code> 因为 <code>nums[0] == 1</code></li>
41+
<li><code>'b'</code> 变为 <code>'c'</code> 因为 <code>nums[1] == 1</code></li>
42+
<li><code>'c'</code> 变为 <code>'d'</code> 因为 <code>nums[2] == 1</code></li>
43+
<li><code>'y'</code> 变为 <code>'z'</code> 因为 <code>nums[24] == 1</code></li>
44+
<li><code>'y'</code> 变为 <code>'z'</code> 因为 <code>nums[24] == 1</code></li>
45+
<li>第一次转换后的字符串为: <code>"bcdzz"</code></li>
46+
</ul>
47+
</li>
48+
<li>
49+
<p><strong>第二次转换 (t = 2)</strong></p>
5050

51-
<ul>
52-
<li><code>'b'</code> 变为 <code>'c'</code> 因为 <code>nums[1] == 1</code></li>
53-
<li><code>'c'</code> 变为 <code>'d'</code> 因为 <code>nums[2] == 1</code></li>
54-
<li><code>'d'</code> 变为 <code>'e'</code> 因为 <code>nums[3] == 1</code></li>
55-
<li><code>'z'</code> 变为 <code>'ab'</code> 因为 <code>nums[25] == 2</code></li>
56-
<li><code>'z'</code> 变为 <code>'ab'</code> 因为 <code>nums[25] == 2</code></li>
57-
<li>第二次转换后的字符串为: <code>"cdeabab"</code></li>
58-
</ul>
59-
</li>
60-
<li>
61-
<p><strong>字符串最终长度:</strong> 字符串为 <code>"cdeabab"</code>,长度为 7 个字符。</p>
62-
</li>
51+
<ul>
52+
<li><code>'b'</code> 变为 <code>'c'</code> 因为 <code>nums[1] == 1</code></li>
53+
<li><code>'c'</code> 变为 <code>'d'</code> 因为 <code>nums[2] == 1</code></li>
54+
<li><code>'d'</code> 变为 <code>'e'</code> 因为 <code>nums[3] == 1</code></li>
55+
<li><code>'z'</code> 变为 <code>'ab'</code> 因为 <code>nums[25] == 2</code></li>
56+
<li><code>'z'</code> 变为 <code>'ab'</code> 因为 <code>nums[25] == 2</code></li>
57+
<li>第二次转换后的字符串为: <code>"cdeabab"</code></li>
58+
</ul>
59+
</li>
60+
<li>
61+
<p><strong>字符串最终长度:</strong> 字符串为 <code>"cdeabab"</code>,长度为 7 个字符。</p>
62+
</li>
6363
</ul>
6464
</div>
6565

Solutions/LeetCode 3356.零数组变换II.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ categories: [题解, LeetCode]
3636

3737
<ul>
3838
<li><strong>对于 i = 0(l = 0, r = 2, val = 1):</strong>
39-
4039
<ul>
4140
<li>在下标&nbsp;<code>[0, 1, 2]</code> 处分别减少 <code>[1, 0, 1]</code>。</li>
4241
<li>数组将变为 <code>[1, 0, 1]</code>。</li>
@@ -62,7 +61,6 @@ categories: [题解, LeetCode]
6261

6362
<ul>
6463
<li><strong>对于 i = 0(l = 1, r = 3, val = 2):</strong>
65-
6664
<ul>
6765
<li>在下标&nbsp;<code>[1, 2, 3]</code> 处分别减少 <code>[2, 2, 1]</code>。</li>
6866
<li>数组将变为 <code>[4, 1, 0, 0]</code>。</li>
@@ -92,7 +90,7 @@ categories: [题解, LeetCode]
9290

9391

9492

95-
## 解题方法:xx
93+
## 解题方法:二分查找+差分数组
9694

9795
首先请解决[3355.零数组变换 I](https://blog.letmefly.xyz/2025/05/21/LeetCode%203355.%E9%9B%B6%E6%95%B0%E7%BB%84%E5%8F%98%E6%8D%A2I/)
9896

0 commit comments

Comments
 (0)