< Summary

Information
Class: LeetCode.Algorithms.MaximumNumberOfTasksYouCanAssign.MaximumNumberOfTasksYouCanAssignLinkedList
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximumNumberOfTasksYouCanAssign\MaximumNumberOfTasksYouCanAssignLinkedList.cs
Line coverage
92%
Covered lines: 50
Uncovered lines: 4
Coverable lines: 54
Total lines: 103
Line coverage: 92.5%
Branch coverage
86%
Covered branches: 19
Total branches: 22
Branch coverage: 86.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MaxTaskAssign(...)100%44100%
CanComplete(...)83.33%191888.23%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximumNumberOfTasksYouCanAssign\MaximumNumberOfTasksYouCanAssignLinkedList.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.MaximumNumberOfTasksYouCanAssign;
 13
 14/// <inheritdoc />
 15public class MaximumNumberOfTasksYouCanAssignLinkedList : IMaximumNumberOfTasksYouCanAssign
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n log n + m log m + min(n, m) × log(min(n, m)))
 19    ///     Space complexity - O(min(n, m))
 20    /// </summary>
 21    /// <param name="tasks"></param>
 22    /// <param name="workers"></param>
 23    /// <param name="pills"></param>
 24    /// <param name="strength"></param>
 25    /// <returns></returns>
 26    public int MaxTaskAssign(int[] tasks, int[] workers, int pills, int strength)
 427    {
 428        Array.Sort(tasks);
 429        Array.Sort(workers);
 30
 431        var left = 0;
 432        var right = Math.Min(tasks.Length, workers.Length);
 433        var result = 0;
 34
 1535        while (left <= right)
 1136        {
 1137            var mid = left + ((right - left) / 2);
 38
 1139            if (CanComplete(tasks, workers, pills, strength, mid))
 840            {
 841                result = mid;
 42
 843                left = mid + 1;
 844            }
 45            else
 346            {
 347                right = mid - 1;
 348            }
 1149        }
 50
 451        return result;
 452    }
 53
 54    private static bool CanComplete(int[] tasks, int[] workers, int pills, int strength, int k)
 1155    {
 1156        if (k == 0)
 057        {
 058            return true;
 59        }
 60
 1161        if (k > workers.Length)
 062        {
 063            return false;
 64        }
 65
 1166        var linkedList = new LinkedList<int>();
 1167        var workerIndex = workers.Length - 1;
 1168        var usedPills = 0;
 69
 5670        for (var i = k - 1; i >= 0; i--)
 2071        {
 4072            while (workerIndex >= workers.Length - k && workers[workerIndex] + strength >= tasks[i])
 2073            {
 2074                linkedList.AddLast(workers[workerIndex]);
 75
 2076                workerIndex--;
 2077            }
 78
 2079            if (linkedList.Count == 0)
 180            {
 181                return false;
 82            }
 83
 1984            if (linkedList.First != null && linkedList.First.Value >= tasks[i])
 1185            {
 1186                linkedList.RemoveFirst();
 1187            }
 88            else
 889            {
 890                if (usedPills >= pills)
 291                {
 292                    return false;
 93                }
 94
 695                usedPills++;
 96
 697                linkedList.RemoveLast();
 698            }
 1799        }
 100
 8101        return true;
 11102    }
 103}