diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README.md b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README.md index fd83bf056b8a7..d36c1ce9da921 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README.md +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README.md @@ -51,55 +51,52 @@ ### 方法一:哈希表 + 前缀和 -我们可以将问题转换为求中间连续子数组的最大长度,使得子数组的和为 $x = sum(nums) - x$。 +根据题目描述,我们需要移除数组 $nums$ 左右两端的元素,使得移除的元素和等于 $x$,且移除的元素个数最少。我们可以将问题转化为:找到数组 $nums$ 中最长的连续子数组,使得子数组的和 $s = \sum_{i=0}^{n} nums[i] - x$。这样,我们就可以将问题转化为求解数组 $nums$ 中和为 $s$ 的最长连续子数组的长度 $mx$,答案即为 $n - mx$。 -定义一个哈希表 `vis`,其中 `vis[s]` 表示前缀和为 $s$ 的最小下标。 +我们初始化 $mx = -1$,然后使用哈希表 $vis$ 来存储前缀和,键为前缀和,值为前缀和对应的下标。 -遍历数组 `nums`,对于每个元素 $nums[i]$,我们先将 $nums[i]$ 加到前缀和 $s$ 上,如果哈希表中不存在 $s$,则将其加入哈希表,其值为当前下标 $i$。然后我们判断 $s - x$ 是否在哈希表中,如果存在,则说明存在一个下标 $j$,使得 $nums[j + 1,..i]$ 的和为 $x$,此时我们更新答案的最小值,即 $ans = min(ans, n - (i - j))$。 +遍历数组 $nums$,对于当前元素 $nums[i]$,计算前缀和 $t$,如果 $t$ 不在哈希表中,则将 $t$ 加入哈希表;如果 $t - s$ 在哈希表中,则更新 $mx = \max(mx, i - vis[t - s])$。 -遍历结束,如果找不到满足条件的子数组,返回 $-1$,否则返回 $ans$。 +最后,如果 $mx = -1$,则返回 $-1$,否则返回 $n - mx$。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。 ```python class Solution: def minOperations(self, nums: List[int], x: int) -> int: - x = sum(nums) - x + s = sum(nums) - x vis = {0: -1} - ans = inf - s, n = 0, len(nums) + mx, t = -1, 0 for i, v in enumerate(nums): - s += v - if s not in vis: - vis[s] = i - if s - x in vis: - j = vis[s - x] - ans = min(ans, n - (i - j)) - return -1 if ans == inf else ans + t += v + if t not in vis: + vis[t] = i + if t - s in vis: + mx = max(mx, i - vis[t - s]) + return -1 if mx == -1 else len(nums) - mx ``` ```java class Solution { public int minOperations(int[] nums, int x) { - x = -x; + int s = -x; for (int v : nums) { - x += v; + s += v; } Map vis = new HashMap<>(); vis.put(0, -1); + int mx = -1, t = 0; int n = nums.length; - int ans = 1 << 30; - for (int i = 0, s = 0; i < n; ++i) { - s += nums[i]; - vis.putIfAbsent(s, i); - if (vis.containsKey(s - x)) { - int j = vis.get(s - x); - ans = Math.min(ans, n - (i - j)); + for (int i = 0; i < n; ++i) { + t += nums[i]; + vis.putIfAbsent(t, i); + if (vis.containsKey(t - s)) { + mx = Math.max(mx, i - vis.get(t - s)); } } - return ans == 1 << 30 ? -1 : ans; + return mx == -1 ? -1 : n - mx; } } ``` @@ -108,180 +105,146 @@ class Solution { class Solution { public: int minOperations(vector& nums, int x) { - x = accumulate(nums.begin(), nums.end(), 0) - x; - unordered_map vis{{0, -1}}; + int s = accumulate(nums.begin(), nums.end(), 0) - x; + unordered_map vis = {{0, -1}}; + int mx = -1, t = 0; int n = nums.size(); - int ans = 1 << 30; - for (int i = 0, s = 0; i < n; ++i) { - s += nums[i]; - if (!vis.count(s)) { - vis[s] = i; + for (int i = 0; i < n; ++i) { + t += nums[i]; + if (!vis.contains(t)) { + vis[t] = i; } - if (vis.count(s - x)) { - int j = vis[s - x]; - ans = min(ans, n - (i - j)); + if (vis.contains(t - s)) { + mx = max(mx, i - vis[t - s]); } } - return ans == 1 << 30 ? -1 : ans; + return mx == -1 ? -1 : n - mx; } }; ``` ```go func minOperations(nums []int, x int) int { - x = -x + s := -x for _, v := range nums { - x += v + s += v } vis := map[int]int{0: -1} - ans := 1 << 30 - s, n := 0, len(nums) + mx, t := -1, 0 for i, v := range nums { - s += v - if _, ok := vis[s]; !ok { - vis[s] = i + t += v + if _, ok := vis[t]; !ok { + vis[t] = i } - if j, ok := vis[s-x]; ok { - ans = min(ans, n-(i-j)) + if j, ok := vis[t-s]; ok { + mx = max(mx, i-j) } } - if ans == 1<<30 { + if mx == -1 { return -1 } - return ans + return len(nums) - mx } ``` ```ts function minOperations(nums: number[], x: number): number { - x = nums.reduce((a, b) => a + b, 0) - x; - const vis = new Map(); - vis.set(0, -1); + const s = nums.reduce((acc, cur) => acc + cur, -x); + const vis: Map = new Map([[0, -1]]); + let [mx, t] = [-1, 0]; const n = nums.length; - let ans = 1 << 30; - for (let i = 0, s = 0; i < n; ++i) { - s += nums[i]; - if (!vis.has(s)) { - vis.set(s, i); + for (let i = 0; i < n; ++i) { + t += nums[i]; + if (!vis.has(t)) { + vis.set(t, i); } - if (vis.has(s - x)) { - const j = vis.get(s - x); - ans = Math.min(ans, n - (i - j)); + if (vis.has(t - s)) { + mx = Math.max(mx, i - vis.get(t - s)!); } } - return ans == 1 << 30 ? -1 : ans; + return ~mx ? n - mx : -1; } ``` ```rust +use std::collections::HashMap; + impl Solution { pub fn min_operations(nums: Vec, x: i32) -> i32 { - let n = nums.len(); - let target = nums.iter().sum::() - x; - if target < 0 { - return -1; - } - let mut ans = i32::MAX; - let mut sum = 0; - let mut i = 0; - for j in 0..n { - sum += nums[j]; - while sum > target { - sum -= nums[i]; - i += 1; + let s = nums.iter().sum::() - x; + let mut vis: HashMap = HashMap::new(); + vis.insert(0, -1); + let mut mx = -1; + let mut t = 0; + for (i, v) in nums.iter().enumerate() { + t += v; + if !vis.contains_key(&t) { + vis.insert(t, i as i32); } - if sum == target { - ans = ans.min((n - 1 - (j - i)) as i32); + if let Some(&j) = vis.get(&(t - s)) { + mx = mx.max((i as i32) - j); } } - if ans == i32::MAX { - return -1; + if mx == -1 { + -1 + } else { + (nums.len() as i32) - mx } - ans } } ``` -```c -#define min(a, b) (((a) < (b)) ? (a) : (b)) - -int minOperations(int* nums, int numsSize, int x) { - int target = -x; - for (int i = 0; i < numsSize; i++) { - target += nums[i]; - } - if (target < 0) { - return -1; - } - int ans = INT_MAX; - int sum = 0; - int i = 0; - for (int j = 0; j < numsSize; j++) { - sum += nums[j]; - while (sum > target) { - sum -= nums[i++]; - } - if (sum == target) { - ans = min(ans, numsSize - 1 - (j - i)); - } - } - if (ans == INT_MAX) { - return -1; - } - return ans; -} -``` - ### 方法二:双指针 -与方法一类似,我们要找到一个子数组,使得子数组的和为 $x = sum(nums) - x$。 +基于方法一的分析,我们需要求解数组 $nums$ 中和为 $s$ 的最长连续子数组的长度 $mx$。由于数组 $nums$ 中的元素都是正整数,数组的前缀和只会单调递增,因此我们可以使用双指针来求解。 + +我们初始化指针 $j = 0$,前缀和 $t = 0$,最长连续子数组的长度 $mx = -1$。 -定义两个指针 $j$ 和 $i$,初始时 $i = j = 0$,然后我们向右移动指针 $i$,将 $nums[i]$ 加到前缀和 $s$ 上。如果 $s \gt x$,那么我们循环向右移动指针 $j$,并且将 $nums[j]$ 从前缀和 $s$ 上减去,直到 $s \le x$。如果 $s = x$,我们可以更新答案的最小值,即 $ans = min(ans, n - (i - j + 1))$。继续向右移动指针 $i$,重复上述过程。 +遍历数组 $nums$,对于当前元素 $nums[i]$,计算前缀和 $t += nums[i]$,如果 $t > s$,则移动指针 $j$,直到 $t \leq s$。如果 $t = s$,则更新 $mx = \max(mx, i - j + 1)$。 -最后,如果找不到满足条件的子数组,返回 $-1$,否则返回 $ans$。 +最后,如果 $mx = -1$,则返回 $-1$,否则返回 $n - mx$。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $nums$ 的长度。空间复杂度 $O(1)$。 ```python class Solution: def minOperations(self, nums: List[int], x: int) -> int: - x = sum(nums) - x - ans = inf - n = len(nums) - s = j = 0 - for i, v in enumerate(nums): - s += v - while j <= i and s > x: - s -= nums[j] + s = sum(nums) - x + j = t = 0 + mx = -1 + for i, x in enumerate(nums): + t += x + while j <= i and t > s: + t -= nums[j] j += 1 - if s == x: - ans = min(ans, n - (i - j + 1)) - return -1 if ans == inf else ans + if t == s: + mx = max(mx, i - j + 1) + return -1 if mx == -1 else len(nums) - mx ``` ```java class Solution { public int minOperations(int[] nums, int x) { - x = -x; + int s = -x; for (int v : nums) { - x += v; + s += v; } + int mx = -1, t = 0; int n = nums.length; - int ans = 1 << 30; - for (int i = 0, j = 0, s = 0; i < n; ++i) { - s += nums[i]; - while (j <= i && s > x) { - s -= nums[j++]; + for (int i = 0, j = 0; i < n; ++i) { + t += nums[i]; + while (j <= i && t > s) { + t -= nums[j++]; } - if (s == x) { - ans = Math.min(ans, n - (i - j + 1)); + if (t == s) { + mx = Math.max(mx, i - j + 1); } } - return ans == 1 << 30 ? -1 : ans; + return mx == -1 ? -1 : n - mx; } } ``` @@ -290,64 +253,87 @@ class Solution { class Solution { public: int minOperations(vector& nums, int x) { - x = accumulate(nums.begin(), nums.end(), 0) - x; + int s = accumulate(nums.begin(), nums.end(), 0) - x; + int mx = -1, t = 0; int n = nums.size(); - int ans = 1 << 30; - for (int i = 0, j = 0, s = 0; i < n; ++i) { - s += nums[i]; - while (j <= i && s > x) { - s -= nums[j++]; + for (int i = 0, j = 0; i < n; ++i) { + t += nums[i]; + while (j <= i && t > s) { + t -= nums[j++]; } - if (s == x) { - ans = min(ans, n - (i - j + 1)); + if (t == s) { + mx = max(mx, i - j + 1); } } - return ans == 1 << 30 ? -1 : ans; + return mx == -1 ? -1 : n - mx; } }; ``` ```go func minOperations(nums []int, x int) int { - x = -x + s := -x for _, v := range nums { - x += v + s += v } - ans := 1 << 30 - s, n := 0, len(nums) - j := 0 + mx, t, j := -1, 0, 0 for i, v := range nums { - s += v - for j <= i && s > x { - s -= nums[j] - j++ + t += v + for ; j <= i && t > s; j++ { + t -= nums[j] } - if s == x { - ans = min(ans, n-(i-j+1)) + if t == s { + mx = max(mx, i-j+1) } } - if ans == 1<<30 { + if mx == -1 { return -1 } - return ans + return len(nums) - mx } ``` ```ts function minOperations(nums: number[], x: number): number { - x = nums.reduce((a, b) => a + b, 0) - x; + const s = nums.reduce((acc, cur) => acc + cur, -x); + let [mx, t] = [-1, 0]; const n = nums.length; - let ans = 1 << 30; - for (let i = 0, j = 0, s = 0; i < n; ++i) { - s += nums[i]; - while (j <= i && s > x) { - s -= nums[j++]; + for (let i = 0, j = 0; i < n; ++i) { + t += nums[i]; + while (t > s) { + t -= nums[j++]; + } + if (t === s) { + mx = Math.max(mx, i - j + 1); + } + } + return ~mx ? n - mx : -1; +} +``` + +```rust +impl Solution { + pub fn min_operations(nums: Vec, x: i32) -> i32 { + let s: i32 = nums.iter().sum::() - x; + let mut j: usize = 0; + let mut t: i32 = 0; + let mut mx: i32 = -1; + for (i, &v) in nums.iter().enumerate() { + t += v; + while j <= i && t > s { + t -= nums[j]; + j += 1; + } + if t == s { + mx = mx.max((i - j + 1) as i32); + } } - if (s == x) { - ans = Math.min(ans, n - (i - j + 1)); + if mx == -1 { + -1 + } else { + (nums.len() as i32) - mx } } - return ans == 1 << 30 ? -1 : ans; } ``` diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README_EN.md b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README_EN.md index 02322ac1014f6..8ad4d3e505410 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README_EN.md +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README_EN.md @@ -47,55 +47,52 @@ ### Solution 1: Hash Table + Prefix Sum -We can transform the problem into finding the maximum length of a continuous subarray in the middle, such that the sum of the subarray is $x = sum(nums) - x$. +According to the problem description, we need to remove elements from both ends of the array $nums$ so that the sum of the removed elements equals $x$, and the number of removed elements is minimized. We can transform the problem into: find the longest consecutive subarray in the array $nums$ such that the sum of the subarray $s = \sum_{i=0}^{n} nums[i] - x$. In this way, we can transform the problem into finding the length $mx$ of the longest consecutive subarray in the array $nums$ with a sum of $s$, and the answer is $n - mx$. -Define a hash table `vis`, where `vis[s]` represents the smallest index with a prefix sum of $s$. +We initialize $mx = -1$, and then use a hash table $vis$ to store the prefix sum, where the key is the prefix sum and the value is the index corresponding to the prefix sum. -Traverse the array `nums`. For each element $nums[i]$, we first add $nums[i]$ to the prefix sum $s$. If $s$ does not exist in the hash table, we add it to the hash table, and its value is the current index $i$. Then we check whether $s - x$ exists in the hash table. If it does, it means that there exists an index $j$ such that the sum of $nums[j + 1,..i]$ is $x$. At this time, we update the minimum value of the answer, that is, $ans = min(ans, n - (i - j))$. +Traverse the array $nums$, for the current element $nums[i]$, calculate the prefix sum $t$, if $t$ is not in the hash table, add $t$ to the hash table; if $t - s$ is in the hash table, update $mx = \max(mx, i - vis[t - s])$. -At the end of the traversal, if no subarray meets the condition, return $-1$, otherwise return $ans$. +Finally, if $mx = -1$, return $-1$, otherwise return $n - mx$. -The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array `nums`. +The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $nums$. ```python class Solution: def minOperations(self, nums: List[int], x: int) -> int: - x = sum(nums) - x + s = sum(nums) - x vis = {0: -1} - ans = inf - s, n = 0, len(nums) + mx, t = -1, 0 for i, v in enumerate(nums): - s += v - if s not in vis: - vis[s] = i - if s - x in vis: - j = vis[s - x] - ans = min(ans, n - (i - j)) - return -1 if ans == inf else ans + t += v + if t not in vis: + vis[t] = i + if t - s in vis: + mx = max(mx, i - vis[t - s]) + return -1 if mx == -1 else len(nums) - mx ``` ```java class Solution { public int minOperations(int[] nums, int x) { - x = -x; + int s = -x; for (int v : nums) { - x += v; + s += v; } Map vis = new HashMap<>(); vis.put(0, -1); + int mx = -1, t = 0; int n = nums.length; - int ans = 1 << 30; - for (int i = 0, s = 0; i < n; ++i) { - s += nums[i]; - vis.putIfAbsent(s, i); - if (vis.containsKey(s - x)) { - int j = vis.get(s - x); - ans = Math.min(ans, n - (i - j)); + for (int i = 0; i < n; ++i) { + t += nums[i]; + vis.putIfAbsent(t, i); + if (vis.containsKey(t - s)) { + mx = Math.max(mx, i - vis.get(t - s)); } } - return ans == 1 << 30 ? -1 : ans; + return mx == -1 ? -1 : n - mx; } } ``` @@ -104,180 +101,146 @@ class Solution { class Solution { public: int minOperations(vector& nums, int x) { - x = accumulate(nums.begin(), nums.end(), 0) - x; - unordered_map vis{{0, -1}}; + int s = accumulate(nums.begin(), nums.end(), 0) - x; + unordered_map vis = {{0, -1}}; + int mx = -1, t = 0; int n = nums.size(); - int ans = 1 << 30; - for (int i = 0, s = 0; i < n; ++i) { - s += nums[i]; - if (!vis.count(s)) { - vis[s] = i; + for (int i = 0; i < n; ++i) { + t += nums[i]; + if (!vis.contains(t)) { + vis[t] = i; } - if (vis.count(s - x)) { - int j = vis[s - x]; - ans = min(ans, n - (i - j)); + if (vis.contains(t - s)) { + mx = max(mx, i - vis[t - s]); } } - return ans == 1 << 30 ? -1 : ans; + return mx == -1 ? -1 : n - mx; } }; ``` ```go func minOperations(nums []int, x int) int { - x = -x + s := -x for _, v := range nums { - x += v + s += v } vis := map[int]int{0: -1} - ans := 1 << 30 - s, n := 0, len(nums) + mx, t := -1, 0 for i, v := range nums { - s += v - if _, ok := vis[s]; !ok { - vis[s] = i + t += v + if _, ok := vis[t]; !ok { + vis[t] = i } - if j, ok := vis[s-x]; ok { - ans = min(ans, n-(i-j)) + if j, ok := vis[t-s]; ok { + mx = max(mx, i-j) } } - if ans == 1<<30 { + if mx == -1 { return -1 } - return ans + return len(nums) - mx } ``` ```ts function minOperations(nums: number[], x: number): number { - x = nums.reduce((a, b) => a + b, 0) - x; - const vis = new Map(); - vis.set(0, -1); + const s = nums.reduce((acc, cur) => acc + cur, -x); + const vis: Map = new Map([[0, -1]]); + let [mx, t] = [-1, 0]; const n = nums.length; - let ans = 1 << 30; - for (let i = 0, s = 0; i < n; ++i) { - s += nums[i]; - if (!vis.has(s)) { - vis.set(s, i); + for (let i = 0; i < n; ++i) { + t += nums[i]; + if (!vis.has(t)) { + vis.set(t, i); } - if (vis.has(s - x)) { - const j = vis.get(s - x); - ans = Math.min(ans, n - (i - j)); + if (vis.has(t - s)) { + mx = Math.max(mx, i - vis.get(t - s)!); } } - return ans == 1 << 30 ? -1 : ans; + return ~mx ? n - mx : -1; } ``` ```rust +use std::collections::HashMap; + impl Solution { pub fn min_operations(nums: Vec, x: i32) -> i32 { - let n = nums.len(); - let target = nums.iter().sum::() - x; - if target < 0 { - return -1; - } - let mut ans = i32::MAX; - let mut sum = 0; - let mut i = 0; - for j in 0..n { - sum += nums[j]; - while sum > target { - sum -= nums[i]; - i += 1; + let s = nums.iter().sum::() - x; + let mut vis: HashMap = HashMap::new(); + vis.insert(0, -1); + let mut mx = -1; + let mut t = 0; + for (i, v) in nums.iter().enumerate() { + t += v; + if !vis.contains_key(&t) { + vis.insert(t, i as i32); } - if sum == target { - ans = ans.min((n - 1 - (j - i)) as i32); + if let Some(&j) = vis.get(&(t - s)) { + mx = mx.max((i as i32) - j); } } - if ans == i32::MAX { - return -1; + if mx == -1 { + -1 + } else { + (nums.len() as i32) - mx } - ans } } ``` -```c -#define min(a, b) (((a) < (b)) ? (a) : (b)) - -int minOperations(int* nums, int numsSize, int x) { - int target = -x; - for (int i = 0; i < numsSize; i++) { - target += nums[i]; - } - if (target < 0) { - return -1; - } - int ans = INT_MAX; - int sum = 0; - int i = 0; - for (int j = 0; j < numsSize; j++) { - sum += nums[j]; - while (sum > target) { - sum -= nums[i++]; - } - if (sum == target) { - ans = min(ans, numsSize - 1 - (j - i)); - } - } - if (ans == INT_MAX) { - return -1; - } - return ans; -} -``` - ### Solution 2: Two Pointers -Similar to Solution 1, we need to find a subarray such that the sum of the subarray is $x = sum(nums) - x$. +Based on the analysis of Solution 1, we need to find the length $mx$ of the longest consecutive subarray in the array $nums$ with a sum of $s$. Since all elements in the array $nums$ are positive integers, the prefix sum of the array will only increase monotonically, so we can use two pointers to solve this problem. + +We initialize pointer $j = 0$, prefix sum $t = 0$, and the length of the longest consecutive subarray $mx = -1$. -Define two pointers $j$ and $i$, initially $i = j = 0$. Then we move the pointer $i$ to the right, add $nums[i]$ to the prefix sum $s$. If $s > x$, then we move the pointer $j$ to the right in a loop, and subtract $nums[j]$ from the prefix sum $s$, until $s \le x$. If $s = x$, we can update the minimum value of the answer, that is, $ans = min(ans, n - (i - j + 1))$. Continue to move the pointer $i$ to the right, and repeat the above process. +Traverse the array $nums$, for the current element $nums[i]$, calculate the prefix sum $t += nums[i]$. If $t > s$, then move the pointer $j$ until $t \leq s$. If $t = s$, then update $mx = \max(mx, i - j + 1)$. -Finally, if no subarray meets the condition, return $-1$, otherwise return $ans$. +Finally, if $mx = -1$, return $-1$, otherwise return $n - mx$. -The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array `nums`. +The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$. ```python class Solution: def minOperations(self, nums: List[int], x: int) -> int: - x = sum(nums) - x - ans = inf - n = len(nums) - s = j = 0 - for i, v in enumerate(nums): - s += v - while j <= i and s > x: - s -= nums[j] + s = sum(nums) - x + j = t = 0 + mx = -1 + for i, x in enumerate(nums): + t += x + while j <= i and t > s: + t -= nums[j] j += 1 - if s == x: - ans = min(ans, n - (i - j + 1)) - return -1 if ans == inf else ans + if t == s: + mx = max(mx, i - j + 1) + return -1 if mx == -1 else len(nums) - mx ``` ```java class Solution { public int minOperations(int[] nums, int x) { - x = -x; + int s = -x; for (int v : nums) { - x += v; + s += v; } + int mx = -1, t = 0; int n = nums.length; - int ans = 1 << 30; - for (int i = 0, j = 0, s = 0; i < n; ++i) { - s += nums[i]; - while (j <= i && s > x) { - s -= nums[j++]; + for (int i = 0, j = 0; i < n; ++i) { + t += nums[i]; + while (j <= i && t > s) { + t -= nums[j++]; } - if (s == x) { - ans = Math.min(ans, n - (i - j + 1)); + if (t == s) { + mx = Math.max(mx, i - j + 1); } } - return ans == 1 << 30 ? -1 : ans; + return mx == -1 ? -1 : n - mx; } } ``` @@ -286,64 +249,87 @@ class Solution { class Solution { public: int minOperations(vector& nums, int x) { - x = accumulate(nums.begin(), nums.end(), 0) - x; + int s = accumulate(nums.begin(), nums.end(), 0) - x; + int mx = -1, t = 0; int n = nums.size(); - int ans = 1 << 30; - for (int i = 0, j = 0, s = 0; i < n; ++i) { - s += nums[i]; - while (j <= i && s > x) { - s -= nums[j++]; + for (int i = 0, j = 0; i < n; ++i) { + t += nums[i]; + while (j <= i && t > s) { + t -= nums[j++]; } - if (s == x) { - ans = min(ans, n - (i - j + 1)); + if (t == s) { + mx = max(mx, i - j + 1); } } - return ans == 1 << 30 ? -1 : ans; + return mx == -1 ? -1 : n - mx; } }; ``` ```go func minOperations(nums []int, x int) int { - x = -x + s := -x for _, v := range nums { - x += v + s += v } - ans := 1 << 30 - s, n := 0, len(nums) - j := 0 + mx, t, j := -1, 0, 0 for i, v := range nums { - s += v - for j <= i && s > x { - s -= nums[j] - j++ + t += v + for ; j <= i && t > s; j++ { + t -= nums[j] } - if s == x { - ans = min(ans, n-(i-j+1)) + if t == s { + mx = max(mx, i-j+1) } } - if ans == 1<<30 { + if mx == -1 { return -1 } - return ans + return len(nums) - mx } ``` ```ts function minOperations(nums: number[], x: number): number { - x = nums.reduce((a, b) => a + b, 0) - x; + const s = nums.reduce((acc, cur) => acc + cur, -x); + let [mx, t] = [-1, 0]; const n = nums.length; - let ans = 1 << 30; - for (let i = 0, j = 0, s = 0; i < n; ++i) { - s += nums[i]; - while (j <= i && s > x) { - s -= nums[j++]; + for (let i = 0, j = 0; i < n; ++i) { + t += nums[i]; + while (t > s) { + t -= nums[j++]; + } + if (t === s) { + mx = Math.max(mx, i - j + 1); + } + } + return ~mx ? n - mx : -1; +} +``` + +```rust +impl Solution { + pub fn min_operations(nums: Vec, x: i32) -> i32 { + let s: i32 = nums.iter().sum::() - x; + let mut j: usize = 0; + let mut t: i32 = 0; + let mut mx: i32 = -1; + for (i, &v) in nums.iter().enumerate() { + t += v; + while j <= i && t > s { + t -= nums[j]; + j += 1; + } + if t == s { + mx = mx.max((i - j + 1) as i32); + } } - if (s == x) { - ans = Math.min(ans, n - (i - j + 1)); + if mx == -1 { + -1 + } else { + (nums.len() as i32) - mx } } - return ans == 1 << 30 ? -1 : ans; } ``` diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.c b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.c deleted file mode 100644 index 386b6de7e2955..0000000000000 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.c +++ /dev/null @@ -1,27 +0,0 @@ -#define min(a, b) (((a) < (b)) ? (a) : (b)) - -int minOperations(int* nums, int numsSize, int x) { - int target = -x; - for (int i = 0; i < numsSize; i++) { - target += nums[i]; - } - if (target < 0) { - return -1; - } - int ans = INT_MAX; - int sum = 0; - int i = 0; - for (int j = 0; j < numsSize; j++) { - sum += nums[j]; - while (sum > target) { - sum -= nums[i++]; - } - if (sum == target) { - ans = min(ans, numsSize - 1 - (j - i)); - } - } - if (ans == INT_MAX) { - return -1; - } - return ans; -} \ No newline at end of file diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.cpp b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.cpp index 62bf7d0114bdf..cb02c0d3199e0 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.cpp +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.cpp @@ -1,20 +1,19 @@ -class Solution { -public: - int minOperations(vector& nums, int x) { - x = accumulate(nums.begin(), nums.end(), 0) - x; - unordered_map vis{{0, -1}}; - int n = nums.size(); - int ans = 1 << 30; - for (int i = 0, s = 0; i < n; ++i) { - s += nums[i]; - if (!vis.count(s)) { - vis[s] = i; - } - if (vis.count(s - x)) { - int j = vis[s - x]; - ans = min(ans, n - (i - j)); - } - } - return ans == 1 << 30 ? -1 : ans; - } +class Solution { +public: + int minOperations(vector& nums, int x) { + int s = accumulate(nums.begin(), nums.end(), 0) - x; + unordered_map vis = {{0, -1}}; + int mx = -1, t = 0; + int n = nums.size(); + for (int i = 0; i < n; ++i) { + t += nums[i]; + if (!vis.contains(t)) { + vis[t] = i; + } + if (vis.contains(t - s)) { + mx = max(mx, i - vis[t - s]); + } + } + return mx == -1 ? -1 : n - mx; + } }; \ No newline at end of file diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.go b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.go index df468c39f1462..847930dbf7dea 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.go +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.go @@ -1,22 +1,21 @@ -func minOperations(nums []int, x int) int { - x = -x - for _, v := range nums { - x += v - } - vis := map[int]int{0: -1} - ans := 1 << 30 - s, n := 0, len(nums) - for i, v := range nums { - s += v - if _, ok := vis[s]; !ok { - vis[s] = i - } - if j, ok := vis[s-x]; ok { - ans = min(ans, n-(i-j)) - } - } - if ans == 1<<30 { - return -1 - } - return ans +func minOperations(nums []int, x int) int { + s := -x + for _, v := range nums { + s += v + } + vis := map[int]int{0: -1} + mx, t := -1, 0 + for i, v := range nums { + t += v + if _, ok := vis[t]; !ok { + vis[t] = i + } + if j, ok := vis[t-s]; ok { + mx = max(mx, i-j) + } + } + if mx == -1 { + return -1 + } + return len(nums) - mx } \ No newline at end of file diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.java b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.java index 1fda2dcf11a25..e1f70f14d9f47 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.java +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.java @@ -1,21 +1,20 @@ -class Solution { - public int minOperations(int[] nums, int x) { - x = -x; - for (int v : nums) { - x += v; - } - Map vis = new HashMap<>(); - vis.put(0, -1); - int n = nums.length; - int ans = 1 << 30; - for (int i = 0, s = 0; i < n; ++i) { - s += nums[i]; - vis.putIfAbsent(s, i); - if (vis.containsKey(s - x)) { - int j = vis.get(s - x); - ans = Math.min(ans, n - (i - j)); - } - } - return ans == 1 << 30 ? -1 : ans; - } +class Solution { + public int minOperations(int[] nums, int x) { + int s = -x; + for (int v : nums) { + s += v; + } + Map vis = new HashMap<>(); + vis.put(0, -1); + int mx = -1, t = 0; + int n = nums.length; + for (int i = 0; i < n; ++i) { + t += nums[i]; + vis.putIfAbsent(t, i); + if (vis.containsKey(t - s)) { + mx = Math.max(mx, i - vis.get(t - s)); + } + } + return mx == -1 ? -1 : n - mx; + } } \ No newline at end of file diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.py b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.py index a124eb004a18f..22a0bddb50450 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.py +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.py @@ -1,14 +1,12 @@ -class Solution: - def minOperations(self, nums: List[int], x: int) -> int: - x = sum(nums) - x - vis = {0: -1} - ans = inf - s, n = 0, len(nums) - for i, v in enumerate(nums): - s += v - if s not in vis: - vis[s] = i - if s - x in vis: - j = vis[s - x] - ans = min(ans, n - (i - j)) - return -1 if ans == inf else ans +class Solution: + def minOperations(self, nums: List[int], x: int) -> int: + s = sum(nums) - x + vis = {0: -1} + mx, t = -1, 0 + for i, v in enumerate(nums): + t += v + if t not in vis: + vis[t] = i + if t - s in vis: + mx = max(mx, i - vis[t - s]) + return -1 if mx == -1 else len(nums) - mx diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.rs b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.rs index b61d6c1af0b9d..8dd1e95030374 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.rs +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.rs @@ -1,26 +1,25 @@ +use std::collections::HashMap; + impl Solution { pub fn min_operations(nums: Vec, x: i32) -> i32 { - let n = nums.len(); - let target = nums.iter().sum::() - x; - if target < 0 { - return -1; - } - let mut ans = i32::MAX; - let mut sum = 0; - let mut i = 0; - for j in 0..n { - sum += nums[j]; - while sum > target { - sum -= nums[i]; - i += 1; + let s = nums.iter().sum::() - x; + let mut vis: HashMap = HashMap::new(); + vis.insert(0, -1); + let mut mx = -1; + let mut t = 0; + for (i, v) in nums.iter().enumerate() { + t += v; + if !vis.contains_key(&t) { + vis.insert(t, i as i32); } - if sum == target { - ans = ans.min((n - 1 - (j - i)) as i32); + if let Some(&j) = vis.get(&(t - s)) { + mx = mx.max((i as i32) - j); } } - if ans == i32::MAX { - return -1; + if mx == -1 { + -1 + } else { + (nums.len() as i32) - mx } - ans } } diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.ts b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.ts index 42fd0c850ffb5..94f61a95ed74e 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.ts +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.ts @@ -1,18 +1,16 @@ function minOperations(nums: number[], x: number): number { - x = nums.reduce((a, b) => a + b, 0) - x; - const vis = new Map(); - vis.set(0, -1); + const s = nums.reduce((acc, cur) => acc + cur, -x); + const vis: Map = new Map([[0, -1]]); + let [mx, t] = [-1, 0]; const n = nums.length; - let ans = 1 << 30; - for (let i = 0, s = 0; i < n; ++i) { - s += nums[i]; - if (!vis.has(s)) { - vis.set(s, i); + for (let i = 0; i < n; ++i) { + t += nums[i]; + if (!vis.has(t)) { + vis.set(t, i); } - if (vis.has(s - x)) { - const j = vis.get(s - x); - ans = Math.min(ans, n - (i - j)); + if (vis.has(t - s)) { + mx = Math.max(mx, i - vis.get(t - s)!); } } - return ans == 1 << 30 ? -1 : ans; + return ~mx ? n - mx : -1; } diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.cpp b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.cpp index 53511235a5b74..f082f1d0cd003 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.cpp +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.cpp @@ -1,18 +1,18 @@ -class Solution { -public: - int minOperations(vector& nums, int x) { - x = accumulate(nums.begin(), nums.end(), 0) - x; - int n = nums.size(); - int ans = 1 << 30; - for (int i = 0, j = 0, s = 0; i < n; ++i) { - s += nums[i]; - while (j <= i && s > x) { - s -= nums[j++]; - } - if (s == x) { - ans = min(ans, n - (i - j + 1)); - } - } - return ans == 1 << 30 ? -1 : ans; - } +class Solution { +public: + int minOperations(vector& nums, int x) { + int s = accumulate(nums.begin(), nums.end(), 0) - x; + int mx = -1, t = 0; + int n = nums.size(); + for (int i = 0, j = 0; i < n; ++i) { + t += nums[i]; + while (j <= i && t > s) { + t -= nums[j++]; + } + if (t == s) { + mx = max(mx, i - j + 1); + } + } + return mx == -1 ? -1 : n - mx; + } }; \ No newline at end of file diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.go b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.go index e900da722c758..3e937ee615df5 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.go +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.go @@ -1,23 +1,20 @@ -func minOperations(nums []int, x int) int { - x = -x - for _, v := range nums { - x += v - } - ans := 1 << 30 - s, n := 0, len(nums) - j := 0 - for i, v := range nums { - s += v - for j <= i && s > x { - s -= nums[j] - j++ - } - if s == x { - ans = min(ans, n-(i-j+1)) - } - } - if ans == 1<<30 { - return -1 - } - return ans +func minOperations(nums []int, x int) int { + s := -x + for _, v := range nums { + s += v + } + mx, t, j := -1, 0, 0 + for i, v := range nums { + t += v + for ; j <= i && t > s; j++ { + t -= nums[j] + } + if t == s { + mx = max(mx, i-j+1) + } + } + if mx == -1 { + return -1 + } + return len(nums) - mx } \ No newline at end of file diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.java b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.java index 15902f37cef3e..4f3a39acfd2d1 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.java +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.java @@ -1,20 +1,20 @@ -class Solution { - public int minOperations(int[] nums, int x) { - x = -x; - for (int v : nums) { - x += v; - } - int n = nums.length; - int ans = 1 << 30; - for (int i = 0, j = 0, s = 0; i < n; ++i) { - s += nums[i]; - while (j <= i && s > x) { - s -= nums[j++]; - } - if (s == x) { - ans = Math.min(ans, n - (i - j + 1)); - } - } - return ans == 1 << 30 ? -1 : ans; - } +class Solution { + public int minOperations(int[] nums, int x) { + int s = -x; + for (int v : nums) { + s += v; + } + int mx = -1, t = 0; + int n = nums.length; + for (int i = 0, j = 0; i < n; ++i) { + t += nums[i]; + while (j <= i && t > s) { + t -= nums[j++]; + } + if (t == s) { + mx = Math.max(mx, i - j + 1); + } + } + return mx == -1 ? -1 : n - mx; + } } \ No newline at end of file diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.py b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.py index 38e18fc805418..ce772e9115043 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.py +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.py @@ -1,14 +1,13 @@ -class Solution: - def minOperations(self, nums: List[int], x: int) -> int: - x = sum(nums) - x - ans = inf - n = len(nums) - s = j = 0 - for i, v in enumerate(nums): - s += v - while j <= i and s > x: - s -= nums[j] - j += 1 - if s == x: - ans = min(ans, n - (i - j + 1)) - return -1 if ans == inf else ans +class Solution: + def minOperations(self, nums: List[int], x: int) -> int: + s = sum(nums) - x + j = t = 0 + mx = -1 + for i, x in enumerate(nums): + t += x + while j <= i and t > s: + t -= nums[j] + j += 1 + if t == s: + mx = max(mx, i - j + 1) + return -1 if mx == -1 else len(nums) - mx diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.rs b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.rs new file mode 100644 index 0000000000000..33be558a92a24 --- /dev/null +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.rs @@ -0,0 +1,23 @@ +impl Solution { + pub fn min_operations(nums: Vec, x: i32) -> i32 { + let s: i32 = nums.iter().sum::() - x; + let mut j: usize = 0; + let mut t: i32 = 0; + let mut mx: i32 = -1; + for (i, &v) in nums.iter().enumerate() { + t += v; + while j <= i && t > s { + t -= nums[j]; + j += 1; + } + if t == s { + mx = mx.max((i - j + 1) as i32); + } + } + if mx == -1 { + -1 + } else { + (nums.len() as i32) - mx + } + } +} diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.ts b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.ts index 8dfb6bc212caf..e4de9c271d037 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.ts +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution2.ts @@ -1,15 +1,15 @@ function minOperations(nums: number[], x: number): number { - x = nums.reduce((a, b) => a + b, 0) - x; + const s = nums.reduce((acc, cur) => acc + cur, -x); + let [mx, t] = [-1, 0]; const n = nums.length; - let ans = 1 << 30; - for (let i = 0, j = 0, s = 0; i < n; ++i) { - s += nums[i]; - while (j <= i && s > x) { - s -= nums[j++]; + for (let i = 0, j = 0; i < n; ++i) { + t += nums[i]; + while (t > s) { + t -= nums[j++]; } - if (s == x) { - ans = Math.min(ans, n - (i - j + 1)); + if (t === s) { + mx = Math.max(mx, i - j + 1); } } - return ans == 1 << 30 ? -1 : ans; + return ~mx ? n - mx : -1; } diff --git a/solution/2400-2499/2401.Longest Nice Subarray/README.md b/solution/2400-2499/2401.Longest Nice Subarray/README.md index 52ff0ed745de0..66dd523736d8c 100644 --- a/solution/2400-2499/2401.Longest Nice Subarray/README.md +++ b/solution/2400-2499/2401.Longest Nice Subarray/README.md @@ -143,6 +143,44 @@ function longestNiceSubarray(nums: number[]): number { } ``` +```rust +impl Solution { + pub fn longest_nice_subarray(nums: Vec) -> i32 { + let mut ans = 0; + let mut mask = 0; + let mut j = 0; + + for (i, &x) in nums.iter().enumerate() { + let mut x = x; + while (mask & x) != 0 { + mask ^= nums[j]; + j += 1; + } + ans = ans.max(i - j + 1); + mask |= x; + } + + ans as i32 + } +} +``` + +```cs +public class Solution { + public int LongestNiceSubarray(int[] nums) { + int ans = 0, mask = 0; + for (int i = 0, j = 0; i < nums.Length; ++i) { + while ((mask & nums[i]) != 0) { + mask ^= nums[j++]; + } + ans = Math.Max(ans, i - j + 1); + mask |= nums[i]; + } + return ans; + } +} +``` + diff --git a/solution/2400-2499/2401.Longest Nice Subarray/README_EN.md b/solution/2400-2499/2401.Longest Nice Subarray/README_EN.md index be4a238db9add..905fec762e044 100644 --- a/solution/2400-2499/2401.Longest Nice Subarray/README_EN.md +++ b/solution/2400-2499/2401.Longest Nice Subarray/README_EN.md @@ -141,6 +141,44 @@ function longestNiceSubarray(nums: number[]): number { } ``` +```rust +impl Solution { + pub fn longest_nice_subarray(nums: Vec) -> i32 { + let mut ans = 0; + let mut mask = 0; + let mut j = 0; + + for (i, &x) in nums.iter().enumerate() { + let mut x = x; + while (mask & x) != 0 { + mask ^= nums[j]; + j += 1; + } + ans = ans.max(i - j + 1); + mask |= x; + } + + ans as i32 + } +} +``` + +```cs +public class Solution { + public int LongestNiceSubarray(int[] nums) { + int ans = 0, mask = 0; + for (int i = 0, j = 0; i < nums.Length; ++i) { + while ((mask & nums[i]) != 0) { + mask ^= nums[j++]; + } + ans = Math.Max(ans, i - j + 1); + mask |= nums[i]; + } + return ans; + } +} +``` + diff --git a/solution/2400-2499/2401.Longest Nice Subarray/Solution.cs b/solution/2400-2499/2401.Longest Nice Subarray/Solution.cs new file mode 100644 index 0000000000000..cc3e51f577fd3 --- /dev/null +++ b/solution/2400-2499/2401.Longest Nice Subarray/Solution.cs @@ -0,0 +1,13 @@ +public class Solution { + public int LongestNiceSubarray(int[] nums) { + int ans = 0, mask = 0; + for (int i = 0, j = 0; i < nums.Length; ++i) { + while ((mask & nums[i]) != 0) { + mask ^= nums[j++]; + } + ans = Math.Max(ans, i - j + 1); + mask |= nums[i]; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2400-2499/2401.Longest Nice Subarray/Solution.rs b/solution/2400-2499/2401.Longest Nice Subarray/Solution.rs new file mode 100644 index 0000000000000..3f993606f8a90 --- /dev/null +++ b/solution/2400-2499/2401.Longest Nice Subarray/Solution.rs @@ -0,0 +1,19 @@ +impl Solution { + pub fn longest_nice_subarray(nums: Vec) -> i32 { + let mut ans = 0; + let mut mask = 0; + let mut j = 0; + + for (i, &x) in nums.iter().enumerate() { + let mut x = x; + while (mask & x) != 0 { + mask ^= nums[j]; + j += 1; + } + ans = ans.max(i - j + 1); + mask |= x; + } + + ans as i32 + } +}