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
  • 정렬
  • js
  • C++
  • 문자열
  • Level 0
  • C
  • 다이나믹 프로그래밍
  • programmers
  • 백준
  • 그래프
  • 제코베 JS 100제
  • Level1
  • Level 2
  • recursion
  • Level 1
  • codeup
  • tree
  • LeetCode

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Greedy / 621번 / Task Scheduler / JS

2023. 4. 28. 17:26

< 문제 바로가기 >

 

Task Scheduler - LeetCode

Can you solve this real interview question? Task Scheduler - Given a characters array tasks, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks could be done in any order. Each task is done in one unit of time. F

leetcode.com

 

< 문제 간단설명 >

해야하는 작업들의 리스트와 n이 주어진다. 여기서 n은 같은 작업을 잇따라 해야할 시에 중간에 반드시 텀을 둬야하는 시간이다.(유휴시간)

예를들어 tasks = ['A', 'A', 'A', 'B', 'B', 'B'], n = 2 라면, A -> B -> idle -> A -> B -> idle -> A -> B 순으로 해야한다.

보면 같은 색의 같은 작업 사이에는 2칸을 반드시 건너뛰는것을 볼 수 있다.

 

/**
 * @param {character[]} tasks
 * @param {number} n
 * @return {number}
 */
var leastInterval = function(tasks, n) {
    if(n === 0) {
        return tasks.length;
    }

    let countMap = tasks.reduce((acc, cur) => {
        acc.set(cur, acc.get(cur) + 1 || 1);
        return acc;
    }, new Map());

    let countArr = Array.from(countMap).sort((a, b) => b[1] - a[1]);
    let [maxTask, maxCount] = countArr.at(0);

    // maxCount에 1을 빼는 이유는 마지막에 idle이 존재하지 않음
    // 만약 ['A', 'A', 'A', 'B', 'B', 'B'] 이고 n = 2 라면,
    // maxCount = 3 이므로, (3 - 1) * 2 = 4
    // A, _, _, A, _, _, A => idleCount = 4
    let idleCount = (maxCount - 1) * n;

    for(const [task, count] of countMap) {
        // idleCount를 구할 때 이미 maxTask의 count로 초기화를 해놓음
        if(task === maxTask) {
            continue;
        }

        // maxCount와 같을 경우 또한 마지막에 idle이 없으므로 count - 1
        // 따라서 아래와 같이 됨. (위에서 구한 idle을 채워넣는 느낌)
        // A, B, _, A, B, _, A, B
        if(count === maxCount) {
            idleCount -= (count - 1);
        }
        // 중간에 빈 idle에 채워넣음
        else {
            idleCount -= count;
        }
    }

    // idleCount가 0 미만으로 내려가면 양 끝에 임의로 배치하면 되므로 0으로 초기화
    idleCount = idleCount < 0 ? 0 : idleCount;
    let result = tasks.length + idleCount;
    return result;
};
저작자표시

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

LeetCode / Tree / 110번 / Balanced Binary Tree / JS  (0) 2023.04.28
LeetCode / Tree / 226번 / Invert Binary Tree / JS  (0) 2023.04.28
LeetCode / Greedy / 2131번 / Longest Palindrome by Concatenating Two Letter Words / JS  (0) 2023.04.28
LeetCode / Linked List / 148번 / Sort List / JS  (0) 2023.04.28
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Tree / 110번 / Balanced Binary Tree / JS
    • LeetCode / Tree / 226번 / Invert Binary Tree / JS
    • LeetCode / Greedy / 2131번 / Longest Palindrome by Concatenating Two Letter Words / JS
    • LeetCode / Linked List / 148번 / Sort List / JS
    KimMinJun
    KimMinJun

    티스토리툴바