Skip to content

Commit 754440b

Browse files
authored
Create 414-third-maximum-number.js
1 parent a22a964 commit 754440b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

414-third-maximum-number.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const thirdMax = function(nums) {
6+
let count = 0, max=mid=small=Number.MIN_SAFE_INTEGER;
7+
for (let i in nums) {
8+
if (count > 0 && nums[i] === max || count > 1 && nums[i] === mid) continue;
9+
count++;
10+
if (nums[i] > max) {
11+
small = mid;
12+
mid = max;
13+
max = nums[i];
14+
} else if (nums[i] > mid) {
15+
small = mid;
16+
mid = nums[i];
17+
} else if (nums[i] > small) {
18+
small = nums[i];
19+
}
20+
}
21+
return count < 3 ? max : small;
22+
};

0 commit comments

Comments
 (0)