2064. Minimized Maximum of Products Distributed to Any Store
UnknownView on LeetCode
Time: O(n log max)
Space: O(1)
Problem Overview
Minimized Maximum of Products Distributed to Any Store (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Binary Search pattern in coding interviews. Binary search on max products per store; validate by summing ceil(qty/mid) for each product type.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Binary search on max products per store; validate by summing ceil(qty/mid) for each product type.
Related patterns: Array, Binary Search
2064.cs
C#
// Approach: Binary search on max products per store; validate by summing ceil(qty/mid) for each product type.
// Time: O(n log max) Space: O(1)
public class Solution
{
public int MinimizedMaximum(int n, int[] quantities)
{
int l = 1;
int r = quantities.Max();
while (l < r)
{
int m = (l + r) / 2;
if (NumStores(quantities, m) <= n)
r = m;
else
l = m + 1;
}
return l;
}
private int NumStores(int[] quantities, int m)
{
// ceil(q / m)
return quantities.Sum(q => (q - 1) / m + 1);
}
}Was this solution helpful?
Related Problems
- 4. Median of Two Sorted Arrays(Hard)
- 11. Container With Most Water(Medium)
- 15. 3Sum(Medium)
- 16. 3Sum Closest(Medium)
- 26. Remove Duplicates from Sorted Array(Easy)
- 27. Remove Element(Easy)