File tree Expand file tree Collapse file tree 6 files changed +295
-0
lines changed
solution/2900-2999/2966.Divide Array Into Arrays With Max Difference Expand file tree Collapse file tree 6 files changed +295
-0
lines changed Original file line number Diff line number Diff line change @@ -192,6 +192,111 @@ function divideArray(nums: number[], k: number): number[][] {
192
192
}
193
193
```
194
194
195
+ #### Swift
196
+
197
+ ``` swift
198
+ class Solution {
199
+ func divideArray (_ nums : [Int ], _ k : Int ) -> [[Int ]] {
200
+ var sortedNums = nums.sorted ()
201
+ var ans: [[Int ]] = []
202
+
203
+ for i in stride (from : 0 , to : sortedNums.count , by : 3 ) {
204
+ if i + 2 >= sortedNums.count {
205
+ return []
206
+ }
207
+
208
+ let t = Array (sortedNums[i..< i+ 3 ])
209
+ if t[2 ] - t[0 ] > k {
210
+ return []
211
+ }
212
+
213
+ ans.append (t)
214
+ }
215
+
216
+ return ans
217
+ }
218
+ }
219
+ ```
220
+
221
+ #### Rust
222
+
223
+ ``` rust
224
+ impl Solution {
225
+ pub fn divide_array (mut nums : Vec <i32 >, k : i32 ) -> Vec <Vec <i32 >> {
226
+ nums . sort ();
227
+ let mut ans = Vec :: new ();
228
+ let n = nums . len ();
229
+
230
+ for i in (0 .. n ). step_by (3 ) {
231
+ if i + 2 >= n {
232
+ return vec! [];
233
+ }
234
+
235
+ let t = & nums [i .. i + 3 ];
236
+ if t [2 ] - t [0 ] > k {
237
+ return vec! [];
238
+ }
239
+
240
+ ans . push (t . to_vec ());
241
+ }
242
+
243
+ ans
244
+ }
245
+ }
246
+ ```
247
+
248
+ #### C#
249
+
250
+ ``` cs
251
+ public class Solution {
252
+ public int [][] DivideArray (int [] nums , int k ) {
253
+ Array .Sort (nums );
254
+ List < int []> ans = new List <int []>();
255
+
256
+ for (int i = 0 ; i < nums .Length ; i += 3 ) {
257
+ if (i + 2 >= nums .Length ) {
258
+ return new int [0 ][];
259
+ }
260
+
261
+ int [] t = new int [] { nums [i ], nums [i + 1 ], nums [i + 2 ] };
262
+ if (t [2 ] - t [0 ] > k ) {
263
+ return new int [0 ][];
264
+ }
265
+
266
+ ans .Add (t );
267
+ }
268
+
269
+ return ans .ToArray ();
270
+ }
271
+ }
272
+ ```
273
+
274
+ #### Dart
275
+
276
+ ``` dart
277
+ class Solution {
278
+ List<List<int>> divideArray(List<int> nums, int k) {
279
+ nums.sort();
280
+ List<List<int>> ans = [];
281
+
282
+ for (int i = 0; i < nums.length; i += 3) {
283
+ if (i + 2 >= nums.length) {
284
+ return [];
285
+ }
286
+
287
+ List<int> t = nums.sublist(i, i + 3);
288
+ if (t[2] - t[0] > k) {
289
+ return [];
290
+ }
291
+
292
+ ans.add(t);
293
+ }
294
+
295
+ return ans;
296
+ }
297
+ }
298
+ ```
299
+
195
300
<!-- tabs: end -->
196
301
197
302
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -190,6 +190,111 @@ function divideArray(nums: number[], k: number): number[][] {
190
190
}
191
191
```
192
192
193
+ #### Rust
194
+
195
+ ``` rust
196
+ impl Solution {
197
+ pub fn divide_array (mut nums : Vec <i32 >, k : i32 ) -> Vec <Vec <i32 >> {
198
+ nums . sort ();
199
+ let mut ans = Vec :: new ();
200
+ let n = nums . len ();
201
+
202
+ for i in (0 .. n ). step_by (3 ) {
203
+ if i + 2 >= n {
204
+ return vec! [];
205
+ }
206
+
207
+ let t = & nums [i .. i + 3 ];
208
+ if t [2 ] - t [0 ] > k {
209
+ return vec! [];
210
+ }
211
+
212
+ ans . push (t . to_vec ());
213
+ }
214
+
215
+ ans
216
+ }
217
+ }
218
+ ```
219
+
220
+ #### C#
221
+
222
+ ``` cs
223
+ public class Solution {
224
+ public int [][] DivideArray (int [] nums , int k ) {
225
+ Array .Sort (nums );
226
+ List < int []> ans = new List <int []>();
227
+
228
+ for (int i = 0 ; i < nums .Length ; i += 3 ) {
229
+ if (i + 2 >= nums .Length ) {
230
+ return new int [0 ][];
231
+ }
232
+
233
+ int [] t = new int [] { nums [i ], nums [i + 1 ], nums [i + 2 ] };
234
+ if (t [2 ] - t [0 ] > k ) {
235
+ return new int [0 ][];
236
+ }
237
+
238
+ ans .Add (t );
239
+ }
240
+
241
+ return ans .ToArray ();
242
+ }
243
+ }
244
+ ```
245
+
246
+ #### Swift
247
+
248
+ ``` swift
249
+ class Solution {
250
+ func divideArray (_ nums : [Int ], _ k : Int ) -> [[Int ]] {
251
+ var sortedNums = nums.sorted ()
252
+ var ans: [[Int ]] = []
253
+
254
+ for i in stride (from : 0 , to : sortedNums.count , by : 3 ) {
255
+ if i + 2 >= sortedNums.count {
256
+ return []
257
+ }
258
+
259
+ let t = Array (sortedNums[i..< i+ 3 ])
260
+ if t[2 ] - t[0 ] > k {
261
+ return []
262
+ }
263
+
264
+ ans.append (t)
265
+ }
266
+
267
+ return ans
268
+ }
269
+ }
270
+ ```
271
+
272
+ #### Dart
273
+
274
+ ``` dart
275
+ class Solution {
276
+ List<List<int>> divideArray(List<int> nums, int k) {
277
+ nums.sort();
278
+ List<List<int>> ans = [];
279
+
280
+ for (int i = 0; i < nums.length; i += 3) {
281
+ if (i + 2 >= nums.length) {
282
+ return [];
283
+ }
284
+
285
+ List<int> t = nums.sublist(i, i + 3);
286
+ if (t[2] - t[0] > k) {
287
+ return [];
288
+ }
289
+
290
+ ans.add(t);
291
+ }
292
+
293
+ return ans;
294
+ }
295
+ }
296
+ ```
297
+
193
298
<!-- tabs: end -->
194
299
195
300
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public int [ ] [ ] DivideArray ( int [ ] nums , int k ) {
3
+ Array . Sort ( nums ) ;
4
+ List < int [ ] > ans = new List < int [ ] > ( ) ;
5
+
6
+ for ( int i = 0 ; i < nums . Length ; i += 3 ) {
7
+ if ( i + 2 >= nums . Length ) {
8
+ return new int [ 0 ] [ ] ;
9
+ }
10
+
11
+ int [ ] t = new int [ ] { nums [ i ] , nums [ i + 1 ] , nums [ i + 2 ] } ;
12
+ if ( t [ 2 ] - t [ 0 ] > k ) {
13
+ return new int [ 0 ] [ ] ;
14
+ }
15
+
16
+ ans . Add ( t ) ;
17
+ }
18
+
19
+ return ans . ToArray ( ) ;
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ List <List <int >> divideArray (List <int > nums, int k) {
3
+ nums.sort ();
4
+ List <List <int >> ans = [];
5
+
6
+ for (int i = 0 ; i < nums.length; i += 3 ) {
7
+ if (i + 2 >= nums.length) {
8
+ return [];
9
+ }
10
+
11
+ List <int > t = nums.sublist (i, i + 3 );
12
+ if (t[2 ] - t[0 ] > k) {
13
+ return [];
14
+ }
15
+
16
+ ans.add (t);
17
+ }
18
+
19
+ return ans;
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn divide_array ( mut nums : Vec < i32 > , k : i32 ) -> Vec < Vec < i32 > > {
3
+ nums. sort ( ) ;
4
+ let mut ans = Vec :: new ( ) ;
5
+ let n = nums. len ( ) ;
6
+
7
+ for i in ( 0 ..n) . step_by ( 3 ) {
8
+ if i + 2 >= n {
9
+ return vec ! [ ] ;
10
+ }
11
+
12
+ let t = & nums[ i..i + 3 ] ;
13
+ if t[ 2 ] - t[ 0 ] > k {
14
+ return vec ! [ ] ;
15
+ }
16
+
17
+ ans. push ( t. to_vec ( ) ) ;
18
+ }
19
+
20
+ ans
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func divideArray( _ nums: [ Int ] , _ k: Int ) -> [ [ Int ] ] {
3
+ var sortedNums = nums. sorted ( )
4
+ var ans : [ [ Int ] ] = [ ]
5
+
6
+ for i in stride ( from: 0 , to: sortedNums. count, by: 3 ) {
7
+ if i + 2 >= sortedNums. count {
8
+ return [ ]
9
+ }
10
+
11
+ let t = Array ( sortedNums [ i..< i+ 3 ] )
12
+ if t [ 2 ] - t[ 0 ] > k {
13
+ return [ ]
14
+ }
15
+
16
+ ans. append ( t)
17
+ }
18
+
19
+ return ans
20
+ }
21
+ }
You can’t perform that action at this time.
0 commit comments