< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Insert(...)100%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\InsertInterval\InsertIntervalIterative.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.InsertInterval;
 13
 14/// <inheritdoc />
 15public class InsertIntervalIterative : IInsertInterval
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="intervals"></param>
 22    /// <param name="newInterval"></param>
 23    /// <returns></returns>
 24    public int[][] Insert(int[][] intervals, int[] newInterval)
 625    {
 626        var result = new List<int[]>();
 27
 628        var i = 0;
 29
 930        while (i < intervals.Length && intervals[i][1] < newInterval[0])
 331        {
 332            result.Add(intervals[i]);
 333            i++;
 334        }
 35
 1036        while (i < intervals.Length && intervals[i][0] <= newInterval[1])
 437        {
 438            newInterval[0] = Math.Min(newInterval[0], intervals[i][0]);
 439            newInterval[1] = Math.Max(newInterval[1], intervals[i][1]);
 440            i++;
 441        }
 42
 643        result.Add(newInterval);
 44
 1045        while (i < intervals.Length)
 446        {
 447            result.Add(intervals[i]);
 448            i++;
 449        }
 50
 651        return [.. result];
 652    }
 53}