mirror of https://github.com/doocs/leetcode.git
feat: add solutions to lc problem: No.2294 (#4507)
No.2294.Partition Array Such That Maximum Difference Is K
This commit is contained in:
parent
590343fc71
commit
0fcbf87ced
|
|
@ -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 -->
|
||||
|
|
|
|||
|
|
@ -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 -->
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue