From a49a601548688c925faf96eef6dfb7e3369f858a Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 26 May 2025 19:46:24 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2966 No.2966.Divide Array Into Arrays With Max Difference --- .../README.md | 105 ++++++++++++++++++ .../README_EN.md | 105 ++++++++++++++++++ .../Solution.cs | 21 ++++ .../Solution.dart | 21 ++++ .../Solution.rs | 22 ++++ .../Solution.swift | 21 ++++ 6 files changed, 295 insertions(+) create mode 100644 solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.cs create mode 100644 solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.dart create mode 100644 solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.rs create mode 100644 solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.swift diff --git a/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/README.md b/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/README.md index a949a7c90ac5a..608c0ab2bf5a5 100644 --- a/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/README.md +++ b/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/README.md @@ -192,6 +192,111 @@ function divideArray(nums: number[], k: number): number[][] { } ``` +#### Swift + +```swift +class Solution { + func divideArray(_ nums: [Int], _ k: Int) -> [[Int]] { + var sortedNums = nums.sorted() + var ans: [[Int]] = [] + + for i in stride(from: 0, to: sortedNums.count, by: 3) { + if i + 2 >= sortedNums.count { + return [] + } + + let t = Array(sortedNums[i.. k { + return [] + } + + ans.append(t) + } + + return ans + } +} +``` + +#### Rust + +```rust +impl Solution { + pub fn divide_array(mut nums: Vec, k: i32) -> Vec> { + nums.sort(); + let mut ans = Vec::new(); + let n = nums.len(); + + for i in (0..n).step_by(3) { + if i + 2 >= n { + return vec![]; + } + + let t = &nums[i..i+3]; + if t[2] - t[0] > k { + return vec![]; + } + + ans.push(t.to_vec()); + } + + ans + } +} +``` + +#### C# + +```cs +public class Solution { + public int[][] DivideArray(int[] nums, int k) { + Array.Sort(nums); + List ans = new List(); + + for (int i = 0; i < nums.Length; i += 3) { + if (i + 2 >= nums.Length) { + return new int[0][]; + } + + int[] t = new int[] { nums[i], nums[i + 1], nums[i + 2] }; + if (t[2] - t[0] > k) { + return new int[0][]; + } + + ans.Add(t); + } + + return ans.ToArray(); + } +} +``` + +#### Dart + +```dart +class Solution { + List> divideArray(List nums, int k) { + nums.sort(); + List> ans = []; + + for (int i = 0; i < nums.length; i += 3) { + if (i + 2 >= nums.length) { + return []; + } + + List t = nums.sublist(i, i + 3); + if (t[2] - t[0] > k) { + return []; + } + + ans.add(t); + } + + return ans; + } +} +``` + diff --git a/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/README_EN.md b/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/README_EN.md index f00f83da93032..137663a42c85f 100644 --- a/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/README_EN.md +++ b/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/README_EN.md @@ -190,6 +190,111 @@ function divideArray(nums: number[], k: number): number[][] { } ``` +#### Rust + +```rust +impl Solution { + pub fn divide_array(mut nums: Vec, k: i32) -> Vec> { + nums.sort(); + let mut ans = Vec::new(); + let n = nums.len(); + + for i in (0..n).step_by(3) { + if i + 2 >= n { + return vec![]; + } + + let t = &nums[i..i+3]; + if t[2] - t[0] > k { + return vec![]; + } + + ans.push(t.to_vec()); + } + + ans + } +} +``` + +#### C# + +```cs +public class Solution { + public int[][] DivideArray(int[] nums, int k) { + Array.Sort(nums); + List ans = new List(); + + for (int i = 0; i < nums.Length; i += 3) { + if (i + 2 >= nums.Length) { + return new int[0][]; + } + + int[] t = new int[] { nums[i], nums[i + 1], nums[i + 2] }; + if (t[2] - t[0] > k) { + return new int[0][]; + } + + ans.Add(t); + } + + return ans.ToArray(); + } +} +``` + +#### Swift + +```swift +class Solution { + func divideArray(_ nums: [Int], _ k: Int) -> [[Int]] { + var sortedNums = nums.sorted() + var ans: [[Int]] = [] + + for i in stride(from: 0, to: sortedNums.count, by: 3) { + if i + 2 >= sortedNums.count { + return [] + } + + let t = Array(sortedNums[i.. k { + return [] + } + + ans.append(t) + } + + return ans + } +} +``` + +#### Dart + +```dart +class Solution { + List> divideArray(List nums, int k) { + nums.sort(); + List> ans = []; + + for (int i = 0; i < nums.length; i += 3) { + if (i + 2 >= nums.length) { + return []; + } + + List t = nums.sublist(i, i + 3); + if (t[2] - t[0] > k) { + return []; + } + + ans.add(t); + } + + return ans; + } +} +``` + diff --git a/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.cs b/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.cs new file mode 100644 index 0000000000000..bc3f44d45140a --- /dev/null +++ b/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.cs @@ -0,0 +1,21 @@ +public class Solution { + public int[][] DivideArray(int[] nums, int k) { + Array.Sort(nums); + List ans = new List(); + + for (int i = 0; i < nums.Length; i += 3) { + if (i + 2 >= nums.Length) { + return new int[0][]; + } + + int[] t = new int[] { nums[i], nums[i + 1], nums[i + 2] }; + if (t[2] - t[0] > k) { + return new int[0][]; + } + + ans.Add(t); + } + + return ans.ToArray(); + } +} diff --git a/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.dart b/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.dart new file mode 100644 index 0000000000000..446c26958ef29 --- /dev/null +++ b/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.dart @@ -0,0 +1,21 @@ +class Solution { + List> divideArray(List nums, int k) { + nums.sort(); + List> ans = []; + + for (int i = 0; i < nums.length; i += 3) { + if (i + 2 >= nums.length) { + return []; + } + + List t = nums.sublist(i, i + 3); + if (t[2] - t[0] > k) { + return []; + } + + ans.add(t); + } + + return ans; + } +} diff --git a/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.rs b/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.rs new file mode 100644 index 0000000000000..039422b71a71f --- /dev/null +++ b/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.rs @@ -0,0 +1,22 @@ +impl Solution { + pub fn divide_array(mut nums: Vec, k: i32) -> Vec> { + nums.sort(); + let mut ans = Vec::new(); + let n = nums.len(); + + for i in (0..n).step_by(3) { + if i + 2 >= n { + return vec![]; + } + + let t = &nums[i..i + 3]; + if t[2] - t[0] > k { + return vec![]; + } + + ans.push(t.to_vec()); + } + + ans + } +} diff --git a/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.swift b/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.swift new file mode 100644 index 0000000000000..7119c3aa16abb --- /dev/null +++ b/solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/Solution.swift @@ -0,0 +1,21 @@ +class Solution { + func divideArray(_ nums: [Int], _ k: Int) -> [[Int]] { + var sortedNums = nums.sorted() + var ans: [[Int]] = [] + + for i in stride(from: 0, to: sortedNums.count, by: 3) { + if i + 2 >= sortedNums.count { + return [] + } + + let t = Array(sortedNums[i.. k { + return [] + } + + ans.append(t) + } + + return ans + } +}