LeetCode 2513: Minimize the Maximum of Two Arrays

Problem Description

Explanation:

To solve this problem, we need to distribute numbers in two arrays arr1 and arr2 such that they satisfy the given conditions. We want to minimize the maximum value in either array. We can start by distributing numbers in such a way that we minimize the maximum value.

We know that we need to have uniqueCnt1 distinct numbers in arr1 and uniqueCnt2 distinct numbers in arr2, and none of these numbers should be divisible by divisor1 or divisor2 respectively. We can start by assigning the smallest possible numbers to these arrays.

We can start by iterating from 1 and keep adding numbers to arr1 and arr2 such that they do not violate the conditions provided. We can stop this process when both arr1 and arr2 have the required number of distinct elements. The maximum value in either array at this point would be the answer.

The time complexity of this approach is O(n) where n is the sum of uniqueCnt1 and uniqueCnt2. The space complexity is O(1) as we are not using any extra space apart from the input and output variables.

Solutions

class Solution {
    public int minimizeTheMax(int divisor1, int divisor2, int uniqueCnt1, int uniqueCnt2) {
        int maxVal = 0;
        int count1 = 0, count2 = 0;
        
        for (int i = 1; count1 < uniqueCnt1 || count2 < uniqueCnt2; i++) {
            if (i % divisor1 != 0 && count1 < uniqueCnt1) {
                maxVal = Math.max(maxVal, i);
                count1++;
            }
            if (i % divisor2 != 0 && count2 < uniqueCnt2) {
                maxVal = Math.max(maxVal, i);
                count2++;
            }
        }
        
        return maxVal;
    }
}

Loading editor...