| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.MinimizedMaximumOfProductsDistributedToAnyStore; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class |
| | 16 | | MinimizedMaximumOfProductsDistributedToAnyStoreBinarySearch : IMinimizedMaximumOfProductsDistributedToAnyStore |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Time complexity - O(n * log Q) |
| | 20 | | /// Space complexity - O(1) |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="n"></param> |
| | 23 | | /// <param name="quantities"></param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public int MinimizedMaximum(int n, int[] quantities) |
| 3 | 26 | | { |
| 3 | 27 | | var left = 1; |
| 3 | 28 | | var right = quantities.Max(); |
| | 29 | |
|
| 3 | 30 | | var answer = right; |
| | 31 | |
|
| 28 | 32 | | while (left <= right) |
| 25 | 33 | | { |
| 25 | 34 | | var mid = left + ((right - left) / 2); |
| | 35 | |
|
| 25 | 36 | | if (CanDistribute(quantities, n, mid)) |
| 6 | 37 | | { |
| 6 | 38 | | answer = mid; |
| | 39 | |
|
| 6 | 40 | | right = mid - 1; |
| 6 | 41 | | } |
| | 42 | | else |
| 19 | 43 | | { |
| 19 | 44 | | left = mid + 1; |
| 19 | 45 | | } |
| 25 | 46 | | } |
| | 47 | |
|
| 3 | 48 | | return answer; |
| 3 | 49 | | } |
| | 50 | |
|
| | 51 | | private static bool CanDistribute(int[] quantities, int n, int maxPerStore) |
| 25 | 52 | | { |
| 62 | 53 | | var storesNeeded = quantities.Sum(quantity => (quantity + maxPerStore - 1) / maxPerStore); |
| | 54 | |
|
| 25 | 55 | | return storesNeeded <= n; |
| 25 | 56 | | } |
| | 57 | | } |