Skip to content

Commit fa0b9d1

Browse files
authored
feat: add solutions to lc problem: No.3634 (#4616)
No.3634.Minimum Removals to Balance Array
1 parent 02f5e66 commit fa0b9d1

File tree

7 files changed

+228
-8
lines changed

7 files changed

+228
-8
lines changed

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

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,32 +86,107 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3634.Mi
8686

8787
<!-- solution:start -->
8888

89-
### 方法一
89+
### 方法一:排序 + 二分查找
90+
91+
我们首先对数组进行排序,然后我们从小到大枚举每个元素 $\textit{nums}[i]$ 作为平衡数组的最小值,那么平衡数组的最大值 $\textit{max}$ 必须满足 $\textit{max} \leq \textit{nums}[i] \times k$。因此,我们可以使用二分查找来找到第一个大于 $\textit{nums}[i] \times k$ 的元素的下标 $j$,那么此时平衡数组的长度为 $j - i$,我们记录下最大的长度 $\textit{cnt}$,最后的答案就是数组长度减去 $\textit{cnt}$。
92+
93+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。
9094

9195
<!-- tabs:start -->
9296

9397
#### Python3
9498

9599
```python
96-
100+
class Solution:
101+
def minRemoval(self, nums: List[int], k: int) -> int:
102+
nums.sort()
103+
cnt = 0
104+
for i, x in enumerate(nums):
105+
j = bisect_right(nums, k * x)
106+
cnt = max(cnt, j - i)
107+
return len(nums) - cnt
97108
```
98109

99110
#### Java
100111

101112
```java
102-
113+
class Solution {
114+
public int minRemoval(int[] nums, int k) {
115+
Arrays.sort(nums);
116+
int cnt = 0;
117+
int n = nums.length;
118+
for (int i = 0; i < n; ++i) {
119+
int j = n;
120+
if (1L * nums[i] * k <= nums[n - 1]) {
121+
j = Arrays.binarySearch(nums, nums[i] * k + 1);
122+
j = j < 0 ? -j - 1 : j;
123+
}
124+
cnt = Math.max(cnt, j - i);
125+
}
126+
return n - cnt;
127+
}
128+
}
103129
```
104130

105131
#### C++
106132

107133
```cpp
108-
134+
class Solution {
135+
public:
136+
int minRemoval(vector<int>& nums, int k) {
137+
ranges::sort(nums);
138+
int cnt = 0;
139+
int n = nums.size();
140+
for (int i = 0; i < n; ++i) {
141+
int j = n;
142+
if (1LL * nums[i] * k <= nums[n - 1]) {
143+
j = upper_bound(nums.begin(), nums.end(), 1LL * nums[i] * k) - nums.begin();
144+
}
145+
cnt = max(cnt, j - i);
146+
}
147+
return n - cnt;
148+
}
149+
};
109150
```
110151
111152
#### Go
112153
113154
```go
155+
func minRemoval(nums []int, k int) int {
156+
sort.Ints(nums)
157+
n := len(nums)
158+
cnt := 0
159+
for i := 0; i < n; i++ {
160+
j := n
161+
if int64(nums[i])*int64(k) <= int64(nums[n-1]) {
162+
target := int64(nums[i])*int64(k) + 1
163+
j = sort.Search(n, func(x int) bool {
164+
return int64(nums[x]) >= target
165+
})
166+
}
167+
cnt = max(cnt, j-i)
168+
}
169+
return n - cnt
170+
}
171+
```
114172

173+
#### TypeScript
174+
175+
```ts
176+
function minRemoval(nums: number[], k: number): number {
177+
nums.sort((a, b) => a - b);
178+
const n = nums.length;
179+
let cnt = 0;
180+
for (let i = 0; i < n; ++i) {
181+
let j = n;
182+
if (nums[i] * k <= nums[n - 1]) {
183+
const target = nums[i] * k + 1;
184+
j = _.sortedIndexBy(nums, target, x => x);
185+
}
186+
cnt = Math.max(cnt, j - i);
187+
}
188+
return n - cnt;
189+
}
115190
```
116191

117192
<!-- tabs:end -->

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

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,32 +84,107 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3634.Mi
8484

8585
<!-- solution:start -->
8686

87-
### Solution 1
87+
### Solution 1: Sorting + Binary Search
88+
89+
We first sort the array, then enumerate each element $\textit{nums}[i]$ from small to large as the minimum value of the balanced array. The maximum value $\textit{max}$ of the balanced array must satisfy $\textit{max} \leq \textit{nums}[i] \times k$. Therefore, we can use binary search to find the index $j$ of the first element greater than $\textit{nums}[i] \times k$. At this point, the length of the balanced array is $j - i$. We record the maximum length $\textit{cnt}$, and the final answer is the array length minus $\textit{cnt}$.
90+
91+
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}$.
8892

8993
<!-- tabs:start -->
9094

9195
#### Python3
9296

9397
```python
94-
98+
class Solution:
99+
def minRemoval(self, nums: List[int], k: int) -> int:
100+
nums.sort()
101+
cnt = 0
102+
for i, x in enumerate(nums):
103+
j = bisect_right(nums, k * x)
104+
cnt = max(cnt, j - i)
105+
return len(nums) - cnt
95106
```
96107

