Skip to content

Commit 8dafaeb

Browse files
authored
update: 添加问题“3356.零数组变换II”的代码和题解(#948)
* 2256: WA.cpp (#946) all 0 Signed-off-by: LetMeFly666 <[email protected]> * 2256: WA.cpp (#946) Signed-off-by: LetMeFly666 <[email protected]> * 2256: WA.cpp (#946) Signed-off-by: LetMeFly666 <[email protected]> * 2256: TLE.cpp (#946) Signed-off-by: LetMeFly666 <[email protected]> * 2256: WA.cpp (#946) Signed-off-by: LetMeFly666 <[email protected]> * 2256: WA.cpp (#946) Signed-off-by: LetMeFly666 <[email protected]> * 2256: TLE.cpp (#946) Signed-off-by: LetMeFly666 <[email protected]> * 2256: WA.cpp (#946) Signed-off-by: LetMeFly666 <[email protected]> * 3356: WA.cpp (#946) Signed-off-by: LetMeFly666 <[email protected]> * 3356: WA.cpp (#946) Signed-off-by: LetMeFly666 <[email protected]> * 3356: WA.cpp (#946) Signed-off-by: LetMeFly666 <[email protected]> * 3356: WA.cpp (#946) Signed-off-by: LetMeFly666 <[email protected]> * 3356: AC.cpp (#946) AC,42.47%,27.75% Signed-off-by: LetMeFly666 <[email protected]> * 3356: RE.py (#946) Signed-off-by: LetMeFly666 <[email protected]> * 3356: CE.java + AC.py (#946) Signed-off-by: LetMeFly666 <[email protected]> * 3356: WA.jav (#946) Signed-off-by: LetMeFly666 <[email protected]> * update: 添加问题“3356.零数组变换II”的代码和题解(#948) Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 5677ed5 commit 8dafaeb

10 files changed

+463
-203
lines changed

.gitlog

Lines changed: 0 additions & 150 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-22 13:41:00
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-22 23:16:48
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
bool ok(vector<int>& nums, vector<vector<int>>& queries, int t) {
14+
vector<int> diff(nums.size() + 1);
15+
for (int i = 0; i < t; i++) {
16+
diff[queries[i][0]] += queries[i][2];
17+
diff[queries[i][1] + 1] -= queries[i][2];
18+
}
19+
int cnt = 0;
20+
for (int i = 0; i < nums.size(); i++) {
21+
cnt += diff[i];
22+
if (nums[i] > cnt) {
23+
return false;
24+
}
25+
}
26+
return true;
27+
}
28+
public:
29+
int minZeroArray(vector<int>& nums, vector<vector<int>>& queries) {
30+
int l = -1, r = queries.size() + 1; // (l, r)
31+
while (l + 1 < r) {
32+
int m = (l + r) >> 1;
33+
if (ok(nums, queries, m)) {
34+
r = m;
35+
} else {
36+
l = m;
37+
}
38+
}
39+
return r > queries.size() ? -1 : r;
40+
}
41+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-22 22:07:10
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-22 23:33:57
6+
*/
7+
package main
8+
9+
func check3356(nums []int, queries [][]int, n int) bool {
10+
diff := make([]int, len(nums) + 1)
11+
for _, q := range queries[:n] {
12+
diff[q[0]] += q[2]
13+
diff[q[1] + 1] -= q[2]
14+
}
15+
cnt := 0
16+
for i := range nums {
17+
cnt += diff[i]
18+
if nums[i] > cnt {
19+
return false
20+
}
21+
}
22+
return true
23+
}
24+
25+
func minZeroArray(nums []int, queries [][]int) int {
26+
l, r := -1, len(queries) + 1
27+
for l + 1 < r {
28+
m := (l + r) >> 1
29+
if check3356(nums, queries, m) {
30+
r = m
31+
} else {
32+
l = m
33+
}
34+
}
35+
if r > len(queries) {
36+
return -1
37+
}
38+
return r
39+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-22 22:07:10
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-22 23:29:09
6+
*/
7+
class Solution {
8+
private int[] nums;
9+
private int[][] queries;
10+
11+
public boolean check(int n) {
12+
int[] diff = new int[nums.length + 1];
13+
for (int i = 0; i < n; i++) {
14+
diff[queries[i][0]] += queries[i][2];
15+
diff[queries[i][1] + 1] -= queries[i][2];
16+
}
17+
int cnt = 0;
18+
for (int i = 0; i < nums.length; i++) {
19+
cnt += diff[i];
20+
if (nums[i] > cnt) {
21+
return false;
22+
}
23+
}
24+
return true;
25+
}
26+
27+
public int minZeroArray(int[] nums, int[][] queries) {
28+
this.nums = nums;
29+
this.queries = queries;
30+
int l = -1, r = queries.length + 1;
31+
while (l + 1 < r) {
32+
int m = (l + r) >> 1;
33+
if (check(m)) {
34+
r = m;
35+
} else {
36+
l = m;
37+
}
38+
}
39+
return r > queries.length ? -1 : r;
40+
}
41+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-05-22 22:07:10
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-05-22 23:27:03
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def check(self, n: int) -> bool:
11+
diff = [0] * (len(self.nums) + 1)
12+
for l, r, v in self.queries[:n]:
13+
diff[l] += v
14+
diff[r + 1] -= v
15+
cnt = 0
16+
for i in range(len(self.nums)):
17+
cnt += diff[i]
18+
if self.nums[i] > cnt:
19+
return False
20+
return True
21+
22+
def minZeroArray(self, nums: List[int], queries: List[List[int]]) -> int:
23+
self.nums = nums
24+
self.queries = queries
25+
l, r = -1, len(queries) + 1
26+
while l + 1 < r:
27+
m = (l + r) >> 1
28+
if self.check(m):
29+
r = m
30+
else:
31+
l = m
32+
return -1 if r > len(queries) else r

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,7 @@
972972
|3341.到达最后一个房间的最少时间I|中等|<a href="https://leetcode.cn/problems/find-minimum-time-to-reach-last-room-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/05/07/LeetCode%203341.%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%B4I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147807022" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-minimum-time-to-reach-last-room-i/solutions/3672076/letmefly-3341dao-da-zui-hou-yi-ge-fang-j-3m6o/" target="_blank">LeetCode题解</a>|
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>|
975+
|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>|
975976
|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>|
976977
|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>|
977978
|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>|

0 commit comments

Comments
 (0)