Skip to content

Commit 3ed21c0

Browse files
committed
2966. Divide Array Into Arrays With Max Difference
1 parent a379bad commit 3ed21c0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
3+
// Solution by Sergey Leschev
4+
// 2966. Divide Array Into Arrays With Max Difference
5+
6+
// Time complexity: O(N log N)
7+
// Space complexity: O(1)
8+
9+
func divideArray(_ nums: [Int], _ k: Int) -> [[Int]] {
10+
var ans: [[Int]] = []
11+
let sortedNums = nums.sorted()
12+
13+
for i in stride(from: 2, to: sortedNums.count, by: 3) {
14+
if sortedNums[i] - sortedNums[i - 2] > k {
15+
return []
16+
}
17+
ans.append([sortedNums[i - 2], sortedNums[i - 1], sortedNums[i]])
18+
}
19+
20+
return ans
21+
}
22+
}

0 commit comments

Comments
 (0)