< Summary

Information
Class: LeetCode.Algorithms.ThirdMaximumNumber.ThirdMaximumNumberLinear
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ThirdMaximumNumber\ThirdMaximumNumberLinear.cs
Line coverage
100%
Covered lines: 28
Uncovered lines: 0
Coverable lines: 28
Total lines: 61
Line coverage: 100%
Branch coverage
96%
Covered branches: 25
Total branches: 26
Branch coverage: 96.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ThirdMax(...)96.15%2626100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ThirdMaximumNumber\ThirdMaximumNumberLinear.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.ThirdMaximumNumber;
 13
 14/// <inheritdoc />
 15public class ThirdMaximumNumberLinear : IThirdMaximumNumber
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <returns></returns>
 23    public int ThirdMax(int[] nums)
 824    {
 825        switch (nums.Length)
 26        {
 27            case 1:
 128                return nums[0];
 29            case 2:
 130                return Math.Max(nums[0], nums[1]);
 31        }
 32
 1833        int? firstMax = null, secondMax = null, thirdMax = null;
 34
 5835        foreach (var num in nums)
 2036        {
 2037            if (num == firstMax || num == secondMax || num == thirdMax)
 338            {
 339                continue;
 40            }
 41
 1742            if (!firstMax.HasValue || num > firstMax)
 1143            {
 1144                thirdMax = secondMax;
 1145                secondMax = firstMax;
 1146                firstMax = num;
 1147            }
 648            else if (!secondMax.HasValue || num > secondMax)
 249            {
 250                thirdMax = secondMax;
 251                secondMax = num;
 252            }
 453            else if (!thirdMax.HasValue || num > thirdMax)
 454            {
 455                thirdMax = num;
 456            }
 1757        }
 58
 659        return thirdMax ?? firstMax.GetValueOrDefault();
 860    }
 61}

Methods/Properties

ThirdMax(System.Int32[])