feat: add solutions to lc problem: No.2294 (#4507)

No.2294.Partition Array Such That Maximum Difference Is K
This commit is contained in:
Libin YANG 2025-06-19 07:03:21 +08:00 committed by GitHub
parent 590343fc71
commit 0fcbf87ced
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 116 additions and 0 deletions

View File

@ -173,6 +173,48 @@ function partitionArray(nums: number[], k: number): number {
}
```
#### Rust
```rust
impl Solution {
pub fn partition_array(mut nums: Vec<i32>, k: i32) -> i32 {
nums.sort();
let mut ans = 1;
let mut a = nums[0];
for &b in nums.iter() {
if b - a > k {
a = b;
ans += 1;
}
}
ans
}
}
```
#### Rust
```rust
public class Solution {
public int PartitionArray(int[] nums, int k) {
Array.Sort(nums);
int ans = 1;
int a = nums[0];
foreach (int b in nums) {
if (b - a > k) {
a = b;
ans++;
}
}
return ans;
}
}
```
<!-- tabs:end -->
<!-- solution:end -->

View File

@ -171,6 +171,48 @@ function partitionArray(nums: number[], k: number): number {
}
```
#### Rust
```rust
impl Solution {
pub fn partition_array(mut nums: Vec<i32>, k: i32) -> i32 {
nums.sort();
let mut ans = 1;
let mut a = nums[0];
for &b in nums.iter() {
if b - a > k {
a = b;
ans += 1;
}
}
ans
}
}
```
#### Rust
```rust
public class Solution {
public int PartitionArray(int[] nums, int k) {
Array.Sort(nums);
int ans = 1;
int a = nums[0];
foreach (int b in nums) {
if (b - a > k) {
a = b;
ans++;
}
}
return ans;
}
}
```
<!-- tabs:end -->
<!-- solution:end -->

View File

@ -0,0 +1,16 @@
public class Solution {
public int PartitionArray(int[] nums, int k) {
Array.Sort(nums);
int ans = 1;
int a = nums[0];
foreach (int b in nums) {
if (b - a > k) {
a = b;
ans++;
}
}
return ans;
}
}

View File

@ -0,0 +1,16 @@
impl Solution {
pub fn partition_array(mut nums: Vec<i32>, k: i32) -> i32 {
nums.sort();
let mut ans = 1;
let mut a = nums[0];
for &b in nums.iter() {
if b - a > k {
a = b;
ans += 1;
}
}
ans
}
}