File tree Expand file tree Collapse file tree 7 files changed +162
-0
lines changed
0800-0899/0899.Orderly Queue
1400-1499/1403.Minimum Subsequence in Non-Increasing Order Expand file tree Collapse file tree 7 files changed +162
-0
lines changed Original file line number Diff line number Diff line change @@ -141,6 +141,25 @@ func orderlyQueue(s string, k int) string {
141
141
}
142
142
```
143
143
144
+ ### ** TypeScript**
145
+
146
+ ``` ts
147
+ function orderlyQueue(s : string , k : number ): string {
148
+ if (k > 1 ) {
149
+ return [... s ].sort ().join (' ' );
150
+ }
151
+ const n = s .length ;
152
+ let min = s ;
153
+ for (let i = 1 ; i < n ; i ++ ) {
154
+ const t = s .slice (i ) + s .slice (0 , i );
155
+ if (t < min ) {
156
+ min = t ;
157
+ }
158
+ }
159
+ return min ;
160
+ }
161
+ ```
162
+
144
163
### ** ...**
145
164
146
165
```
Original file line number Diff line number Diff line change @@ -119,6 +119,25 @@ func orderlyQueue(s string, k int) string {
119
119
}
120
120
```
121
121
122
+ ### ** TypeScript**
123
+
124
+ ``` ts
125
+ function orderlyQueue(s : string , k : number ): string {
126
+ if (k > 1 ) {
127
+ return [... s ].sort ().join (' ' );
128
+ }
129
+ const n = s .length ;
130
+ let min = s ;
131
+ for (let i = 1 ; i < n ; i ++ ) {
132
+ const t = s .slice (i ) + s .slice (0 , i );
133
+ if (t < min ) {
134
+ min = t ;
135
+ }
136
+ }
137
+ return min ;
138
+ }
139
+ ```
140
+
122
141
### ** ...**
123
142
124
143
```
Original file line number Diff line number Diff line change
1
+ function orderlyQueue ( s : string , k : number ) : string {
2
+ if ( k > 1 ) {
3
+ return [ ...s ] . sort ( ) . join ( '' ) ;
4
+ }
5
+ const n = s . length ;
6
+ let min = s ;
7
+ for ( let i = 1 ; i < n ; i ++ ) {
8
+ const t = s . slice ( i ) + s . slice ( 0 , i ) ;
9
+ if ( t < min ) {
10
+ min = t ;
11
+ }
12
+ }
13
+ return min ;
14
+ }
Original file line number Diff line number Diff line change @@ -141,6 +141,46 @@ func minSubsequence(nums []int) []int {
141
141
}
142
142
```
143
143
144
+ ### ** TypeScript**
145
+
146
+ ``` ts
147
+ function minSubsequence(nums : number []): number [] {
148
+ nums .sort ((a , b ) => b - a );
149
+ const sum = nums .reduce ((r , c ) => r + c );
150
+ const res: number [] = [];
151
+ let t = 0 ;
152
+ for (const num of nums ) {
153
+ t += num ;
154
+ res .push (num );
155
+ if (t > sum - t ) {
156
+ break ;
157
+ }
158
+ }
159
+ return res ;
160
+ }
161
+ ```
162
+
163
+ ### ** Rust**
164
+
165
+ ``` rust
166
+ impl Solution {
167
+ pub fn min_subsequence (mut nums : Vec <i32 >) -> Vec <i32 > {
168
+ nums . sort_by (| a , b | b . cmp (a ));
169
+ let sum = nums . iter (). sum :: <i32 >();
170
+ let mut res = vec! [];
171
+ let mut t = 0 ;
172
+ for num in nums . into_iter () {
173
+ t += num ;
174
+ res . push (num );
175
+ if t > sum - t {
176
+ break ;
177
+ }
178
+ }
179
+ res
180
+ }
181
+ }
182
+ ```
183
+
144
184
### ** ...**
145
185
146
186
```
Original file line number Diff line number Diff line change @@ -143,6 +143,46 @@ func minSubsequence(nums []int) []int {
143
143
}
144
144
```
145
145
146
+ ### ** TypeScript**
147
+
148
+ ``` ts
149
+ function minSubsequence(nums : number []): number [] {
150
+ nums .sort ((a , b ) => b - a );
151
+ const sum = nums .reduce ((r , c ) => r + c );
152
+ const res: number [] = [];
153
+ let t = 0 ;
154
+ for (const num of nums ) {
155
+ t += num ;
156
+ res .push (num );
157
+ if (t > sum - t ) {
158
+ break ;
159
+ }
160
+ }
161
+ return res ;
162
+ }
163
+ ```
164
+
165
+ ### ** Rust**
166
+
167
+ ``` rust
168
+ impl Solution {
169
+ pub fn min_subsequence (mut nums : Vec <i32 >) -> Vec <i32 > {
170
+ nums . sort_by (| a , b | b . cmp (a ));
171
+ let sum = nums . iter (). sum :: <i32 >();
172
+ let mut res = vec! [];
173
+ let mut t = 0 ;
174
+ for num in nums . into_iter () {
175
+ t += num ;
176
+ res . push (num );
177
+ if t > sum - t {
178
+ break ;
179
+ }
180
+ }
181
+ res
182
+ }
183
+ }
184
+ ```
185
+
146
186
### ** ...**
147
187
148
188
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn min_subsequence ( mut nums : Vec < i32 > ) -> Vec < i32 > {
3
+ nums. sort_by ( |a, b| b. cmp ( a) ) ;
4
+ let sum = nums. iter ( ) . sum :: < i32 > ( ) ;
5
+ let mut res = vec ! [ ] ;
6
+ let mut t = 0 ;
7
+ for num in nums. into_iter ( ) {
8
+ t += num;
9
+ res. push ( num) ;
10
+ if t > sum - t {
11
+ break ;
12
+ }
13
+ }
14
+ res
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ function minSubsequence ( nums : number [ ] ) : number [ ] {
2
+ nums . sort ( ( a , b ) => b - a ) ;
3
+ const sum = nums . reduce ( ( r , c ) => r + c ) ;
4
+ const res : number [ ] = [ ] ;
5
+ let t = 0 ;
6
+ for ( const num of nums ) {
7
+ t += num ;
8
+ res . push ( num ) ;
9
+ if ( t > sum - t ) {
10
+ break ;
11
+ }
12
+ }
13
+ return res ;
14
+ }
You can’t perform that action at this time.
0 commit comments