From 35637c5d724cf1ea3d5bf51bcbe65bae2f51e89d Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 17 Nov 2024 21:56:49 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.3354~3356 --- .../README.md | 97 +++++++++++- .../README_EN.md | 97 +++++++++++- .../Solution.cpp | 17 ++ .../Solution.go | 23 +++ .../Solution.java | 16 ++ .../Solution.py | 12 ++ .../Solution.ts | 14 ++ .../README.md | 100 +++++++++++- .../README_EN.md | 100 +++++++++++- .../Solution.cpp | 20 +++ .../Solution.go | 16 ++ .../Solution.java | 18 +++ .../Solution.py | 12 ++ .../Solution.ts | 15 ++ .../README.md | 147 +++++++++++++++++- .../README_EN.md | 147 +++++++++++++++++- .../Solution.cpp | 33 ++++ .../Solution.go | 23 +++ .../Solution.java | 38 +++++ .../Solution.py | 17 ++ .../Solution.ts | 29 ++++ 21 files changed, 969 insertions(+), 22 deletions(-) create mode 100644 solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.cpp create mode 100644 solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.go create mode 100644 solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.java create mode 100644 solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.py create mode 100644 solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.ts create mode 100644 solution/3300-3399/3355.Zero Array Transformation I/Solution.cpp create mode 100644 solution/3300-3399/3355.Zero Array Transformation I/Solution.go create mode 100644 solution/3300-3399/3355.Zero Array Transformation I/Solution.java create mode 100644 solution/3300-3399/3355.Zero Array Transformation I/Solution.py create mode 100644 solution/3300-3399/3355.Zero Array Transformation I/Solution.ts create mode 100644 solution/3300-3399/3356.Zero Array Transformation II/Solution.cpp create mode 100644 solution/3300-3399/3356.Zero Array Transformation II/Solution.go create mode 100644 solution/3300-3399/3356.Zero Array Transformation II/Solution.java create mode 100644 solution/3300-3399/3356.Zero Array Transformation II/Solution.py create mode 100644 solution/3300-3399/3356.Zero Array Transformation II/Solution.ts diff --git a/solution/3300-3399/3354.Make Array Elements Equal to Zero/README.md b/solution/3300-3399/3354.Make Array Elements Equal to Zero/README.md index 4099c355a3f7e..5ccce967f115f 100644 --- a/solution/3300-3399/3354.Make Array Elements Equal to Zero/README.md +++ b/solution/3300-3399/3354.Make Array Elements Equal to Zero/README.md @@ -93,32 +93,121 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3354.Ma -### 方法一 +### 方法一:枚举 + 前缀和 + +假设我们初始向左移动,遇到了一个非零元素,那么我们就需要将这个元素减一,然后改变移动方向,继续移动。 + +因此,我们可以维护每个零值元素左侧的元素和 $l$,右侧元素的和 $s - l$。如果 $l = s - l$,即左侧元素和等于右侧元素和,那么我们可以选择当前零值元素,向左或向右移动,答案加 $2$;如果 $|l - (s - l)| = 1$,此时如果左侧元素和更大,那么我们可以选择当前零值元素,向左移动,答案加 $1$,如果右侧元素和更大,那么我们可以选择当前零值元素,向右移动,答案加 $1$。 + +时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def countValidSelections(self, nums: List[int]) -> int: + s = sum(nums) + ans = l = 0 + for x in nums: + if x: + l += x + elif l * 2 == s: + ans += 2 + elif abs(l * 2 - s) == 1: + ans += 1 + return ans ``` #### Java ```java - +class Solution { + public int countValidSelections(int[] nums) { + int s = Arrays.stream(nums).sum(); + int ans = 0, l = 0; + for (int x : nums) { + if (x != 0) { + l += x; + } else if (l * 2 == s) { + ans += 2; + } else if (Math.abs(l * 2 - s) <= 1) { + ++ans; + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int countValidSelections(vector& nums) { + int s = accumulate(nums.begin(), nums.end(), 0); + int ans = 0, l = 0; + for (int x : nums) { + if (x) { + l += x; + } else if (l * 2 == s) { + ans += 2; + } else if (abs(l * 2 - s) <= 1) { + ++ans; + } + } + return ans; + } +}; ``` #### Go ```go +func countValidSelections(nums []int) (ans int) { + l, s := 0, 0 + for _, x := range nums { + s += x + } + for _, x := range nums { + if x != 0 { + l += x + } else if l*2 == s { + ans += 2 + } else if abs(l*2-s) <= 1 { + ans++ + } + } + return +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` +#### TypeScript + +```ts +function countValidSelections(nums: number[]): number { + const s = nums.reduce((acc, x) => acc + x, 0); + let [ans, l] = [0, 0]; + for (const x of nums) { + if (x) { + l += x; + } else if (l * 2 === s) { + ans += 2; + } else if (Math.abs(l * 2 - s) <= 1) { + ++ans; + } + } + return ans; +} ``` diff --git a/solution/3300-3399/3354.Make Array Elements Equal to Zero/README_EN.md b/solution/3300-3399/3354.Make Array Elements Equal to Zero/README_EN.md index 3a191da017c37..b4418dbc2e6b5 100644 --- a/solution/3300-3399/3354.Make Array Elements Equal to Zero/README_EN.md +++ b/solution/3300-3399/3354.Make Array Elements Equal to Zero/README_EN.md @@ -91,32 +91,121 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3354.Ma -### Solution 1 +### Solution 1: Enumeration + Prefix Sum + +Suppose we initially move to the left and encounter a non-zero element. In that case, we need to decrement this element by one, then change the direction of movement and continue moving. + +Therefore, we can maintain the sum of elements to the left of each zero-value element as $l$, and the sum of elements to the right as $s - l$. If $l = s - l$, meaning the sum of elements on the left equals the sum of elements on the right, we can choose the current zero-value element and move either left or right, adding $2$ to the answer. If $|l - (s - l)| = 1$, and the sum of elements on the left is greater, we can choose the current zero-value element and move left, adding $1$ to the answer. If the sum of elements on the right is greater, we can choose the current zero-value element and move right, adding $1$ to the answer. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. #### Python3 ```python - +class Solution: + def countValidSelections(self, nums: List[int]) -> int: + s = sum(nums) + ans = l = 0 + for x in nums: + if x: + l += x + elif l * 2 == s: + ans += 2 + elif abs(l * 2 - s) == 1: + ans += 1 + return ans ``` #### Java ```java - +class Solution { + public int countValidSelections(int[] nums) { + int s = Arrays.stream(nums).sum(); + int ans = 0, l = 0; + for (int x : nums) { + if (x != 0) { + l += x; + } else if (l * 2 == s) { + ans += 2; + } else if (Math.abs(l * 2 - s) <= 1) { + ++ans; + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int countValidSelections(vector& nums) { + int s = accumulate(nums.begin(), nums.end(), 0); + int ans = 0, l = 0; + for (int x : nums) { + if (x) { + l += x; + } else if (l * 2 == s) { + ans += 2; + } else if (abs(l * 2 - s) <= 1) { + ++ans; + } + } + return ans; + } +}; ``` #### Go ```go +func countValidSelections(nums []int) (ans int) { + l, s := 0, 0 + for _, x := range nums { + s += x + } + for _, x := range nums { + if x != 0 { + l += x + } else if l*2 == s { + ans += 2 + } else if abs(l*2-s) <= 1 { + ans++ + } + } + return +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` +#### TypeScript + +```ts +function countValidSelections(nums: number[]): number { + const s = nums.reduce((acc, x) => acc + x, 0); + let [ans, l] = [0, 0]; + for (const x of nums) { + if (x) { + l += x; + } else if (l * 2 === s) { + ans += 2; + } else if (Math.abs(l * 2 - s) <= 1) { + ++ans; + } + } + return ans; +} ``` diff --git a/solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.cpp b/solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.cpp new file mode 100644 index 0000000000000..513ff77e46097 --- /dev/null +++ b/solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int countValidSelections(vector& nums) { + int s = accumulate(nums.begin(), nums.end(), 0); + int ans = 0, l = 0; + for (int x : nums) { + if (x) { + l += x; + } else if (l * 2 == s) { + ans += 2; + } else if (abs(l * 2 - s) <= 1) { + ++ans; + } + } + return ans; + } +}; diff --git a/solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.go b/solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.go new file mode 100644 index 0000000000000..df95acaa161f4 --- /dev/null +++ b/solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.go @@ -0,0 +1,23 @@ +func countValidSelections(nums []int) (ans int) { + l, s := 0, 0 + for _, x := range nums { + s += x + } + for _, x := range nums { + if x != 0 { + l += x + } else if l*2 == s { + ans += 2 + } else if abs(l*2-s) <= 1 { + ans++ + } + } + return +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} diff --git a/solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.java b/solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.java new file mode 100644 index 0000000000000..5c74958fd680e --- /dev/null +++ b/solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.java @@ -0,0 +1,16 @@ +class Solution { + public int countValidSelections(int[] nums) { + int s = Arrays.stream(nums).sum(); + int ans = 0, l = 0; + for (int x : nums) { + if (x != 0) { + l += x; + } else if (l * 2 == s) { + ans += 2; + } else if (Math.abs(l * 2 - s) <= 1) { + ++ans; + } + } + return ans; + } +} diff --git a/solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.py b/solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.py new file mode 100644 index 0000000000000..652f94179fb7f --- /dev/null +++ b/solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.py @@ -0,0 +1,12 @@ +class Solution: + def countValidSelections(self, nums: List[int]) -> int: + s = sum(nums) + ans = l = 0 + for x in nums: + if x: + l += x + elif l * 2 == s: + ans += 2 + elif abs(l * 2 - s) == 1: + ans += 1 + return ans diff --git a/solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.ts b/solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.ts new file mode 100644 index 0000000000000..587cc0d4edd9c --- /dev/null +++ b/solution/3300-3399/3354.Make Array Elements Equal to Zero/Solution.ts @@ -0,0 +1,14 @@ +function countValidSelections(nums: number[]): number { + const s = nums.reduce((acc, x) => acc + x, 0); + let [ans, l] = [0, 0]; + for (const x of nums) { + if (x) { + l += x; + } else if (l * 2 === s) { + ans += 2; + } else if (Math.abs(l * 2 - s) <= 1) { + ++ans; + } + } + return ans; +} diff --git a/solution/3300-3399/3355.Zero Array Transformation I/README.md b/solution/3300-3399/3355.Zero Array Transformation I/README.md index 34609836a49c1..a8e29d5413143 100644 --- a/solution/3300-3399/3355.Zero Array Transformation I/README.md +++ b/solution/3300-3399/3355.Zero Array Transformation I/README.md @@ -97,32 +97,124 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3355.Ze -### 方法一 +### 方法一:差分数组 + +我们可以使用差分数组来解决这个问题。 + +定义一个长度为 $n + 1$ 的数组 $d$,初始值全部为 $0$。对于每个查询 $[l, r]$,我们将 $d[l]$ 加 $1$,将 $d[r + 1]$ 减 $1$。 + +然后我们遍历数组 $d$ 在 $[0, n - 1]$ 范围内的每个元素,累加前缀和 $s$,如果 $\textit{nums}[i] > s$,说明 $\textit{nums}$ 不能转换为零数组,返回 $\textit{false}$。 + +遍历结束后,返回 $\textit{true}$。 + +时间复杂度 $O(n + m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别为数组 $\textit{nums}$ 和 $\textit{queries}$ 的长度。 #### Python3 ```python - +class Solution: + def isZeroArray(self, nums: List[int], queries: List[List[int]]) -> bool: + d = [0] * (len(nums) + 1) + for l, r in queries: + d[l] += 1 + d[r + 1] -= 1 + s = 0 + for x, y in zip(nums, d): + s += y + if x > s: + return False + return True ``` #### Java ```java - +class Solution { + public boolean isZeroArray(int[] nums, int[][] queries) { + int n = nums.length; + int[] d = new int[n + 1]; + for (var q : queries) { + int l = q[0], r = q[1]; + ++d[l]; + --d[r + 1]; + } + for (int i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; + } +} ``` #### C++ ```cpp - +class Solution { +public: + bool isZeroArray(vector& nums, vector>& queries) { + int n = nums.size(); + int d[n + 1]; + memset(d, 0, sizeof(d)); + for (const auto& q : queries) { + int l = q[0], r = q[1]; + ++d[l]; + --d[r + 1]; + } + for (int i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; + } +}; ``` #### Go ```go +func isZeroArray(nums []int, queries [][]int) bool { + d := make([]int, len(nums)+1) + for _, q := range queries { + l, r := q[0], q[1] + d[l]++ + d[r+1]-- + } + s := 0 + for i, x := range nums { + s += d[i] + if x > s { + return false + } + } + return true +} +``` +#### TypeScript + +```ts +function isZeroArray(nums: number[], queries: number[][]): boolean { + const n = nums.length; + const d: number[] = Array(n + 1).fill(0); + for (const [l, r] of queries) { + ++d[l]; + --d[r + 1]; + } + for (let i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; +} ``` diff --git a/solution/3300-3399/3355.Zero Array Transformation I/README_EN.md b/solution/3300-3399/3355.Zero Array Transformation I/README_EN.md index a13c02c717370..c78da98514b51 100644 --- a/solution/3300-3399/3355.Zero Array Transformation I/README_EN.md +++ b/solution/3300-3399/3355.Zero Array Transformation I/README_EN.md @@ -95,32 +95,124 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3355.Ze -### Solution 1 +### Solution 1: Difference Array + +We can use a difference array to solve this problem. + +Define an array $d$ of length $n + 1$, with all initial values set to $0$. For each query $[l, r]$, we add $1$ to $d[l]$ and subtract $1$ from $d[r + 1]$. + +Then we traverse the array $d$ within the range $[0, n - 1]$, accumulating the prefix sum $s$. If $\textit{nums}[i] > s$, it means $\textit{nums}$ cannot be converted to a zero array, so we return $\textit{false}$. + +After traversing, return $\textit{true}$. + +The time complexity is $O(n + m)$, and the space complexity is $O(n)$. Here, $n$ and $m$ are the lengths of the array $\textit{nums}$ and the number of queries, respectively. #### Python3 ```python - +class Solution: + def isZeroArray(self, nums: List[int], queries: List[List[int]]) -> bool: + d = [0] * (len(nums) + 1) + for l, r in queries: + d[l] += 1 + d[r + 1] -= 1 + s = 0 + for x, y in zip(nums, d): + s += y + if x > s: + return False + return True ``` #### Java ```java - +class Solution { + public boolean isZeroArray(int[] nums, int[][] queries) { + int n = nums.length; + int[] d = new int[n + 1]; + for (var q : queries) { + int l = q[0], r = q[1]; + ++d[l]; + --d[r + 1]; + } + for (int i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; + } +} ``` #### C++ ```cpp - +class Solution { +public: + bool isZeroArray(vector& nums, vector>& queries) { + int n = nums.size(); + int d[n + 1]; + memset(d, 0, sizeof(d)); + for (const auto& q : queries) { + int l = q[0], r = q[1]; + ++d[l]; + --d[r + 1]; + } + for (int i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; + } +}; ``` #### Go ```go +func isZeroArray(nums []int, queries [][]int) bool { + d := make([]int, len(nums)+1) + for _, q := range queries { + l, r := q[0], q[1] + d[l]++ + d[r+1]-- + } + s := 0 + for i, x := range nums { + s += d[i] + if x > s { + return false + } + } + return true +} +``` +#### TypeScript + +```ts +function isZeroArray(nums: number[], queries: number[][]): boolean { + const n = nums.length; + const d: number[] = Array(n + 1).fill(0); + for (const [l, r] of queries) { + ++d[l]; + --d[r + 1]; + } + for (let i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; +} ``` diff --git a/solution/3300-3399/3355.Zero Array Transformation I/Solution.cpp b/solution/3300-3399/3355.Zero Array Transformation I/Solution.cpp new file mode 100644 index 0000000000000..2d82b953d484f --- /dev/null +++ b/solution/3300-3399/3355.Zero Array Transformation I/Solution.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + bool isZeroArray(vector& nums, vector>& queries) { + int n = nums.size(); + int d[n + 1]; + memset(d, 0, sizeof(d)); + for (const auto& q : queries) { + int l = q[0], r = q[1]; + ++d[l]; + --d[r + 1]; + } + for (int i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; + } +}; diff --git a/solution/3300-3399/3355.Zero Array Transformation I/Solution.go b/solution/3300-3399/3355.Zero Array Transformation I/Solution.go new file mode 100644 index 0000000000000..3bbe77edce387 --- /dev/null +++ b/solution/3300-3399/3355.Zero Array Transformation I/Solution.go @@ -0,0 +1,16 @@ +func isZeroArray(nums []int, queries [][]int) bool { + d := make([]int, len(nums)+1) + for _, q := range queries { + l, r := q[0], q[1] + d[l]++ + d[r+1]-- + } + s := 0 + for i, x := range nums { + s += d[i] + if x > s { + return false + } + } + return true +} diff --git a/solution/3300-3399/3355.Zero Array Transformation I/Solution.java b/solution/3300-3399/3355.Zero Array Transformation I/Solution.java new file mode 100644 index 0000000000000..a3a3a459264d4 --- /dev/null +++ b/solution/3300-3399/3355.Zero Array Transformation I/Solution.java @@ -0,0 +1,18 @@ +class Solution { + public boolean isZeroArray(int[] nums, int[][] queries) { + int n = nums.length; + int[] d = new int[n + 1]; + for (var q : queries) { + int l = q[0], r = q[1]; + ++d[l]; + --d[r + 1]; + } + for (int i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; + } +} diff --git a/solution/3300-3399/3355.Zero Array Transformation I/Solution.py b/solution/3300-3399/3355.Zero Array Transformation I/Solution.py new file mode 100644 index 0000000000000..85a0cad1047b8 --- /dev/null +++ b/solution/3300-3399/3355.Zero Array Transformation I/Solution.py @@ -0,0 +1,12 @@ +class Solution: + def isZeroArray(self, nums: List[int], queries: List[List[int]]) -> bool: + d = [0] * (len(nums) + 1) + for l, r in queries: + d[l] += 1 + d[r + 1] -= 1 + s = 0 + for x, y in zip(nums, d): + s += y + if x > s: + return False + return True diff --git a/solution/3300-3399/3355.Zero Array Transformation I/Solution.ts b/solution/3300-3399/3355.Zero Array Transformation I/Solution.ts new file mode 100644 index 0000000000000..f0c3b7ebe408b --- /dev/null +++ b/solution/3300-3399/3355.Zero Array Transformation I/Solution.ts @@ -0,0 +1,15 @@ +function isZeroArray(nums: number[], queries: number[][]): boolean { + const n = nums.length; + const d: number[] = Array(n + 1).fill(0); + for (const [l, r] of queries) { + ++d[l]; + --d[r + 1]; + } + for (let i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; +} diff --git a/solution/3300-3399/3356.Zero Array Transformation II/README.md b/solution/3300-3399/3356.Zero Array Transformation II/README.md index a8a80ece93b37..f1a712d8f4e85 100644 --- a/solution/3300-3399/3356.Zero Array Transformation II/README.md +++ b/solution/3300-3399/3356.Zero Array Transformation II/README.md @@ -110,25 +110,166 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3356.Ze #### Python3 ```python - +class Solution: + def minZeroArray(self, nums: List[int], queries: List[List[int]]) -> int: + def check(k: int) -> bool: + d = [0] * (len(nums) + 1) + for l, r, val in queries[:k]: + d[l] += val + d[r + 1] -= val + s = 0 + for x, y in zip(nums, d): + s += y + if x > s: + return False + return True + + m = len(queries) + l = bisect_left(range(m + 1), True, key=check) + return -1 if l > m else l ``` #### Java ```java - +class Solution { + private int n; + private int[] nums; + private int[][] queries; + + public int minZeroArray(int[] nums, int[][] queries) { + this.nums = nums; + this.queries = queries; + n = nums.length; + int m = queries.length; + int l = 0, r = m + 1; + while (l < r) { + int mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; + } + } + return l > m ? -1 : l; + } + + private boolean check(int k) { + int[] d = new int[n + 1]; + for (int i = 0; i < k; ++i) { + int l = queries[i][0], r = queries[i][1], val = queries[i][2]; + d[l] += val; + d[r + 1] -= val; + } + for (int i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minZeroArray(vector& nums, vector>& queries) { + int n = nums.size(); + int d[n + 1]; + int m = queries.size(); + int l = 0, r = m + 1; + auto check = [&](int k) -> bool { + memset(d, 0, sizeof(d)); + for (int i = 0; i < k; ++i) { + int l = queries[i][0], r = queries[i][1], val = queries[i][2]; + d[l] += val; + d[r + 1] -= val; + } + for (int i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; + }; + while (l < r) { + int mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; + } + } + return l > m ? -1 : l; + } +}; ``` #### Go ```go +func minZeroArray(nums []int, queries [][]int) int { + n, m := len(nums), len(queries) + l := sort.Search(m+1, func(k int) bool { + d := make([]int, n+1) + for _, q := range queries[:k] { + l, r, val := q[0], q[1], q[2] + d[l] += val + d[r+1] -= val + } + s := 0 + for i, x := range nums { + s += d[i] + if x > s { + return false + } + } + return true + }) + if l > m { + return -1 + } + return l +} +``` +#### TypeScript + +```ts +function minZeroArray(nums: number[], queries: number[][]): number { + const [n, m] = [nums.length, queries.length]; + const d: number[] = Array(n + 1); + let [l, r] = [0, m + 1]; + const check = (k: number): boolean => { + d.fill(0); + for (let i = 0; i < k; ++i) { + const [l, r, val] = queries[i]; + d[l] += val; + d[r + 1] -= val; + } + for (let i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; + }; + while (l < r) { + const mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; + } + } + return l > m ? -1 : l; +} ``` diff --git a/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md b/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md index 32e3357937f0c..d635064a6edbe 100644 --- a/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md +++ b/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md @@ -108,25 +108,166 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3356.Ze #### Python3 ```python - +class Solution: + def minZeroArray(self, nums: List[int], queries: List[List[int]]) -> int: + def check(k: int) -> bool: + d = [0] * (len(nums) + 1) + for l, r, val in queries[:k]: + d[l] += val + d[r + 1] -= val + s = 0 + for x, y in zip(nums, d): + s += y + if x > s: + return False + return True + + m = len(queries) + l = bisect_left(range(m + 1), True, key=check) + return -1 if l > m else l ``` #### Java ```java - +class Solution { + private int n; + private int[] nums; + private int[][] queries; + + public int minZeroArray(int[] nums, int[][] queries) { + this.nums = nums; + this.queries = queries; + n = nums.length; + int m = queries.length; + int l = 0, r = m + 1; + while (l < r) { + int mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; + } + } + return l > m ? -1 : l; + } + + private boolean check(int k) { + int[] d = new int[n + 1]; + for (int i = 0; i < k; ++i) { + int l = queries[i][0], r = queries[i][1], val = queries[i][2]; + d[l] += val; + d[r + 1] -= val; + } + for (int i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minZeroArray(vector& nums, vector>& queries) { + int n = nums.size(); + int d[n + 1]; + int m = queries.size(); + int l = 0, r = m + 1; + auto check = [&](int k) -> bool { + memset(d, 0, sizeof(d)); + for (int i = 0; i < k; ++i) { + int l = queries[i][0], r = queries[i][1], val = queries[i][2]; + d[l] += val; + d[r + 1] -= val; + } + for (int i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; + }; + while (l < r) { + int mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; + } + } + return l > m ? -1 : l; + } +}; ``` #### Go ```go +func minZeroArray(nums []int, queries [][]int) int { + n, m := len(nums), len(queries) + l := sort.Search(m+1, func(k int) bool { + d := make([]int, n+1) + for _, q := range queries[:k] { + l, r, val := q[0], q[1], q[2] + d[l] += val + d[r+1] -= val + } + s := 0 + for i, x := range nums { + s += d[i] + if x > s { + return false + } + } + return true + }) + if l > m { + return -1 + } + return l +} +``` +#### TypeScript + +```ts +function minZeroArray(nums: number[], queries: number[][]): number { + const [n, m] = [nums.length, queries.length]; + const d: number[] = Array(n + 1); + let [l, r] = [0, m + 1]; + const check = (k: number): boolean => { + d.fill(0); + for (let i = 0; i < k; ++i) { + const [l, r, val] = queries[i]; + d[l] += val; + d[r + 1] -= val; + } + for (let i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; + }; + while (l < r) { + const mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; + } + } + return l > m ? -1 : l; +} ``` diff --git a/solution/3300-3399/3356.Zero Array Transformation II/Solution.cpp b/solution/3300-3399/3356.Zero Array Transformation II/Solution.cpp new file mode 100644 index 0000000000000..4ddc2d5c17cd0 --- /dev/null +++ b/solution/3300-3399/3356.Zero Array Transformation II/Solution.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + int minZeroArray(vector& nums, vector>& queries) { + int n = nums.size(); + int d[n + 1]; + int m = queries.size(); + int l = 0, r = m + 1; + auto check = [&](int k) -> bool { + memset(d, 0, sizeof(d)); + for (int i = 0; i < k; ++i) { + int l = queries[i][0], r = queries[i][1], val = queries[i][2]; + d[l] += val; + d[r + 1] -= val; + } + for (int i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; + }; + while (l < r) { + int mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; + } + } + return l > m ? -1 : l; + } +}; diff --git a/solution/3300-3399/3356.Zero Array Transformation II/Solution.go b/solution/3300-3399/3356.Zero Array Transformation II/Solution.go new file mode 100644 index 0000000000000..9af8f9f0a7abb --- /dev/null +++ b/solution/3300-3399/3356.Zero Array Transformation II/Solution.go @@ -0,0 +1,23 @@ +func minZeroArray(nums []int, queries [][]int) int { + n, m := len(nums), len(queries) + l := sort.Search(m+1, func(k int) bool { + d := make([]int, n+1) + for _, q := range queries[:k] { + l, r, val := q[0], q[1], q[2] + d[l] += val + d[r+1] -= val + } + s := 0 + for i, x := range nums { + s += d[i] + if x > s { + return false + } + } + return true + }) + if l > m { + return -1 + } + return l +} diff --git a/solution/3300-3399/3356.Zero Array Transformation II/Solution.java b/solution/3300-3399/3356.Zero Array Transformation II/Solution.java new file mode 100644 index 0000000000000..951dc57069d94 --- /dev/null +++ b/solution/3300-3399/3356.Zero Array Transformation II/Solution.java @@ -0,0 +1,38 @@ +class Solution { + private int n; + private int[] nums; + private int[][] queries; + + public int minZeroArray(int[] nums, int[][] queries) { + this.nums = nums; + this.queries = queries; + n = nums.length; + int m = queries.length; + int l = 0, r = m + 1; + while (l < r) { + int mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; + } + } + return l > m ? -1 : l; + } + + private boolean check(int k) { + int[] d = new int[n + 1]; + for (int i = 0; i < k; ++i) { + int l = queries[i][0], r = queries[i][1], val = queries[i][2]; + d[l] += val; + d[r + 1] -= val; + } + for (int i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; + } +} diff --git a/solution/3300-3399/3356.Zero Array Transformation II/Solution.py b/solution/3300-3399/3356.Zero Array Transformation II/Solution.py new file mode 100644 index 0000000000000..570af93df7448 --- /dev/null +++ b/solution/3300-3399/3356.Zero Array Transformation II/Solution.py @@ -0,0 +1,17 @@ +class Solution: + def minZeroArray(self, nums: List[int], queries: List[List[int]]) -> int: + def check(k: int) -> bool: + d = [0] * (len(nums) + 1) + for l, r, val in queries[:k]: + d[l] += val + d[r + 1] -= val + s = 0 + for x, y in zip(nums, d): + s += y + if x > s: + return False + return True + + m = len(queries) + l = bisect_left(range(m + 1), True, key=check) + return -1 if l > m else l diff --git a/solution/3300-3399/3356.Zero Array Transformation II/Solution.ts b/solution/3300-3399/3356.Zero Array Transformation II/Solution.ts new file mode 100644 index 0000000000000..071689b67323e --- /dev/null +++ b/solution/3300-3399/3356.Zero Array Transformation II/Solution.ts @@ -0,0 +1,29 @@ +function minZeroArray(nums: number[], queries: number[][]): number { + const [n, m] = [nums.length, queries.length]; + const d: number[] = Array(n + 1); + let [l, r] = [0, m + 1]; + const check = (k: number): boolean => { + d.fill(0); + for (let i = 0; i < k; ++i) { + const [l, r, val] = queries[i]; + d[l] += val; + d[r + 1] -= val; + } + for (let i = 0, s = 0; i < n; ++i) { + s += d[i]; + if (nums[i] > s) { + return false; + } + } + return true; + }; + while (l < r) { + const mid = (l + r) >> 1; + if (check(mid)) { + r = mid; + } else { + l = mid + 1; + } + } + return l > m ? -1 : l; +}