< Summary

Information
Class: LeetCode.Algorithms.TwoBestNonOverlappingEvents.TwoBestNonOverlappingEventsBinarySearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\TwoBestNonOverlappingEvents\TwoBestNonOverlappingEventsBinarySearch.cs
Line coverage
100%
Covered lines: 30
Uncovered lines: 0
Coverable lines: 30
Total lines: 65
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MaxTwoEvents(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\TwoBestNonOverlappingEvents\TwoBestNonOverlappingEventsBinarySearch.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.TwoBestNonOverlappingEvents;
 13
 14/// <inheritdoc />
 15public class TwoBestNonOverlappingEventsBinarySearch : ITwoBestNonOverlappingEvents
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n log n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="events"></param>
 22    /// <returns></returns>
 23    public int MaxTwoEvents(int[][] events)
 324    {
 1225        Array.Sort(events, (a, b) => a[1] - b[1]);
 26
 327        var maxSum = 0;
 328        var maxValueBefore = new int[events.Length];
 329        var maxValueSoFar = 0;
 30
 2431        for (var i = 0; i < events.Length; i++)
 932        {
 933            var startTime = events[i][0];
 934            var value = events[i][2];
 35
 936            var low = 0;
 937            var high = i - 1;
 938            var maxPreviousValue = 0;
 39
 1740            while (low <= high)
 841            {
 842                var mid = (low + high) / 2;
 43
 844                if (events[mid][1] < startTime)
 445                {
 446                    maxPreviousValue = Math.Max(maxPreviousValue, maxValueBefore[mid]);
 47
 448                    low = mid + 1;
 449                }
 50                else
 451                {
 452                    high = mid - 1;
 453                }
 854            }
 55
 956            maxSum = Math.Max(maxSum, maxPreviousValue + value);
 57
 958            maxValueSoFar = Math.Max(maxValueSoFar, value);
 59
 960            maxValueBefore[i] = maxValueSoFar;
 961        }
 62
 363        return maxSum;
 364    }
 65}

Methods/Properties

MaxTwoEvents(System.Int32[][])