문제
https://school.programmers.co.kr/learn/courses/30/lessons/42586
문제 설명
프로그래머스 팀에서는 기능 개선 작업을 수행 중입니다. 각 기능은 진도가 100%일 때 서비스에 반영할 수 있습니다.
또, 각 기능의 개발속도는 모두 다르기 때문에 뒤에 있는 기능이 앞에 있는 기능보다 먼저 개발될 수 있고, 이때 뒤에 있는 기능은 앞에 있는 기능이 배포될 때 함께 배포됩니다.
먼저 배포되어야 하는 순서대로 작업의 진도가 적힌 정수 배열 progresses와 각 작업의 개발 속도가 적힌 정수 배열 speeds가 주어질 때 각 배포마다 몇 개의 기능이 배포되는지를 return 하도록 solution 함수를 완성하세요.
입출력 예시
| progresses | speeds | return |
| [93, 30, 55] | [1, 30, 5] | [2, 1] |
| [95, 90, 99, 99, 80, 99] | [1, 1, 1, 1, 1, 1] | [1, 3, 2] |
Solution
첫 풀이
import java.util.*;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {
int[] temp = new int[100];
int day = 0;
for (int i=0; i < progresses.length; i++) {
int progress = progresses[i];
int speed = speeds[i];
while(progress < 100) {
progress += speed;
day++;
}
// 작업률이 100을 넘으면 해당 날짜를 인덱스번호로 활용.
// 해당 날짜에 카운트를 올린다.
temp[day]++;
day = 0;
}
// temp에 값이 0이 아닌것만 따로 모아서 출력.
ArrayList<Integer> answer = new ArrayList<>();
int count = 0;
for (int i=0; i<temp.length; i++) {
if (temp[i] != 0) {
answer.add(temp[i]);
}
}
System.out.print(answer);
return null;
}
}
배열을 사용해 풀이하였지만 실패
'뒤에 있는 기능을 앞에 있는 기능이 배포될 떄 함꼐 배포됩니다'
라는 조건이 적용되지 않은 문제라는것을 발견했다.
day를 초기화해야한다고 생각했는데 day를 초기화하면 안되는 구나..
최초 기능개발 이후에는 처음 초기화된 day를 가지고 검사를 진행해야한다.
내가 작성한 코드는 첫번째 검사 한 후
그 다음 검사를 진행할 때 무조건 day가 카운트 되어 각각 검색을 진행했다.
day를 유지시키며 검사할 수 있도록 코드를 수정했다.
while문 안에 검사내용을 넣었다.
최종 풀이
import java.util.*;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {
int[] temp = new int[100];
int day = 0;
for (int i=0; i < progresses.length; i++) {
int pro = progresses[i];
int speed = speeds[i];
// 수정한 부분
while(progresses[i] + (speeds[i] * day) < 100) {
day++;
}
temp[day]++;
}
int count = 0;
for (int n : temp) {
if (n != 0) {
count++;
}
}
int[] answer = new int[count];
count = 0;
for (int n:temp) {
if (n !=0) {
answer[count++] = n;
}
}
return answer;
}
}
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
| 프로그래머스 | JAVA | 프린터 (0) | 2022.10.18 |
|---|---|
| 프로그래머스 | JAVA | 올바른 괄호 (0) | 2022.10.18 |
| 22.10.07 [프로그래머스] 해시 (0) | 2022.10.07 |
| 2022.09.25 프로그래머스 알고리즘 공부 (0) | 2022.09.26 |
| 2022.09.24 알고리즘 공부 (1) | 2022.09.25 |