3634. Minimum Removals to Balance Array
UnknownView on LeetCode
Time: O(n)
Space: O(n)
Problem Overview
Minimum Removals to Balance Array (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Greedy pattern in coding interviews. Precompute prefix min-max and suffix min-max; find min removals where left max < right min.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Precompute prefix min-max and suffix min-max; find min removals where left max < right min.
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;
}
}Was this solution helpful?
Related Problems
- 11. Container With Most Water(Medium)
- 122. Best Time to Buy and Sell Stock II(Medium)
- 135. Candy(Hard)
- 179. Largest Number(Medium)
- 321. Create Maximum Number(Unknown)
- 330. Patching Array(Hard)