프로그래머스 고득점 키트 - 기능개발 java

메이도·2023년 7월 3일

알고리즘 테스트

목록 보기
5/5

오늘은 스택/큐의 기능개발 문제를 풀었다.
사실 지난번에 풀던건데 못풀어서 다시 풀음.

소요 시간 3시간 반.. 이게 맞는건가
https://school.programmers.co.kr/learn/courses/30/lessons/42586

나의 풀이

List<Integer> arrayList = new ArrayList<>();
        
int head = 0;
        
for(int i = 0; head < progresses.length; i++){
   //progress update
   for (int j = 0; j < progresses.length; j++){
       if(progresses[j] < 100)
           progresses[j] = progresses[j] + speeds[j];
       }
   //set result
       int deployCount = 0;
       for (int j = head; j < progresses.length; j++){
           if(progresses[j] >= 100){
               deployCount = deployCount + 1;
               head ++;
           }else{
               break;
           }
       }
       
       if(deployCount != 0){
          arrayList.add(deployCount);
       }
                
   }
        
   int[] answer = new int[arrayList.size()];
   for(int j = 0; j < arrayList.size(); j++){
       answer[j] = arrayList.get(j);
   }
   return answer;  
  1. 매일 progress를 speed에 맞게 업데이트
  2. 100이 넘은 프로세스가 있다면 head를 증가시켜 deploy가 어느 작업까지 됐는지를 확인한다.
  3. 해당 날짜에 작업이 완료된 프로세스가 있다면 리스트에 넣어둔다.

프로그래머스 다른 사람의 풀이

import java.util.ArrayList;
import java.util.Arrays;
class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        int[] dayOfend = new int[100];
        int day = -1;
        for(int i=0; i<progresses.length; i++) {
            while(progresses[i] + (day*speeds[i]) < 100) {
                day++;
            }
            dayOfend[day]++;
        }
        return Arrays.stream(dayOfend).filter(i -> i!=0).toArray();
    }
}

엄청나게 간결하다.
방법은
1. 프로세스(A)가 끝나는 날짜를 구한다. dayOfEnd + 1
2. 프로세스(A)의 다음 프로세스(B)가 해당 날짜에 작업이 끝났는지 확인
3. B의 작업이 끝나있다면 해당 dayOfEnd[day]++
4. B의 작업이 끝나지 않았다면 day를 새로 계산해서 dayOfEnd에 추가

자주자주 풀다보면 늘겠지!

0개의 댓글