https://school.programmers.co.kr/learn/courses/30/lessons/176962#qna

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

function solution(plans) {
  var answer = [];
  var notFinish = [];

  let plan = plans.sort((a, b) => {
    const [aHours, aMinutes] = a[1].split(':').map(Number);
    const [bHours, bMinutes] = b[1].split(':').map(Number);
    return aHours * 60 + aMinutes - (bHours * 60 + bMinutes);
  });

  for (let idx = 0; idx < plan.length; idx++) {
    const p = plan[idx];
    const [hours, minutes] = p[1].split(':').map(Number);
    const date = new Date();
    date.setHours(hours, minutes, 0, 0);

    const playTime = parseInt(p[2]) * 60 * 1000;
    date.setTime(date.getTime() + playTime);

    if (idx + 1 < plan.length) {
      const [nextHours, nextMinutes] = plan[idx + 1][1].split(':').map(Number);
      const nextDate = new Date();
      nextDate.setHours(nextHours, nextMinutes, 0, 0); 

      if (nextDate < date) {
      
        notFinish.push([p[0], (date - nextDate) / (60 * 1000)]); 
      } else {
        answer.push(p[0]);

      
        let remainingTime = (nextDate - date) / (60 * 1000); 
        while (notFinish.length > 0 && remainingTime > 0) {
          const [task, timeLeft] = notFinish.pop(); 
          if (remainingTime >= timeLeft) {
            answer.push(task);
            remainingTime -= timeLeft;
          } else {
            notFinish.push([task, timeLeft - remainingTime]);
            break;
          }
        }
      }
    } else {
      answer.push(p[0]); 

      while (notFinish.length > 0) {
        answer.push(notFinish.pop()[0]);
      }
    }
  }

  return answer;
}

조건문지옥....................... 

 

스택을 활용해서 풀면 된다 

물론 자바스크립트는 스택이나 큐를 제공하지 않기 때문에 shift push pop 을 적절히 사용해서 배열을 스택처럼 써야한다. 

 

 

오답노트 : 

 

다음 과제 시작 전에 미뤄둔 과제를 처리해줘야하는데

미뤄둔 과제 리스트중에 첫번째 과제가 40분이고 다음 과제까지 남은시간이 20분이다

그러면 20분이라도 했다고 처리해줘야 문제가 해결되는거였음

 

처음에 풀때

남은시간은 20분인데 40분짜리 과제? 절대못하죠.  이러고 넘어가버려서 절반이 오류케이스로 나왔었다 

테스트케이스에 이런경우도 같이 고려해서 넣어주면 좋겠음. 설명이 부족한 문제인듯 

+ Recent posts