3824. Minimum K to Reduce Array Within Limit
Description
You are given a positive integer array nums.
For a positive integer k, define nonPositive(nums, k) as the minimum number of operations needed to make every element of nums non-positive. In one operation, you can choose an index i and reduce nums[i] by k.
Return an integer denoting the minimum value of k such that nonPositive(nums, k) <= k2.
Β
Example 1:
Input: nums = [3,7,5]
Output: 3
Explanation:
When k = 3, nonPositive(nums, k) = 6 <= k2.
- Reduce
nums[0] = 3one time.nums[0]becomes3 - 3 = 0. - Reduce
nums[1] = 7three times.nums[1]becomes7 - 3 - 3 - 3 = -2. - Reduce
nums[2] = 5two times.nums[2]becomes5 - 3 - 3 = -1.
Example 2:
Input: nums = [1]
Output: 1
Explanation:
When k = 1, nonPositive(nums, k) = 1 <= k2.
- Reduce
nums[0] = 1one time.nums[0]becomes1 - 1 = 0.
Β
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 105
Solutions
Solution 1: Binary Search
We notice that as \(k\) increases, it becomes easier to satisfy the condition. This exhibits monotonicity, so we can use binary search to find the minimum \(k\).
We define the left boundary of the binary search as \(l = 1\) and the right boundary as \(r = 10^5\). In each binary search iteration, we calculate the middle value \(mid = \lfloor (l + r) / 2 \rfloor\) and determine whether the condition \(\text{nonPositive}(\text{nums}, k) \leq k^2\) is satisfied when \(k = mid\). If the condition is satisfied, we update the right boundary to \(r = mid\); otherwise, we update the left boundary to \(l = mid + 1\). When the binary search ends, the left boundary \(l\) is the minimum \(k\) we are looking for.
The time complexity is \(O(n \log M)\), where \(n\) and \(M\) are the length of the array \(\textit{nums}\) and the maximum range respectively. The space complexity is \(O(1)\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |