leet code - 414. Third Maximum Number
414. Third Maximum Number
Given integer array nums, return the third maximum number in this array. If the third maximum does not exist, return the maximum number.
Example 1:
Input: nums = [3,2,1]
Output: 1
Explanation: The third maximum is 1.
Example 2:
Input: nums = [1,2]
Output: 2
Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
Example 3:
Input: nums = [2,2,3,1]
Output: 1
Explanation: Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.
Constraints:
1 <= nums.length <= 104
-231 <= nums[i] <= 231 - 1
풀이 Runtime : 84ms, Memory : 41.7MB
var thirdMax = function(nums) {
let s = new Set();
let arr = [];
for(let i = 0; i < nums.length; i++){
s.add(nums[i]);
}
for(i of s){
arr.push(i);
}
arr.sort((a,b) => b-a);
if(arr[2] !== undefined) return arr[2];
return Math.max(...arr);
};
Author And Source
이 문제에 관하여(leet code - 414. Third Maximum Number), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@dlawogus/leet-code-414.-Third-Maximum-Number저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)