LeetCode [JS] > Third Maximum Number

2025. 3. 26. 23:53·🔒Algorithm

✅문제

https://leetcode.com/problems/third-maximum-number/description/

✅풀이

Set으로 중복제거하고 시작하면 편하게 구할 수 있다. 시간복잡도는 O(NlogN) 으로 이런 이유는 js sorting algorithm 이 tim sort 방식을 사용하기 때문이다. 참고로 tim sort 방식은 merge sort 와 insertion sort를 결합하여 만든 sorting 방식이라고 한다. 자세히 알고 싶으면 https://d2.naver.com/helloworld/0315536 를 보면 된다.

/**
 * @param {number[]} nums
 * @return {number}
 */
var thirdMax = function (nums) {
    const sorted = [...new Set(nums)].sort((prev, next) => next - prev);

    let answer = undefined;
    for (let i = 0; i < 3; ++i) {
        const num = sorted[i];
        if (num === undefined) {
            return sorted[0];
        }

        answer = num;
    }

    return answer;
};

'🔒Algorithm' 카테고리의 다른 글

LeetCode > 2591. Distribute Money to Maximum Children  (0) 2025.03.28
LeetCode [JS] > 392. Is Subsequence  (0) 2025.03.27
HackerRank > big-sorting  (0) 2025.03.22
LeetCode[JS] > 2718. Sum of Matrix After Queries  (0) 2025.03.15
LeetCode [JS] > 3192. Minimum Operations to Make Binary Array Elements Equal to One II  (0) 2025.03.12
'🔒Algorithm' 카테고리의 다른 글
  • LeetCode > 2591. Distribute Money to Maximum Children
  • LeetCode [JS] > 392. Is Subsequence
  • HackerRank > big-sorting
  • LeetCode[JS] > 2718. Sum of Matrix After Queries
devWarrior
devWarrior
  • devWarrior
    devWarrior
    devWarrior
  • 전체
    오늘
    어제
    • 🧩Dev (263)
      • ⭐FE (34)
      • 🔒Algorithm (155)
      • ➕Etc. (11)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 글쓰기
    • 관리
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    dp
    티스토리챌린지
    react
    오블완
    실버4
    실버2
    leetcode
    그리디
    코딩테스트
    js
    알고리즘
    코테
    BFS
    실버1
    FE
    Algorithm
    Easy
    프론트엔드
    프로그래머스
    자스
    node.js
    실버3
    자바스크립트
    nodejs
    백준
    구현
    Lv2
    골드5
    DFS
    javascript
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
devWarrior
LeetCode [JS] > Third Maximum Number
상단으로

티스토리툴바