개발하는 사막여우
개발하는 사막여우
개발하는 사막여우
전체 방문자
오늘
어제
  • All (310)
    • Books (13)
      • 읽기 좋은 코드가 좋은 코드다 (13)
    • Study (6)
      • Blockchain (3)
      • Algorithm (3)
    • Baekjoon (36)
    • Programmers (166)
    • LeetCode (15)
    • Open Source (1)
      • Youtube Popout Player (1)
    • Language (32)
      • Python (9)
      • JS (8)
      • Java (5)
      • HTML (6)
      • CSS (4)
    • Library & Framework (15)
      • React.js (15)
    • IDE (2)
      • IntelliJ (2)
    • Airdrop (9)
    • Tistory (2)
    • etc.. (12)
      • Cozubi (6)
      • lol-chess (0)

블로그 메뉴

  • Github

공지사항

인기 글

태그

  • Cozubi
  • 2018 KAKAO BLIND RECRUITMENT
  • 신규 코인 에어드랍
  • 코딩테스트연습
  • 코주비
  • 백준
  • Java
  • programmers
  • 클린 코드
  • 카카오 알고리즘 문제
  • 카카오 공채
  • 프로그래머스 위클리 챌린지
  • 카카오 코딩테스트
  • 코인줍줍
  • 클린 코드 작성법
  • 알고리즘문제풀이
  • 프로그래머스
  • Python
  • 파이썬
  • 읽기 좋은 코드가 좋은 코드다

최근 댓글

최근 글

티스토리

반응형
hELLO · Designed By 정상우.
개발하는 사막여우

개발하는 사막여우

[리트코드] 1647. Minimum Deletions to Make Character Frequencies Unique / Javascript
LeetCode

[리트코드] 1647. Minimum Deletions to Make Character Frequencies Unique / Javascript

2021. 9. 8. 14:20
반응형

문제주소 :https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/

 

Minimum Deletions to Make Character Frequencies Unique - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com


<문제 설명>

더보기

A string s is called good if there are no two different characters in s that have the same frequency.

Given a string s, return the minimum number of characters you need to delete to make s good.

The frequency of a character in a string is the number of times it appears in the string. For example, in the string "aab", the frequency of 'a' is 2, while the frequency of 'b' is 1.

 

Example 1:

Input: s = "aab" Output: 0 Explanation: s is already good.

Example 2:

Input: s = "aaabbbcc" Output: 2 Explanation: You can delete two 'b's resulting in the good string "aaabcc". Another way it to delete one 'b' and one 'c' resulting in the good string "aaabbc".

Example 3:

Input: s = "ceabaacb" Output: 2 Explanation: You can delete both 'c's resulting in the good string "eabaab". Note that we only care about characters that are still in the string at the end (i.e. frequency of 0 is ignored).

 

Constraints:

  • 1 <= s.length <= 105
  • s contains only lowercase English letters.

 

<풀이법>

▒ 한줄 개념: First-Fit ▒ 

메모리 관리 기법 중 First-Fit 방식이 생각나서 그걸 적용해서 풀어봤습니다.

문제가 풀리긴 했으나 좋은 방식은 아닌 것 같습니다.

풀이 방식은 다음과 같습니다.

 

1. char 별 등장 횟수 계산

2. 횟수들을 순서대로 탐색하며 First-Fit 방식을 사용하여 계산

 

<코드(Javascript)>

/**
 * @param {string} s
 * @return {number}
 */
var minDeletions = function(s) {
    let answer = 0;
    const arr = s.split('');
    const dict = {};
    const mem = [];
    arr.map(char => {
        dict[char] = dict[char] ? dict[char] + 1 : 1;
    })
    const values = Object.values(dict);
    values.map(v => {
        if(mem[v]){
            let nextPos = v;
            while(nextPos > 0){
                if(!mem[nextPos]){
                    mem[nextPos] = true;
                    answer += v - nextPos;
                    break;
                }else{
                    nextPos--;
                }
            }
            if(nextPos === 0){
                answer += v;
            }
        }else{
            mem[v] = true;
        }
    })
    return answer;
};

 

 

더 많은 코드 보기(GitHub) : github.com/dwkim-97/CodingTest

 

 

반응형
저작자표시 (새창열림)

'LeetCode' 카테고리의 다른 글

[리트코드] 2. Add Two Numbers / Javascript  (0) 2021.09.09
[리트코드] 1130. Minimum Cost Tree From Leaf Values / Javascript  (0) 2021.09.08
[리트코드] 6. ZigZag Conversion / Javascript  (0) 2021.09.07
[리트코드] 142. Linked List Cycle II / Javascript  (0) 2021.06.08
[리트코드] 141. Linked List Cycle / Javascript  (0) 2021.06.08
    'LeetCode' 카테고리의 다른 글
    • [리트코드] 2. Add Two Numbers / Javascript
    • [리트코드] 1130. Minimum Cost Tree From Leaf Values / Javascript
    • [리트코드] 6. ZigZag Conversion / Javascript
    • [리트코드] 142. Linked List Cycle II / Javascript
    개발하는 사막여우
    개발하는 사막여우
    개발개발 주저리주저리

    티스토리툴바