File tree Expand file tree Collapse file tree 7 files changed +239
-6
lines changed
solution/3500-3599/3542.Minimum Operations to Convert All Elements to Zero Expand file tree Collapse file tree 7 files changed +239
-6
lines changed Original file line number Diff line number Diff line change @@ -103,25 +103,103 @@ tags:
103103#### Python3
104104
105105``` python
106-
106+ class Solution :
107+ def minOperations (self , nums : List[int ]) -> int :
108+ stk = []
109+ ans = 0
110+ for x in nums:
111+ while stk and stk[- 1 ] > x:
112+ ans += 1
113+ stk.pop()
114+ if x and (not stk or stk[- 1 ] != x):
115+ stk.append(x)
116+ ans += len (stk)
117+ return ans
107118```
108119
109120#### Java
110121
111122``` java
112-
123+ class Solution {
124+ public int minOperations (int [] nums ) {
125+ Deque<Integer > stk = new ArrayDeque<> ();
126+ int ans = 0 ;
127+ for (int x : nums) {
128+ while (! stk. isEmpty() && stk. peek() > x) {
129+ ans++ ;
130+ stk. pop();
131+ }
132+ if (x != 0 && (stk. isEmpty() || stk. peek() != x)) {
133+ stk. push(x);
134+ }
135+ }
136+ ans += stk. size();
137+ return ans;
138+ }
139+ }
113140```
114141
115142#### C++
116143
117144``` cpp
118-
145+ class Solution {
146+ public:
147+ int minOperations(vector<int >& nums) {
148+ vector<int > stk;
149+ int ans = 0;
150+ for (int x : nums) {
151+ while (!stk.empty() && stk.back() > x) {
152+ ++ans;
153+ stk.pop_back();
154+ }
155+ if (x != 0 && (stk.empty() || stk.back() != x)) {
156+ stk.push_back(x);
157+ }
158+ }
159+ ans += stk.size();
160+ return ans;
161+ }
162+ };
119163```
120164
121165#### Go
122166
123167```go
168+ func minOperations(nums []int) int {
169+ stk := []int{}
170+ ans := 0
171+ for _, x := range nums {
172+ for len(stk) > 0 && stk[len(stk)-1] > x {
173+ ans++
174+ stk = stk[:len(stk)-1]
175+ }
176+ if x != 0 && (len(stk) == 0 || stk[len(stk)-1] != x) {
177+ stk = append(stk, x)
178+ }
179+ }
180+ ans += len(stk)
181+ return ans
182+ }
183+ ```
124184
185+ #### TypeScript
186+
187+ ``` ts
188+ function minOperations(nums : number []): number {
189+ const stk: number [] = [];
190+ let ans = 0 ;
191+ for (const x of nums ) {
192+ while (stk .length > 0 && stk [stk .length - 1 ] > x ) {
193+ ans ++ ;
194+ stk .pop ();
195+ }
196+ if (x !== 0 && (stk .length === 0 || stk [stk .length - 1 ] !== x )) {
197+ stk .push (x );
198+ }
199+ }
200+ ans += stk .length ;
201+ return ans ;
202+ }
125203```
126204
127205<!-- tabs: end -->
Original file line number Diff line number Diff line change @@ -100,25 +100,103 @@ tags:
100100#### Python3
101101
102102``` python
103-
103+ class Solution :
104+ def minOperations (self , nums : List[int ]) -> int :
105+ stk = []
106+ ans = 0
107+ for x in nums:
108+ while stk and stk[- 1 ] > x:
109+ ans += 1
110+ stk.pop()
111+ if x and (not stk or stk[- 1 ] != x):
112+ stk.append(x)
113+ ans += len (stk)
114+ return ans
104115```
105116
106117#### Java
107118
108119``` java
109-
120+ class Solution {
121+ public int minOperations (int [] nums ) {
122+ Deque<Integer > stk = new ArrayDeque<> ();
123+ int ans = 0 ;
124+ for (int x : nums) {
125+ while (! stk. isEmpty() && stk. peek() > x) {
126+ ans++ ;
127+ stk. pop();
128+ }
129+ if (x != 0 && (stk. isEmpty() || stk. peek() != x)) {
130+ stk. push(x);
131+ }
132+ }
133+ ans += stk. size();
134+ return ans;
135+ }
136+ }
110137```
111138
112139#### C++
113140
114141``` cpp
115-
142+ class Solution {
143+ public:
144+ int minOperations(vector<int >& nums) {
145+ vector<int > stk;
146+ int ans = 0;
147+ for (int x : nums) {
148+ while (!stk.empty() && stk.back() > x) {
149+ ++ans;
150+ stk.pop_back();
151+ }
152+ if (x != 0 && (stk.empty() || stk.back() != x)) {
153+ stk.push_back(x);
154+ }
155+ }
156+ ans += stk.size();
157+ return ans;
158+ }
159+ };
116160```
117161
118162#### Go
119163
120164```go
165+ func minOperations(nums []int) int {
166+ stk := []int{}
167+ ans := 0
168+ for _, x := range nums {
169+ for len(stk) > 0 && stk[len(stk)-1] > x {
170+ ans++
171+ stk = stk[:len(stk)-1]
172+ }
173+ if x != 0 && (len(stk) == 0 || stk[len(stk)-1] != x) {
174+ stk = append(stk, x)
175+ }
176+ }
177+ ans += len(stk)
178+ return ans
179+ }
180+ ```
121181
182+ #### TypeScript
183+
184+ ``` ts
185+ function minOperations(nums : number []): number {
186+ const stk: number [] = [];
187+ let ans = 0 ;
188+ for (const x of nums ) {
189+ while (stk .length > 0 && stk [stk .length - 1 ] > x ) {
190+ ans ++ ;
191+ stk .pop ();
192+ }
193+ if (x !== 0 && (stk .length === 0 || stk [stk .length - 1 ] !== x )) {
194+ stk .push (x );
195+ }
196+ }
197+ ans += stk .length ;
198+ return ans ;
199+ }
122200```
123201
124202<!-- tabs: end -->
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int minOperations (vector<int >& nums) {
4+ vector<int > stk;
5+ int ans = 0 ;
6+ for (int x : nums) {
7+ while (!stk.empty () && stk.back () > x) {
8+ ++ans;
9+ stk.pop_back ();
10+ }
11+ if (x != 0 && (stk.empty () || stk.back () != x)) {
12+ stk.push_back (x);
13+ }
14+ }
15+ ans += stk.size ();
16+ return ans;
17+ }
18+ };
Original file line number Diff line number Diff line change 1+ func minOperations (nums []int ) int {
2+ stk := []int {}
3+ ans := 0
4+ for _ , x := range nums {
5+ for len (stk ) > 0 && stk [len (stk )- 1 ] > x {
6+ ans ++
7+ stk = stk [:len (stk )- 1 ]
8+ }
9+ if x != 0 && (len (stk ) == 0 || stk [len (stk )- 1 ] != x ) {
10+ stk = append (stk , x )
11+ }
12+ }
13+ ans += len (stk )
14+ return ans
15+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int minOperations (int [] nums ) {
3+ Deque <Integer > stk = new ArrayDeque <>();
4+ int ans = 0 ;
5+ for (int x : nums ) {
6+ while (!stk .isEmpty () && stk .peek () > x ) {
7+ ans ++;
8+ stk .pop ();
9+ }
10+ if (x != 0 && (stk .isEmpty () || stk .peek () != x )) {
11+ stk .push (x );
12+ }
13+ }
14+ ans += stk .size ();
15+ return ans ;
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def minOperations (self , nums : List [int ]) -> int :
3+ stk = []
4+ ans = 0
5+ for x in nums :
6+ while stk and stk [- 1 ] > x :
7+ ans += 1
8+ stk .pop ()
9+ if x and (not stk or stk [- 1 ] != x ):
10+ stk .append (x )
11+ ans += len (stk )
12+ return ans
Original file line number Diff line number Diff line change 1+ function minOperations ( nums : number [ ] ) : number {
2+ const stk : number [ ] = [ ] ;
3+ let ans = 0 ;
4+ for ( const x of nums ) {
5+ while ( stk . length > 0 && stk [ stk . length - 1 ] > x ) {
6+ ans ++ ;
7+ stk . pop ( ) ;
8+ }
9+ if ( x !== 0 && ( stk . length === 0 || stk [ stk . length - 1 ] !== x ) ) {
10+ stk . push ( x ) ;
11+ }
12+ }
13+ ans += stk . length ;
14+ return ans ;
15+ }
You can’t perform that action at this time.
0 commit comments