97108
#### Java
98109

99110
```java
100-
111+
class Solution {
112+
public int minRemoval(int[] nums, int k) {
113+
Arrays.sort(nums);
114+
int cnt = 0;
115+
int n = nums.length;
116+
for (int i = 0; i < n; ++i) {
117+
int j = n;
118+
if (1L * nums[i] * k <= nums[n - 1]) {
119+
j = Arrays.binarySearch(nums, nums[i] * k + 1);
120+
j = j < 0 ? -j - 1 : j;
121+
}
122+
cnt = Math.max(cnt, j - i);
123+
}
124+
return n - cnt;
125+
}
126+
}
101127
```
102128

103129
#### C++
104130

105131
```cpp
106-
132+
class Solution {
133+
public:
134+
int minRemoval(vector<int>& nums, int k) {
135+
ranges::sort(nums);
136+
int cnt = 0;
137+
int n = nums.size();
138+
for (int i = 0; i < n; ++i) {
139+
int j = n;
140+
if (1LL * nums[i] * k <= nums[n - 1]) {
141+
j = upper_bound(nums.begin(), nums.end(), 1LL * nums[i] * k) - nums.begin();
142+
}
143+
cnt = max(cnt, j - i);
144+
}
145+
return n - cnt;
146+
}
147+
};
107148
```
108149
109150
#### Go
110151
111152
```go
153+
func minRemoval(nums []int, k int) int {
154+
sort.Ints(nums)
155+
n := len(nums)
156+
cnt := 0
157+
for i := 0; i < n; i++ {
158+
j := n
159+
if int64(nums[i])*int64(k) <= int64(nums[n-1]) {
160+
target := int64(nums[i])*int64(k) + 1
161+
j = sort.Search(n, func(x int) bool {
162+
return int64(nums[x]) >= target
163+
})
164+
}
165+
cnt = max(cnt, j-i)
166+
}
167+
return n - cnt
168+
}
169+
```
112170

171+
#### TypeScript
172+
173+
```ts
174+
function minRemoval(nums: number[], k: number): number {
175+
nums.sort((a, b) => a - b);
176+
const n = nums.length;
177+
let cnt = 0;
178+
for (let i = 0; i < n; ++i) {
179+
let j = n;
180+
if (nums[i] * k <= nums[n - 1]) {
181+
const target = nums[i] * k + 1;
182+
j = _.sortedIndexBy(nums, target, x => x);
183+
}
184+
cnt = Math.max(cnt, j - i);
185+
}
186+
return n - cnt;
187+
}
113188
```
114189

115190
<!-- tabs:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int minRemoval(vector<int>& nums, int k) {
4+
ranges::sort(nums);
5+
int cnt = 0;
6+
int n = nums.size();
7+
for (int i = 0; i < n; ++i) {
8+
int j = n;
9+
if (1LL * nums[i] * k <= nums[n - 1]) {
10+
j = upper_bound(nums.begin(), nums.end(), 1LL * nums[i] * k) - nums.begin();
11+
}
12+
cnt = max(cnt, j - i);
13+
}
14+
return n - cnt;
15+
}
16+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func minRemoval(nums []int, k int) int {
2+
sort.Ints(nums)
3+
n := len(nums)
4+
cnt := 0
5+
for i := 0; i < n; i++ {
6+
j := n
7+
if int64(nums[i])*int64(k) <= int64(nums[n-1]) {
8+
target := int64(nums[i])*int64(k) + 1
9+
j = sort.Search(n, func(x int) bool {
10+
return int64(nums[x]) >= target
11+
})
12+
}
13+
cnt = max(cnt, j-i)
14+
}
15+
return n - cnt
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int minRemoval(int[] nums, int k) {
3+
Arrays.sort(nums);
4+
int cnt = 0;
5+
int n = nums.length;
6+
for (int i = 0; i < n; ++i) {
7+
int j = n;
8+
if (1L * nums[i] * k <= nums[n - 1]) {
9+
j = Arrays.binarySearch(nums, nums[i] * k + 1);
10+
j = j < 0 ? -j - 1 : j;
11+
}
12+
cnt = Math.max(cnt, j - i);
13+
}
14+
return n - cnt;
15+
}
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def minRemoval(self, nums: List[int], k: int) -> int:
3+
nums.sort()
4+
cnt = 0
5+
for i, x in enumerate(nums):
6+
j = bisect_right(nums, k * x)
7+
cnt = max(cnt, j - i)
8+
return len(nums) - cnt
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function minRemoval(nums: number[], k: number): number {
2+
nums.sort((a, b) => a - b);
3+
const n = nums.length;
4+
let cnt = 0;
5+
for (let i = 0; i < n; ++i) {
6+
let j = n;
7+
if (nums[i] * k <= nums[n - 1]) {
8+
const target = nums[i] * k + 1;
9+
j = _.sortedIndexBy(nums, target, x => x);
10+
}
11+
cnt = Math.max(cnt, j - i);
12+
}
13+
return n - cnt;
14+
}

0 commit comments

Comments
 (0)