Skip to content

Commit 6856a56

Browse files
committed
feat: add Rust solution for lc No.3634
1 parent 187f99d commit 6856a56

File tree

4 files changed

+153
-1
lines changed

4 files changed

+153
-1
lines changed

solution/3600-3699/3634.Minimum Removals to Balance Array/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,27 @@ function minRemoval(nums: number[], k: number): number {
195195
}
196196
```
197197

198+
#### Rust
199+
200+
```rust
201+
impl Solution {
202+
pub fn min_removal(mut nums: Vec<i32>, k: i32) -> i32 {
203+
nums.sort();
204+
let mut cnt = 0;
205+
let n = nums.len();
206+
for i in 0..n {
207+
let mut j = n;
208+
let target = nums[i] as i64 * k as i64;
209+
if target <= nums[n - 1] as i64 {
210+
j = nums.partition_point(|&x| x as i64 <= target);
211+
}
212+
cnt = cnt.max(j - i);
213+
}
214+
(n - cnt) as i32
215+
}
216+
}
217+
```
218+
198219
<!-- tabs:end -->
199220

200221
<!-- solution:end -->
@@ -265,6 +286,24 @@ public:
265286
};
266287
```
267288
289+
#### Go
290+
291+
```go
292+
func minRemoval(nums []int, k int) int {
293+
sort.Ints(nums)
294+
n := len(nums)
295+
ans := n
296+
r := 0
297+
for l := 0; l < n; l++ {
298+
for r < n && nums[r] <= nums[l]*k {
299+
r++
300+
}
301+
ans = min(ans, n-(r-l))
302+
}
303+
return ans
304+
}
305+
```
306+
268307
#### TypeScript
269308

270309
```ts
@@ -283,6 +322,27 @@ function minRemoval(nums: number[], k: number): number {
283322
}
284323
```
285324

325+
#### Rust
326+
327+
```rust
328+
impl Solution {
329+
pub fn min_removal(mut nums: Vec<i32>, k: i32) -> i32 {
330+
nums.sort();
331+
let n = nums.len();
332+
let mut ans = n;
333+
let mut r = 0;
334+
let k = k as i64;
335+
for l in 0..n {
336+
while r < n && nums[r] as i64 <= nums[l] as i64 * k {
337+
r += 1;
338+
}
339+
ans = ans.min(n - (r - l));
340+
}
341+
ans as i32
342+
}
343+
}
344+
```
345+
286346
<!-- tabs:end -->
287347

288348
<!-- solution:end -->

solution/3600-3699/3634.Minimum Removals to Balance Array/README_EN.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,27 @@ function minRemoval(nums: number[], k: number): number {
193193
}
194194
```
195195

196+
#### Rust
197+
198+
```rust
199+
impl Solution {
200+
pub fn min_removal(mut nums: Vec<i32>, k: i32) -> i32 {
201+
nums.sort();
202+
let mut cnt = 0;
203+
let n = nums.len();
204+
for i in 0..n {
205+
let mut j = n;
206+
let target = nums[i] as i64 * k as i64;
207+
if target <= nums[n - 1] as i64 {
208+
j = nums.partition_point(|&x| x as i64 <= target);
209+
}
210+
cnt = cnt.max(j - i);
211+
}
212+
(n - cnt) as i32
213+
}
214+
}
215+
```
216+
196217
<!-- tabs:end -->
197218

198219
<!-- solution:end -->
@@ -203,7 +224,7 @@ function minRemoval(nums: number[], k: number): number {
203224

204225
We first sort the array, then use two pointers to maintain a sliding window. The left pointer $l$ enumerates each element $\textit{nums}[l]$ from left to right as the minimum value of the balanced array. The right pointer $r$ keeps moving right until $\textit{nums}[r]$ is greater than $\textit{nums}[l] \times k$. At this point, the length of the balanced array is $r - l$, and the number of elements to be removed is $n - (r - l)$. We record the minimum number of removals as the answer.
205226

206-
The time complexity is $O(n \log n)$ and the space complexity is $O(\log n)$, where $n$ is the length of the array $\textit{nums}$.
227+
The time complexity is $O(n \times \log n)$ and the space complexity is $O(\log n)$, where $n$ is the length of the array $\textit{nums}$.
207228

208229
<!-- tabs:start -->
209230

@@ -263,6 +284,24 @@ public:
263284
};
264285
```
265286
287+
#### Go
288+
289+
```go
290+
func minRemoval(nums []int, k int) int {
291+
sort.Ints(nums)
292+
n := len(nums)
293+
ans := n
294+
r := 0
295+
for l := 0; l < n; l++ {
296+
for r < n && nums[r] <= nums[l]*k {
297+
r++
298+
}
299+
ans = min(ans, n-(r-l))
300+
}
301+
return ans
302+
}
303+
```
304+
266305
#### TypeScript
267306

268307
```ts
@@ -281,6 +320,27 @@ function minRemoval(nums: number[], k: number): number {
281320
}
282321
```
283322

323+
#### Rust
324+
325+
```rust
326+
impl Solution {
327+
pub fn min_removal(mut nums: Vec<i32>, k: i32) -> i32 {
328+
nums.sort();
329+
let n = nums.len();
330+
let mut ans = n;
331+
let mut r = 0;
332+
let k = k as i64;
333+
for l in 0..n {
334+
while r < n && nums[r] as i64 <= nums[l] as i64 * k {
335+
r += 1;
336+
}
337+
ans = ans.min(n - (r - l));
338+
}
339+
ans as i32
340+
}
341+
}
342+
```
343+
284344
<!-- tabs:end -->
285345

286346
<!-- solution:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn min_removal(mut nums: Vec<i32>, k: i32) -> i32 {
3+
nums.sort();
4+
let mut cnt = 0;
5+
let n = nums.len();
6+
for i in 0..n {
7+
let mut j = n;
8+
let target = nums[i] as i64 * k as i64;
9+
if target <= nums[n - 1] as i64 {
10+
j = nums.partition_point(|&x| x as i64 <= target);
11+
}
12+
cnt = cnt.max(j - i);
13+
}
14+
(n - cnt) as i32
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 min_removal(mut nums: Vec<i32>, k: i32) -> i32 {
3+
nums.sort();
4+
let n = nums.len();
5+
let mut ans = n;
6+
let mut r = 0;
7+
let k = k as i64;
8+
for l in 0..n {
9+
while r < n && nums[r] as i64 <= nums[l] as i64 * k {
10+
r += 1;
11+
}
12+
ans = ans.min(n - (r - l));
13+
}
14+
ans as i32
15+
}
16+
}

0 commit comments

Comments
 (0)