| | 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 | |
|
| | 12 | | namespace LeetCode.Algorithms.ThirdMaximumNumber; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class ThirdMaximumNumberDistinctOrder : IThirdMaximumNumber |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n log n) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="nums"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int ThirdMax(int[] nums) |
| 8 | 24 | | { |
| 8 | 25 | | var numsLength = nums.Length; |
| | 26 | |
|
| 8 | 27 | | switch (numsLength) |
| | 28 | | { |
| | 29 | | case 1: |
| 1 | 30 | | return nums[0]; |
| | 31 | | case 2: |
| 1 | 32 | | return Math.Max(nums[0], nums[1]); |
| | 33 | | } |
| | 34 | |
|
| 6 | 35 | | var distinct = nums.Distinct().Order().ToArray(); |
| | 36 | |
|
| 6 | 37 | | var distinctLength = distinct.Length; |
| | 38 | |
|
| 6 | 39 | | return distinctLength < 3 ? distinct.Last() : distinct[distinctLength - 3]; |
| 8 | 40 | | } |
| | 41 | | } |