Similar Problems

Similar Problems not available

Minimize Maximum Of Array - Leetcode Solution

Companies:

LeetCode:  Minimize Maximum Of Array Leetcode Solution

Difficulty: Medium

Topics: greedy dynamic-programming binary-search array prefix-sum  

Problem Statement:

Given an array nums and a value target, return the minimum possible value of max(nums[i], nums[j], nums[k]) where i, j, and k are indices of the array and max(nums[i], nums[j], nums[k]) <= target.

Example:

Input: nums = [1,2,3,4,5,6,7,8,9], target = 10

Output: 5

Explanation: The maximum of any three numbers in the array is 9, and it is less than or equal to the target value of 10.

Solution:

This problem can be solved using binary search. We will first define the minimum and maximum possible values that the maximum value of the array can take. The minimum possible value is 0, and the maximum possible value is the largest value in the array.

We will then perform binary search on the possible range of values for the maximum value of the array. For each value that we test, we will check if there exist i, j, k such that max(nums[i], nums[j], nums[k]) is less than or equal to the current tested value. If such i, j, k exist, then we will search for a smaller value of the maximum value, otherwise we will search for a larger value.

Let's define the function check, which will check if there exist i, j, k such that max(nums[i], nums[j], nums[k]) is less than or equal to the current tested value:

def check(nums, target, max_value): # i, j, and k are indices of the array and max(nums[i], nums[j], nums[k]) <= max_value

count = 0
n = len(nums)
i = 0

while i < n:
    j = i + 1
    k = n - 1
    
    while j < k:
        if nums[i] + nums[j] + nums[k] <= max_value:
            count += k - j  # Any k greater than j will be valid
            j += 1
        else:
            k -= 1  # Reduce the value of k, so that the sum reduces
            
    i += 1

return count >= target

In the above function, we are using two-pointers to find all possible i, j, k such that max(nums[i], nums[j], nums[k]) is less than or equal to the current tested value. count keeps track of the number of such possible i, j, k. If it is greater than or equal to the target, then we return True, otherwise False.

Now we will use binary search to find the minimum value of max(nums[i], nums[j], nums[k]) for which check returns True:

def minimize_max(nums, target): left = 0 right = max(nums)

while left < right:
    mid = left + (right - left) // 2
    
    if check(nums, target, mid):
        right = mid
    else:
        left = mid + 1

return left

In the above function, we are performing binary search on the possible range of values for the maximum value of the array. If check returns True for the mid value, then we search for a smaller value to the left of mid, otherwise we search for a larger value to the right of mid. We continue this process until left and right converge, which means we have found the minimum possible value of max(nums[i], nums[j], nums[k]).

Time Complexity:

The time complexity of this solution is O(n^2 log n), where n is the length of the array nums. The check function has a time complexity of O(n^2), as we are using two-pointers to find all possible i, j, k. We are performing binary search on the range of values from 0 to the largest value in the array, which has a time complexity of O(log n). Therefore, the overall time complexity is O(n^2 log n).

Space Complexity:

The space complexity of this solution is O(1), as we are not using any additional data structure.

Minimize Maximum Of Array Solution Code

1