기능개발
작업의 진도가 적힌 정수 배열 progresses와 각 작업의 개발 속도가 적힌 정수 배열 speeds가 주어질 때, 각 배포마다 몇개의 기능이 배포되는지를 반환해주는 프로그램을 개발
■ 문제 풀이
이번 문제에서는 Stack 자료구조를 사용하였다.
1. 현재 남은 작업의 진도가 몇일 안에 끝나는 지를 구한다.
2. 초기 max값을 설정한다.
3. max 값보다 큰 값이 들어오면, 기능 개발이 완료되었으므로 배포
4. max 값보다 작은 값이 들어오면, 기능 개발이 완료되지 않았으므로 배포 예정 갯수(cnt 또는 stack) 저장
5. 배포 개수를 저장한 리스트를 배열로 변환하여 결과를 반환
package programmers;
import java.util.*;
public class DevFunction {
static int[] solution(int[] progresses, int[] speeds) {
ArrayList<Integer> list = new ArrayList<Integer>();
final int progressCnt = 100;
int[] workingDays = new int[progresses.length];
for (int i = 0; i < progresses.length; i++) {
int speed = speeds[i];
int quotient = (progressCnt - progresses[i]) / speed;
int remainder = (progressCnt - progresses[i]) % speed;
int calculate = remainder == 0 ? quotient : quotient + 1;
workingDays[i] = calculate;
}
int cnt = 1;
int max = workingDays[0]; //max 값을 초기화
for (int i = 1; i < workingDays.length; i++) {
if (max < workingDays[i]) {
list.add(cnt);
max = workingDays[i];
cnt = 1;
} else {
cnt++;
}
}
list.add(cnt);
return list.stream().mapToInt(i -> i).toArray();
}
static int[] solution_ver2(int[] progresses, int[] speeds) {
Stack<Integer> stack = new Stack<>();
ArrayList<Integer> list = new ArrayList<Integer>();
final int progressCnt = 100;
int max = 0;
for (int i = 0; i < progresses.length; i++) {
int quotient = (progressCnt - progresses[i]) / speeds[i];
int remainder = (progressCnt - progresses[i]) % speeds[i];
int calculate = remainder == 0 ? quotient : quotient + 1;
if (i == 0) {
// max 값 초기화
max = calculate;
}
if (max < calculate) {
max = calculate;
list.add(stack.size());
stack.clear();
}
stack.push(calculate);
}
if (!stack.isEmpty()) {
list.add(stack.size());
}
return list.stream().mapToInt(i -> i).toArray();
}
public static void main(String[] args) {
/**
* test case 1
*/
//int[] progresses = {95, 90, 99, 99, 80, 99};
//int[] speeds = {1, 1, 1, 1, 1, 1};
/**
* test case 2
*/
//int[] progresses = {93, 30, 55};
//int[] speeds = {1, 30, 5};
/**
* test case 3
*/
int[] progresses = {3, 36, 45};
int[] speeds = {1, 60, 22};
int[] answer = solution_ver2(progresses, speeds);
System.out.println(Arrays.toString(answer));
}
}
'CodingTest Practice' 카테고리의 다른 글
[프로그래머스] 전화번호 목록 (0) | 2022.03.27 |
---|---|
[프로그래머스] 완주하지 못한 선수 (0) | 2022.03.24 |
[프로그래머스] 신규 아이디 추천 (0) | 2022.03.17 |
[programmers] 신고 결과 받기 (0) | 2022.01.18 |
[programmers] 로또의 최고 순위와 최저 순위 (0) | 2022.01.11 |