File tree 19 files changed +602
-538
lines changed
1600-1699/1658.Minimum Operations to Reduce X to Zero
2400-2499/2401.Longest Nice Subarray
19 files changed +602
-538
lines changed Load Diff Large diffs are not rendered by default.
Load Diff Large diffs are not rendered by default.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
- class Solution {
2
- public:
3
- int minOperations (vector<int >& nums, int x) {
4
- x = accumulate (nums.begin (), nums.end (), 0 ) - x;
5
- unordered_map<int , int > vis{{0 , -1 }};
6
- int n = nums.size ();
7
- int ans = 1 << 30 ;
8
- for (int i = 0 , s = 0 ; i < n; ++i) {
9
- s += nums[i];
10
- if (!vis.count (s)) {
11
- vis[s] = i;
12
- }
13
- if (vis.count (s - x)) {
14
- int j = vis[s - x];
15
- ans = min (ans, n - (i - j));
16
- }
17
- }
18
- return ans == 1 << 30 ? -1 : ans;
19
- }
1
+ class Solution {
2
+ public:
3
+ int minOperations (vector<int >& nums, int x) {
4
+ int s = accumulate (nums.begin (), nums.end (), 0 ) - x;
5
+ unordered_map<int , int > vis = {{0 , -1 }};
6
+ int mx = -1 , t = 0 ;
7
+ int n = nums.size ();
8
+ for (int i = 0 ; i < n; ++i) {
9
+ t += nums[i];
10
+ if (!vis.contains (t)) {
11
+ vis[t] = i;
12
+ }
13
+ if (vis.contains (t - s)) {
14
+ mx = max (mx, i - vis[t - s]);
15
+ }
16
+ }
17
+ return mx == -1 ? -1 : n - mx;
18
+ }
20
19
};
Original file line number Diff line number Diff line change 1
- func minOperations (nums []int , x int ) int {
2
- x = - x
3
- for _ , v := range nums {
4
- x += v
5
- }
6
- vis := map [int ]int {0 : - 1 }
7
- ans := 1 << 30
8
- s , n := 0 , len (nums )
9
- for i , v := range nums {
10
- s += v
11
- if _ , ok := vis [s ]; ! ok {
12
- vis [s ] = i
13
- }
14
- if j , ok := vis [s - x ]; ok {
15
- ans = min (ans , n - (i - j ))
16
- }
17
- }
18
- if ans == 1 << 30 {
19
- return - 1
20
- }
21
- return ans
1
+ func minOperations (nums []int , x int ) int {
2
+ s := - x
3
+ for _ , v := range nums {
4
+ s += v
5
+ }
6
+ vis := map [int ]int {0 : - 1 }
7
+ mx , t := - 1 , 0
8
+ for i , v := range nums {
9
+ t += v
10
+ if _ , ok := vis [t ]; ! ok {
11
+ vis [t ] = i
12
+ }
13
+ if j , ok := vis [t - s ]; ok {
14
+ mx = max (mx , i - j )
15
+ }
16
+ }
17
+ if mx == - 1 {
18
+ return - 1
19
+ }
20
+ return len (nums ) - mx
22
21
}
Original file line number Diff line number Diff line change 1
- class Solution {
2
- public int minOperations (int [] nums , int x ) {
3
- x = -x ;
4
- for (int v : nums ) {
5
- x += v ;
6
- }
7
- Map <Integer , Integer > vis = new HashMap <>();
8
- vis .put (0 , -1 );
9
- int n = nums .length ;
10
- int ans = 1 << 30 ;
11
- for (int i = 0 , s = 0 ; i < n ; ++i ) {
12
- s += nums [i ];
13
- vis .putIfAbsent (s , i );
14
- if (vis .containsKey (s - x )) {
15
- int j = vis .get (s - x );
16
- ans = Math .min (ans , n - (i - j ));
17
- }
18
- }
19
- return ans == 1 << 30 ? -1 : ans ;
20
- }
1
+ class Solution {
2
+ public int minOperations (int [] nums , int x ) {
3
+ int s = -x ;
4
+ for (int v : nums ) {
5
+ s += v ;
6
+ }
7
+ Map <Integer , Integer > vis = new HashMap <>();
8
+ vis .put (0 , -1 );
9
+ int mx = -1 , t = 0 ;
10
+ int n = nums .length ;
11
+ for (int i = 0 ; i < n ; ++i ) {
12
+ t += nums [i ];
13
+ vis .putIfAbsent (t , i );
14
+ if (vis .containsKey (t - s )) {
15
+ mx = Math .max (mx , i - vis .get (t - s ));
16
+ }
17
+ }
18
+ return mx == -1 ? -1 : n - mx ;
19
+ }
21
20
}
Original file line number Diff line number Diff line change 1
- class Solution :
2
- def minOperations (self , nums : List [int ], x : int ) -> int :
3
- x = sum (nums ) - x
4
- vis = {0 : - 1 }
5
- ans = inf
6
- s , n = 0 , len (nums )
7
- for i , v in enumerate (nums ):
8
- s += v
9
- if s not in vis :
10
- vis [s ] = i
11
- if s - x in vis :
12
- j = vis [s - x ]
13
- ans = min (ans , n - (i - j ))
14
- return - 1 if ans == inf else ans
1
+ class Solution :
2
+ def minOperations (self , nums : List [int ], x : int ) -> int :
3
+ s = sum (nums ) - x
4
+ vis = {0 : - 1 }
5
+ mx , t = - 1 , 0
6
+ for i , v in enumerate (nums ):
7
+ t += v
8
+ if t not in vis :
9
+ vis [t ] = i
10
+ if t - s in vis :
11
+ mx = max (mx , i - vis [t - s ])
12
+ return - 1 if mx == - 1 else len (nums ) - mx
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashMap ;
2
+
1
3
impl Solution {
2
4
pub fn min_operations ( nums : Vec < i32 > , x : i32 ) -> i32 {
3
- let n = nums. len ( ) ;
4
- let target = nums. iter ( ) . sum :: < i32 > ( ) - x;
5
- if target < 0 {
6
- return -1 ;
7
- }
8
- let mut ans = i32:: MAX ;
9
- let mut sum = 0 ;
10
- let mut i = 0 ;
11
- for j in 0 ..n {
12
- sum += nums[ j] ;
13
- while sum > target {
14
- sum -= nums[ i] ;
15
- i += 1 ;
5
+ let s = nums. iter ( ) . sum :: < i32 > ( ) - x;
6
+ let mut vis: HashMap < i32 , i32 > = HashMap :: new ( ) ;
7
+ vis. insert ( 0 , -1 ) ;
8
+ let mut mx = -1 ;
9
+ let mut t = 0 ;
10
+ for ( i, v) in nums. iter ( ) . enumerate ( ) {
11
+ t += v;
12
+ if !vis. contains_key ( & t) {
13
+ vis. insert ( t, i as i32 ) ;
16
14
}
17
- if sum == target {
18
- ans = ans . min ( ( n - 1 - ( j - i ) ) as i32 ) ;
15
+ if let Some ( & j ) = vis . get ( & ( t - s ) ) {
16
+ mx = mx . max ( ( i as i32 ) - j ) ;
19
17
}
20
18
}
21
- if ans == i32:: MAX {
22
- return -1 ;
19
+ if mx == -1 {
20
+ -1
21
+ } else {
22
+ ( nums. len ( ) as i32 ) - mx
23
23
}
24
- ans
25
24
}
26
25
}
Original file line number Diff line number Diff line change 1
1
function minOperations ( nums : number [ ] , x : number ) : number {
2
- x = nums . reduce ( ( a , b ) => a + b , 0 ) - x ;
3
- const vis = new Map ( ) ;
4
- vis . set ( 0 , - 1 ) ;
2
+ const s = nums . reduce ( ( acc , cur ) => acc + cur , - x ) ;
3
+ const vis : Map < number , number > = new Map ( [ [ 0 , - 1 ] ] ) ;
4
+ let [ mx , t ] = [ - 1 , 0 ] ;
5
5
const n = nums . length ;
6
- let ans = 1 << 30 ;
7
- for ( let i = 0 , s = 0 ; i < n ; ++ i ) {
8
- s += nums [ i ] ;
9
- if ( ! vis . has ( s ) ) {
10
- vis . set ( s , i ) ;
6
+ for ( let i = 0 ; i < n ; ++ i ) {
7
+ t += nums [ i ] ;
8
+ if ( ! vis . has ( t ) ) {
9
+ vis . set ( t , i ) ;
11
10
}
12
- if ( vis . has ( s - x ) ) {
13
- const j = vis . get ( s - x ) ;
14
- ans = Math . min ( ans , n - ( i - j ) ) ;
11
+ if ( vis . has ( t - s ) ) {
12
+ mx = Math . max ( mx , i - vis . get ( t - s ) ! ) ;
15
13
}
16
14
}
17
- return ans == 1 << 30 ? - 1 : ans ;
15
+ return ~ mx ? n - mx : - 1 ;
18
16
}
Original file line number Diff line number Diff line change 1
- class Solution {
2
- public:
3
- int minOperations (vector<int >& nums, int x) {
4
- x = accumulate (nums.begin (), nums.end (), 0 ) - x;
5
- int n = nums. size () ;
6
- int ans = 1 << 30 ;
7
- for (int i = 0 , j = 0 , s = 0 ; i < n; ++i) {
8
- s += nums[i];
9
- while (j <= i && s > x ) {
10
- s -= nums[j++];
11
- }
12
- if (s == x ) {
13
- ans = min (ans, n - ( i - j + 1 ) );
14
- }
15
- }
16
- return ans == 1 << 30 ? -1 : ans ;
17
- }
1
+ class Solution {
2
+ public:
3
+ int minOperations (vector<int >& nums, int x) {
4
+ int s = accumulate (nums.begin (), nums.end (), 0 ) - x;
5
+ int mx = - 1 , t = 0 ;
6
+ int n = nums. size () ;
7
+ for (int i = 0 , j = 0 ; i < n; ++i) {
8
+ t += nums[i];
9
+ while (j <= i && t > s ) {
10
+ t -= nums[j++];
11
+ }
12
+ if (t == s ) {
13
+ mx = max (mx, i - j + 1 );
14
+ }
15
+ }
16
+ return mx == - 1 ? -1 : n - mx ;
17
+ }
18
18
};
Original file line number Diff line number Diff line change 1
- func minOperations (nums []int , x int ) int {
2
- x = - x
3
- for _ , v := range nums {
4
- x += v
5
- }
6
- ans := 1 << 30
7
- s , n := 0 , len (nums )
8
- j := 0
9
- for i , v := range nums {
10
- s += v
11
- for j <= i && s > x {
12
- s -= nums [j ]
13
- j ++
14
- }
15
- if s == x {
16
- ans = min (ans , n - (i - j + 1 ))
17
- }
18
- }
19
- if ans == 1 << 30 {
20
- return - 1
21
- }
22
- return ans
1
+ func minOperations (nums []int , x int ) int {
2
+ s := - x
3
+ for _ , v := range nums {
4
+ s += v
5
+ }
6
+ mx , t , j := - 1 , 0 , 0
7
+ for i , v := range nums {
8
+ t += v
9
+ for ; j <= i && t > s ; j ++ {
10
+ t -= nums [j ]
11
+ }
12
+ if t == s {
13
+ mx = max (mx , i - j + 1 )
14
+ }
15
+ }
16
+ if mx == - 1 {
17
+ return - 1
18
+ }
19
+ return len (nums ) - mx
23
20
}
Original file line number Diff line number Diff line change 1
- class Solution {
2
- public int minOperations (int [] nums , int x ) {
3
- x = -x ;
4
- for (int v : nums ) {
5
- x += v ;
6
- }
7
- int n = nums . length ;
8
- int ans = 1 << 30 ;
9
- for (int i = 0 , j = 0 , s = 0 ; i < n ; ++i ) {
10
- s += nums [i ];
11
- while (j <= i && s > x ) {
12
- s -= nums [j ++];
13
- }
14
- if (s == x ) {
15
- ans = Math .min ( ans , n - ( i - j + 1 ) );
16
- }
17
- }
18
- return ans == 1 << 30 ? -1 : ans ;
19
- }
1
+ class Solution {
2
+ public int minOperations (int [] nums , int x ) {
3
+ int s = -x ;
4
+ for (int v : nums ) {
5
+ s += v ;
6
+ }
7
+ int mx = - 1 , t = 0 ;
8
+ int n = nums . length ;
9
+ for (int i = 0 , j = 0 ; i < n ; ++i ) {
10
+ t += nums [i ];
11
+ while (j <= i && t > s ) {
12
+ t -= nums [j ++];
13
+ }
14
+ if (t == s ) {
15
+ mx = Math .max ( mx , i - j + 1 );
16
+ }
17
+ }
18
+ return mx == - 1 ? -1 : n - mx ;
19
+ }
20
20
}
You can’t perform that action at this time.
0 commit comments