KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • 문자열
  • 수학
  • string
  • LeetCode
  • C++
  • recursion
  • tree
  • Level 1
  • codeup
  • C
  • programmers
  • Level 0
  • Level1
  • 그래프
  • 제코베 JS 100제
  • 다이나믹 프로그래밍
  • Level 2
  • 정렬
  • 백준
  • js

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Heap / 692번 / Top K Frequent Words / JS

2023. 4. 18. 01:44

< 문제 바로가기 >

 

Top K Frequent Words - LeetCode

Can you solve this real interview question? Top K Frequent Words - Given an array of strings words and an integer k, return the k most frequent strings. Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequenc

leetcode.com

 

< 문제 간단설명 >

단어들이 들어있는 배열 words 배열과 정수인 k가 들어온다. words 배열에서 가장 많이 나온 단어들을 순서대로 k개만 반환한다. 이 때, 사전식으로 정렬하여 반환한다.

 

/**
 * @param {string[]} words
 * @param {number} k
 * @return {string[]}
 */
var topKFrequent = function(words, k) {
    const customSort = (a, b) => {
        let [stringA, countA] = a;
        let [stringB, countB] = b;

        if(countA === countB) {
            // 사전식으로 했을 때 stringA가 stringB 앞에 올 경우 음수를 반환하고,
            // 같다면 0,
            // 뒤에 올 경우엔 양수를 반환한다.
            return stringA.localeCompare(stringB);
        }
        else {
            // 양수면 자리를 바꾸고, 아니면 바꾸지 않는다.
            return countB - countA;
        }
    };

    let wordCountMap = new Map();

    words.forEach((word) => {
        wordCountMap.set(word, wordCountMap.get(word) + 1 || 1);
    });

    let arr = Array.from(wordCountMap).sort((a, b) => customSort(a, b));
    arr = arr.map((el) => el[0]).splice(0, k);
    
    return arr;
};

 

저작자표시 (새창열림)

'PS > LeetCode' 카테고리의 다른 글

LeetCode / Simulation / 54번 / Spiral Matrix / JS  (0) 2023.04.18
LeetCode / Implementation / 202번 / Happy Number / JS  (0) 2023.04.18
LeetCode / Heap / 1046번 / Last Stone Weight / JS  (0) 2023.04.18
LeetCode / Stack / 394번 / Decode String / JS  (0) 2023.04.18
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Simulation / 54번 / Spiral Matrix / JS
    • LeetCode / Implementation / 202번 / Happy Number / JS
    • LeetCode / Heap / 1046번 / Last Stone Weight / JS
    • LeetCode / Stack / 394번 / Decode String / JS
    KimMinJun
    KimMinJun

    티스토리툴바