Leetcode Problem 2513. Minimize the Maximum of Two Arrays

2513. Minimize the Maximum of Two Arrays

Leetcode Solutions

Binary Search on Answers

  1. Define a function isPossible(maxVal, divisor1, divisor2, uniqueCnt1, uniqueCnt2) that returns true if maxVal can be the maximum value in either array without violating the constraints.
  2. In isPossible, calculate the number of integers up to maxVal that are not divisible by divisor1 and divisor2 respectively, and the number of integers not divisible by either.
  3. Check if there are enough integers to satisfy uniqueCnt1 and uniqueCnt2 after accounting for integers that are not divisible by either divisor.
  4. Perform binary search on the range [1, 2 * (uniqueCnt1 + uniqueCnt2)] to find the minimum maximum value.
  5. If isPossible(mid) is true, update the answer and search the lower half; otherwise, search the upper half.
  6. Return the answer found by binary search.
UML Thumbnail

Iterative Incremental Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...