All files / leetCode 0414.ts

100% Statements 15/15
100% Branches 12/12
100% Functions 1/1
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21  3x 3x 3x   3x 9x 5x 5x 5x 4x 1x 1x 3x 2x       3x    
export default function thirdMax(nums: number[]): number {
  let max1 = Number.MIN_SAFE_INTEGER
  let max2 = Number.MIN_SAFE_INTEGER
  let max3 = Number.MIN_SAFE_INTEGER
 
  for (let i = 0; i < nums.length; i++) {
    if (nums[i] > max1) {
      max3 = max2
      max2 = max1
      max1 = nums[i]
    } else if (nums[i] > max2 && nums[i] < max1) {
      max3 = max2
      max2 = nums[i]
    } else if (nums[i] > max3 && nums[i] < max2) {
      max3 = nums[i]
    }
  }
 
  return max3 === Number.MIN_SAFE_INTEGER ? max1 : max3
}