< Summary

Information
Class: LeetCode.Algorithms.MinimizedMaximumOfProductsDistributedToAnyStore.MinimizedMaximumOfProductsDistributedToAnyStoreBinarySearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimizedMaximumOfProductsDistributedToAnyStore\MinimizedMaximumOfProductsDistributedToAnyStoreBinarySearch.cs
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 57
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinimizedMaximum(...)100%44100%
CanDistribute(...)100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MinimizedMaximumOfProductsDistributedToAnyStore\MinimizedMaximumOfProductsDistributedToAnyStoreBinarySearch.cs

#LineLine coverage
 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
 12namespace LeetCode.Algorithms.MinimizedMaximumOfProductsDistributedToAnyStore;
 13
 14/// <inheritdoc />
 15public 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)
 326    {
 327        var left = 1;
 328        var right = quantities.Max();
 29
 330        var answer = right;
 31
 2832        while (left <= right)
 2533        {
 2534            var mid = left + ((right - left) / 2);
 35
 2536            if (CanDistribute(quantities, n, mid))
 637            {
 638                answer = mid;
 39
 640                right = mid - 1;
 641            }
 42            else
 1943            {
 1944                left = mid + 1;
 1945            }
 2546        }
 47
 348        return answer;
 349    }
 50
 51    private static bool CanDistribute(int[] quantities, int n, int maxPerStore)
 2552    {
 6253        var storesNeeded = quantities.Sum(quantity => (quantity + maxPerStore - 1) / maxPerStore);
 54
 2555        return storesNeeded <= n;
 2556    }
 57}