< 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)
 724    {
 725        switch (nums.Length)
 26        {
 27            case 1:
 128                return nums[0];
 29            case 2:
 130                return Math.Max(nums[0], nums[1]);
 31        }
 32
 1533        int? firstMax = null, secondMax = null, thirdMax = null;
 34
 4735        foreach (var num in nums)
 1636        {
 1637            if (num == firstMax || num == secondMax || num == thirdMax)
 238            {
 239                continue;
 40            }
 41
 1442            if (!firstMax.HasValue || num > firstMax)
 943            {
 944                thirdMax = secondMax;
 945                secondMax = firstMax;
 946                firstMax = num;
 947            }
 548            else if (!secondMax.HasValue || num > secondMax)
 249            {
 250                thirdMax = secondMax;
 251                secondMax = num;
 252            }
 353            else if (!thirdMax.HasValue || num > thirdMax)
 354            {
 355                thirdMax = num;
 356            }
 1457        }
 58
 559        return thirdMax ?? firstMax.GetValueOrDefault();
 760    }
 61}

Methods/Properties

ThirdMax(System.Int32[])