본문 바로가기
Programmers

[Java] 프로그래머스 : 기능개발

by 엘딘 2022. 7. 11.

https://school.programmers.co.kr/learn/courses/30/lessons/42586

 

프로그래머스

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

programmers.co.kr

 

 

 

 

 

문제

 

 

 ·  ArrayList 사용 : ArrayList는 처음 선언해줄 때 길이 지정을 해주지 않아도 된다

 ·  배열 : 처음 선언때 길이 지정이 필요

 

 ·  3번 줄 : number는 answer 배열에 넣을 값들을 임시 보관해주는 ArrayList 변수

      return해줄 값들이 2개가 될지 3개가 될지 알 수 없기때문에 ArrayList를 사용

 ·  11번 줄 : i번째의 값을 progress에 넣어준 후 15번 줄에서 speeds의 i번째 값들을 더해주는 역할

      13번 줄의 for문을 통해 progress가 100이 넘을때까지 계속 돌려준다. 

      progress가 100이 넘어갈 경우 17번 줄을 통해 speeds[i]를 몇 번 더했는지 num에 넣어 준 후 break

 ·  23번 줄~37번 줄 : num[]에 넣어 준 수(speeds[i]를 더한 횟 수)들의 크기를 비교하여 앞 수보다 뒷 수가 크면 break

      앞 수가 크면 count++해주며 작은 뒷수(num[j])는 0으로 바꿔준다.

      30번 줄에서 작은 수를 0으로 만든 이유는 26번 줄에서 0인 숫자를 걸러내면서 숫자 크기 비교를 하기 위해서

 ·  35번 줄 : count + 1을 한 이유는 앞 숫자가 크면 count++해주나 0으로 바꿔준 작은 수까지는 count 해주지 않으며 

      마지막으로 하나 남은 숫자는 비교할 숫자가 없으므로 +1해줌

 ·  24번 줄 : count를 전역변수로 지정하지 않은 이유는 23번 줄 첫번째 for문이 다시 돌 땐 초기화가 되어야 하기때문

 ·  40번 줄~42번 줄 : answer는 int로 return해주기 때문에 Integer타입만 가능한 ArrrayList를 배열(int)로 바꿔준다.

      

<제가 작성한 코드는 느려서 테스트는 통과하나 오답처리 됨. 물론, 큐/스택도 사용안함>
import java.util.*;

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        ArrayList<Integer> number = new ArrayList<>();
		int[] num = new int[progresses.length];
		
		int i, j;
		
        for(i = 0; i < progresses.length; i++ ) {
            int progress = progresses[i];

            for(j = 0; j < 100; j++) {
                if(100 > progress) {
                    progress += speeds[i];
                }else {
                    num[i] = j;
                    break;
                }
            }
        }

        for(i = 0; i < progresses.length; i++) {
            int count = 0;

            if(num[i] != 0) {
                for(j = i+1; j < progresses.length; j++) {
                    if(num[i]>num[j]) {
                        count++;
                        num[j] = 0;
                    }else {
                        break;
                    }
                }
                number.add(count + 1);
            }
        }

        int[] answer = new int[number.size()];
        for(i = 0; i < answer.length; i++){
            answer[i] = number.get(i);
        }

        return answer;
    }
}

 

 

다른 분의 코드 (하... 짧고 빠르다...)
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();
    }
}

 

 

Queue사용한 코드
import java.util.*;

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        Queue<Integer> q = new LinkedList<>();
        List<Integer> answerList = new ArrayList<>();

        for (int i = 0; i < speeds.length; i++) {
            double remain = (100 - progresses[i]) / (double) speeds[i];
            int date = (int) Math.ceil(remain);

            if (!q.isEmpty() && q.peek() < date) {
                answerList.add(q.size());
                q.clear();
            }

            q.offer(date);
        }

        answerList.add(q.size());

        int[] answer = new int[answerList.size()];

        for (int i = 0; i < answer.length; i++) {
            answer[i] = answerList.get(i);
        }

        return answer;
    }
}

댓글