Skip to content

Commit 0fcbf87

Browse files
authored
feat: add solutions to lc problem: No.2294 (#4507)
No.2294.Partition Array Such That Maximum Difference Is K
1 parent 590343f commit 0fcbf87

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,48 @@ function partitionArray(nums: number[], k: number): number {
173173
}
174174
```
175175

176+
#### Rust
177+
178+
```rust
179+
impl Solution {
180+
pub fn partition_array(mut nums: Vec<i32>, k: i32) -> i32 {
181+
nums.sort();
182+
let mut ans = 1;
183+
let mut a = nums[0];
184+
185+
for &b in nums.iter() {
186+
if b - a > k {
187+
a = b;
188+
ans += 1;
189+
}
190+
}
191+
192+
ans
193+
}
194+
}
195+
```
196+
197+
#### Rust
198+
199+
```rust
200+
public class Solution {
201+
public int PartitionArray(int[] nums, int k) {
202+
Array.Sort(nums);
203+
int ans = 1;
204+
int a = nums[0];
205+
206+
foreach (int b in nums) {
207+
if (b - a > k) {
208+
a = b;
209+
ans++;
210+
}
211+
}
212+
213+
return ans;
214+
}
215+
}
216+
```
217+
176218
<!-- tabs:end -->
177219

178220
<!-- solution:end -->

solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/README_EN.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,48 @@ function partitionArray(nums: number[], k: number): number {
171171
}
172172
```
173173

174+
#### Rust
175+
176+
```rust
177+
impl Solution {
178+
pub fn partition_array(mut nums: Vec<i32>, k: i32) -> i32 {
179+
nums.sort();
180+
let mut ans = 1;
181+
let mut a = nums[0];
182+
183+
for &b in nums.iter() {
184+
if b - a > k {
185+
a = b;
186+
ans += 1;
187+
}
188+
}
189+
190+
ans
191+
}
192+
}
193+
```
194+
195+
#### Rust
196+
197+
```rust
198+
public class Solution {
199+
public int PartitionArray(int[] nums, int k) {
200+
Array.Sort(nums);
201+
int ans = 1;
202+
int a = nums[0];
203+
204+
foreach (int b in nums) {
205+
if (b - a > k) {
206+
a = b;
207+
ans++;
208+
}
209+
}
210+
211+
return ans;
212+
}
213+
}
214+
```
215+
174216
<!-- tabs:end -->
175217

176218
<!-- solution:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Solution {
2+
public int PartitionArray(int[] nums, int k) {
3+
Array.Sort(nums);
4+
int ans = 1;
5+
int a = nums[0];
6+
7+
foreach (int b in nums) {
8+
if (b - a > k) {
9+
a = b;
10+
ans++;
11+
}
12+
}
13+
14+
return ans;
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn partition_array(mut nums: Vec<i32>, k: i32) -> i32 {
3+
nums.sort();
4+
let mut ans = 1;
5+
let mut a = nums[0];
6+
7+
for &b in nums.iter() {
8+
if b - a > k {
9+
a = b;
10+
ans += 1;
11+
}
12+
}
13+
14+
ans
15+
}
16+
}

0 commit comments

Comments
 (0)