Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.1658,2401 #2578

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
312 changes: 149 additions & 163 deletions solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README.md

Large diffs are not rendered by default.

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
class Solution {
public:
int minOperations(vector<int>& nums, int x) {
x = accumulate(nums.begin(), nums.end(), 0) - x;
unordered_map<int, int> 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<int>& nums, int x) {
int s = accumulate(nums.begin(), nums.end(), 0) - x;
unordered_map<int, int> 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;
}
};
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
class Solution {
public int minOperations(int[] nums, int x) {
x = -x;
for (int v : nums) {
x += v;
}
Map<Integer, Integer> 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<Integer, Integer> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
use std::collections::HashMap;

impl Solution {
pub fn min_operations(nums: Vec<i32>, x: i32) -> i32 {
let n = nums.len();
let target = nums.iter().sum::<i32>() - 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::<i32>() - x;
let mut vis: HashMap<i32, i32> = 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
}
}
Original file line number Diff line number Diff line change
@@ -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<number, number> = 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;
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
class Solution {
public:
int minOperations(vector<int>& 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<int>& 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;
}
};
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
impl Solution {
pub fn min_operations(nums: Vec<i32>, x: i32) -> i32 {
let s: i32 = nums.iter().sum::<i32>() - 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
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
38 changes: 38 additions & 0 deletions solution/2400-2499/2401.Longest Nice Subarray/README.md
Original file line number Diff line number Diff line change
@@ -143,6 +143,44 @@ function longestNiceSubarray(nums: number[]): number {
}
```

```rust
impl Solution {
pub fn longest_nice_subarray(nums: Vec<i32>) -> 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;
}
}
```

<!-- tabs:end -->

<!-- end -->
38 changes: 38 additions & 0 deletions solution/2400-2499/2401.Longest Nice Subarray/README_EN.md
Original file line number Diff line number Diff line change
@@ -141,6 +141,44 @@ function longestNiceSubarray(nums: number[]): number {
}
```

```rust
impl Solution {
pub fn longest_nice_subarray(nums: Vec<i32>) -> 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;
}
}
```

<!-- tabs:end -->

<!-- end -->
13 changes: 13 additions & 0 deletions solution/2400-2499/2401.Longest Nice Subarray/Solution.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
19 changes: 19 additions & 0 deletions solution/2400-2499/2401.Longest Nice Subarray/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
impl Solution {
pub fn longest_nice_subarray(nums: Vec<i32>) -> 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
}
}