Skip to content

Commit 20e2f92

Browse files
authored
Create 2966_divide_array_into_arrays_with_max_difference.rs
1 parent 9a7ff32 commit 20e2f92

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn divide_array(nums: Vec<i32>, k: i32) -> Vec<Vec<i32>> {
3+
let mut nums = nums;
4+
nums.sort_unstable();
5+
let n = nums.len();
6+
let jump = n / (n / 3);
7+
let mut answer = vec![];
8+
let mut start = 0;
9+
while start < n - jump + 1 {
10+
let array = nums[start..start + jump].to_vec();
11+
if array[array.len() - 1] - array[0] > k {
12+
return vec![];
13+
}
14+
answer.push(array);
15+
start = start + jump;
16+
}
17+
answer
18+
}
19+
}

0 commit comments

Comments
 (0)