diff --git a/solution/3600-3699/3634.Minimum Removals to Balance Array/README.md b/solution/3600-3699/3634.Minimum Removals to Balance Array/README.md index e2e5e52c736a2..9477d7c3074f8 100644 --- a/solution/3600-3699/3634.Minimum Removals to Balance Array/README.md +++ b/solution/3600-3699/3634.Minimum Removals to Balance Array/README.md @@ -86,32 +86,107 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3634.Mi -### 方法一 +### 方法一:排序 + 二分查找 + +我们首先对数组进行排序,然后我们从小到大枚举每个元素 $\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}$。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。 #### Python3 ```python - +class Solution: + def minRemoval(self, nums: List[int], k: int) -> int: + nums.sort() + cnt = 0 + for i, x in enumerate(nums): + j = bisect_right(nums, k * x) + cnt = max(cnt, j - i) + return len(nums) - cnt ``` #### Java ```java - +class Solution { + public int minRemoval(int[] nums, int k) { + Arrays.sort(nums); + int cnt = 0; + int n = nums.length; + for (int i = 0; i < n; ++i) { + int j = n; + if (1L * nums[i] * k <= nums[n - 1]) { + j = Arrays.binarySearch(nums, nums[i] * k + 1); + j = j < 0 ? -j - 1 : j; + } + cnt = Math.max(cnt, j - i); + } + return n - cnt; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minRemoval(vector& nums, int k) { + ranges::sort(nums); + int cnt = 0; + int n = nums.size(); + for (int i = 0; i < n; ++i) { + int j = n; + if (1LL * nums[i] * k <= nums[n - 1]) { + j = upper_bound(nums.begin(), nums.end(), 1LL * nums[i] * k) - nums.begin(); + } + cnt = max(cnt, j - i); + } + return n - cnt; + } +}; ``` #### Go ```go +func minRemoval(nums []int, k int) int { + sort.Ints(nums) + n := len(nums) + cnt := 0 + for i := 0; i < n; i++ { + j := n + if int64(nums[i])*int64(k) <= int64(nums[n-1]) { + target := int64(nums[i])*int64(k) + 1 + j = sort.Search(n, func(x int) bool { + return int64(nums[x]) >= target + }) + } + cnt = max(cnt, j-i) + } + return n - cnt +} +``` +#### TypeScript + +```ts +function minRemoval(nums: number[], k: number): number { + nums.sort((a, b) => a - b); + const n = nums.length; + let cnt = 0; + for (let i = 0; i < n; ++i) { + let j = n; + if (nums[i] * k <= nums[n - 1]) { + const target = nums[i] * k + 1; + j = _.sortedIndexBy(nums, target, x => x); + } + cnt = Math.max(cnt, j - i); + } + return n - cnt; +} ``` diff --git a/solution/3600-3699/3634.Minimum Removals to Balance Array/README_EN.md b/solution/3600-3699/3634.Minimum Removals to Balance Array/README_EN.md index 24239e197eb2e..dfa0844b84f67 100644 --- a/solution/3600-3699/3634.Minimum Removals to Balance Array/README_EN.md +++ b/solution/3600-3699/3634.Minimum Removals to Balance Array/README_EN.md @@ -84,32 +84,107 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3634.Mi -### Solution 1 +### Solution 1: Sorting + Binary Search + +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}$. + +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}$. #### Python3 ```python - +class Solution: + def minRemoval(self, nums: List[int], k: int) -> int: + nums.sort() + cnt = 0 + for i, x in enumerate(nums): + j = bisect_right(nums, k * x) + cnt = max(cnt, j - i) + return len(nums) - cnt ``` #### Java ```java - +class Solution { + public int minRemoval(int[] nums, int k) { + Arrays.sort(nums); + int cnt = 0; + int n = nums.length; + for (int i = 0; i < n; ++i) { + int j = n; + if (1L * nums[i] * k <= nums[n - 1]) { + j = Arrays.binarySearch(nums, nums[i] * k + 1); + j = j < 0 ? -j - 1 : j; + } + cnt = Math.max(cnt, j - i); + } + return n - cnt; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minRemoval(vector& nums, int k) { + ranges::sort(nums); + int cnt = 0; + int n = nums.size(); + for (int i = 0; i < n; ++i) { + int j = n; + if (1LL * nums[i] * k <= nums[n - 1]) { + j = upper_bound(nums.begin(), nums.end(), 1LL * nums[i] * k) - nums.begin(); + } + cnt = max(cnt, j - i); + } + return n - cnt; + } +}; ``` #### Go ```go +func minRemoval(nums []int, k int) int { + sort.Ints(nums) + n := len(nums) + cnt := 0 + for i := 0; i < n; i++ { + j := n + if int64(nums[i])*int64(k) <= int64(nums[n-1]) { + target := int64(nums[i])*int64(k) + 1 + j = sort.Search(n, func(x int) bool { + return int64(nums[x]) >= target + }) + } + cnt = max(cnt, j-i) + } + return n - cnt +} +``` +#### TypeScript + +```ts +function minRemoval(nums: number[], k: number): number { + nums.sort((a, b) => a - b); + const n = nums.length; + let cnt = 0; + for (let i = 0; i < n; ++i) { + let j = n; + if (nums[i] * k <= nums[n - 1]) { + const target = nums[i] * k + 1; + j = _.sortedIndexBy(nums, target, x => x); + } + cnt = Math.max(cnt, j - i); + } + return n - cnt; +} ``` diff --git a/solution/3600-3699/3634.Minimum Removals to Balance Array/Solution.cpp b/solution/3600-3699/3634.Minimum Removals to Balance Array/Solution.cpp new file mode 100644 index 0000000000000..d550ddd6051fe --- /dev/null +++ b/solution/3600-3699/3634.Minimum Removals to Balance Array/Solution.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int minRemoval(vector& nums, int k) { + ranges::sort(nums); + int cnt = 0; + int n = nums.size(); + for (int i = 0; i < n; ++i) { + int j = n; + if (1LL * nums[i] * k <= nums[n - 1]) { + j = upper_bound(nums.begin(), nums.end(), 1LL * nums[i] * k) - nums.begin(); + } + cnt = max(cnt, j - i); + } + return n - cnt; + } +}; diff --git a/solution/3600-3699/3634.Minimum Removals to Balance Array/Solution.go b/solution/3600-3699/3634.Minimum Removals to Balance Array/Solution.go new file mode 100644 index 0000000000000..03f8de2293b31 --- /dev/null +++ b/solution/3600-3699/3634.Minimum Removals to Balance Array/Solution.go @@ -0,0 +1,16 @@ +func minRemoval(nums []int, k int) int { + sort.Ints(nums) + n := len(nums) + cnt := 0 + for i := 0; i < n; i++ { + j := n + if int64(nums[i])*int64(k) <= int64(nums[n-1]) { + target := int64(nums[i])*int64(k) + 1 + j = sort.Search(n, func(x int) bool { + return int64(nums[x]) >= target + }) + } + cnt = max(cnt, j-i) + } + return n - cnt +} diff --git a/solution/3600-3699/3634.Minimum Removals to Balance Array/Solution.java b/solution/3600-3699/3634.Minimum Removals to Balance Array/Solution.java new file mode 100644 index 0000000000000..e3f20dfff3ff3 --- /dev/null +++ b/solution/3600-3699/3634.Minimum Removals to Balance Array/Solution.java @@ -0,0 +1,16 @@ +class Solution { + public int minRemoval(int[] nums, int k) { + Arrays.sort(nums); + int cnt = 0; + int n = nums.length; + for (int i = 0; i < n; ++i) { + int j = n; + if (1L * nums[i] * k <= nums[n - 1]) { + j = Arrays.binarySearch(nums, nums[i] * k + 1); + j = j < 0 ? -j - 1 : j; + } + cnt = Math.max(cnt, j - i); + } + return n - cnt; + } +} \ No newline at end of file diff --git a/solution/3600-3699/3634.Minimum Removals to Balance Array/Solution.py b/solution/3600-3699/3634.Minimum Removals to Balance Array/Solution.py new file mode 100644 index 0000000000000..b8c404653aacf --- /dev/null +++ b/solution/3600-3699/3634.Minimum Removals to Balance Array/Solution.py @@ -0,0 +1,8 @@ +class Solution: + def minRemoval(self, nums: List[int], k: int) -> int: + nums.sort() + cnt = 0 + for i, x in enumerate(nums): + j = bisect_right(nums, k * x) + cnt = max(cnt, j - i) + return len(nums) - cnt diff --git a/solution/3600-3699/3634.Minimum Removals to Balance Array/Solution.ts b/solution/3600-3699/3634.Minimum Removals to Balance Array/Solution.ts new file mode 100644 index 0000000000000..bbfef40f7aa16 --- /dev/null +++ b/solution/3600-3699/3634.Minimum Removals to Balance Array/Solution.ts @@ -0,0 +1,14 @@ +function minRemoval(nums: number[], k: number): number { + nums.sort((a, b) => a - b); + const n = nums.length; + let cnt = 0; + for (let i = 0; i < n; ++i) { + let j = n; + if (nums[i] * k <= nums[n - 1]) { + const target = nums[i] * k + 1; + j = _.sortedIndexBy(nums, target, x => x); + } + cnt = Math.max(cnt, j - i); + } + return n - cnt; +}