3634. Minimum Removals to Balance Array
UnknownView on LeetCode
Time: O(n)
Space: O(n)
Advertisement
Approach
Precompute prefix min-max and suffix min-max; find min removals where left max < right min.
Key Techniques
Array
Array problems involve manipulating elements stored in a contiguous block of memory. Key techniques include two-pointer traversal, prefix sums, sliding windows, and in-place partitioning. In C#, arrays are zero-indexed and fixed in size — use List<T> when you need dynamic resizing.
Greedy
Greedy algorithms make locally optimal choices at each step, hoping to reach a global optimum. Greedy works when a problem has the "greedy choice property" and "optimal substructure". Common applications: interval scheduling, activity selection, Huffman coding, and jump game.
3634.cs
C#
// Approach: Precompute prefix min-max and suffix min-max; find min removals where left max < right min.
// Time: O(n) Space: O(n)
public class Solution
{
public int MinRemoval(int[] nums, int k)
{
Array.Sort(nums);
int cnt = 0;
int n = nums.Length;
for (int i = 0; i < n; ++i)
{
int j = n;
if ((long)nums[i] * k <= nums[n - 1])
{
j = Array.BinarySearch(nums, nums[i] * k + 1);
j = j < 0 ? ~j : j;
}
cnt = Math.Max(cnt, j - i);
}
return n - cnt;
}
}Advertisement
Was this solution helpful?