< Summary

Information
Class: LeetCode.Algorithms.AppleRedistributionIntoBoxes.AppleRedistributionIntoBoxesGreedy
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AppleRedistributionIntoBoxes\AppleRedistributionIntoBoxesGreedy.cs
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 69
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinimumBoxes(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AppleRedistributionIntoBoxes\AppleRedistributionIntoBoxesGreedy.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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.AppleRedistributionIntoBoxes;
 13
 14/// <inheritdoc />
 15public sealed class AppleRedistributionIntoBoxesGreedy : IAppleRedistributionIntoBoxes
 16{
 17    private const int MaxSize = 50;
 18
 19    /// <summary>
 20    ///     Time complexity - O(n + m)
 21    ///     Space complexity - O(1)
 22    /// </summary>
 23    /// <param name="apples"></param>
 24    /// <param name="capacities"></param>
 25    /// <returns></returns>
 26    public int MinimumBoxes(int[] apples, int[] capacities)
 227    {
 228        var applesCount = 0;
 29
 1630        for (var i = 0; i < apples.Length; i++)
 631        {
 632            var apple = apples[i];
 33
 634            applesCount += apple;
 635        }
 36
 237        Span<int> boxes = stackalloc int[MaxSize + 1];
 38
 2239        for (var i = 0; i < capacities.Length; i++)
 940        {
 941            var capacity = capacities[i];
 42
 943            boxes[capacity]++;
 944        }
 45
 246        var result = 0;
 47
 20448        for (var i = boxes.Length - 1; i >= 0; i--)
 10149        {
 10150            if (applesCount == 0)
 151            {
 152                break;
 53            }
 54
 10055            var box = boxes[i];
 56
 10657            while (box > 0 && applesCount > 0)
 658            {
 659                applesCount -= i;
 60
 661                box--;
 62
 663                result++;
 664            }
 10065        }
 66
 267        return result;
 268    }
 69}