https://school.programmers.co.kr/learn/courses/30/lessons/42586
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제
풀이 1 - 스택 (큐가 적합한 방법)
아무 생각 없이 스택으로 했다가 힘든 길을 걸었던 ..
import java.util.*;
class Solution {
public List<Integer> solution(int[] progresses, int[] speeds) {
List<Integer> duration = new ArrayList<>();
for (int i = 0; i < progresses.length; i++) {
double d = (double)(100-progresses[i])/speeds[i];
duration.add((int)Math.ceil(d));
}
Stack<Integer> stack = new Stack<>();
List<Integer> answer = new ArrayList<>();
int a = duration.get(0) ;
int count;
for (int i = 0; i < duration.size(); i++) {
if(stack.isEmpty()) {
a = duration.get(i);
stack.push(a);
}
else if(a > duration.get(i)) {
stack.push(duration.get(i));
}
else {
count = 0;
while(!stack.isEmpty()){
int b = stack.pop();
count++;
}
answer.add(count);
stack.push(duration.get(i));
}
}
if(!stack.isEmpty()){
count = 0;
while(!stack.isEmpty()){
int b = stack.pop();
count++;
}
answer.add(count);
}
return answer;
}
}
어찌저찌 힘든 길을 통해 구현은 해서 주어진 테스트 케이스는 맞는 데 제출은 하면 틀린다.
이유는 아직 모름 ..
큐가 맞으니 큐로 다시 풀어봤다.
풀이 2 - 큐
package programmers;
import java.util.*;
public class develop {
public static void main(String[] args) {
int[] progresses = {93, 30, 55};
int[] speeds = {1, 30, 5};
List<Integer> duration = new ArrayList<>();
double a;
for (int i = 0; i < progresses.length; i++) {
a = Math.ceil((double)( 100 - progresses[i]) / speeds[i]);
duration.add((int) a);
System.out.print(duration.get(i));
}
List<Integer> answer = new ArrayList<>();
Queue<Integer> queue = new LinkedList<>();
int count;
for(int i : duration){
System.out.println(i + "입장! ");
if(queue.isEmpty()) {
queue.add(i);
System.out.println(i + " 최초 들어감");
}
else if(i < queue.peek()) {
queue.offer(i);
System.out.println(i + " 들어감");
}
else {
count = 0;
while (!queue.isEmpty()){
System.out.println(queue.peek() + "나옴");
queue.poll();
count++;
}
answer.add(count);
queue.offer(i);
}
}
count = 0;
while (!queue.isEmpty()){
System.out.println(queue.peek() + "나옴");
queue.poll();
count++;
}
answer.add(count);
System.out.println(answer);
}
}
같은 로직으로 큐로만 바꿔서 풀었더니 위처럼 테스트케이스는 맞는데, 정답 제출을 하니 몇개가 예외가 생겨 오답 처리가 된다. 로직의 문제이겠다.
해결
queue에 들어간게 가장 앞에 있는 것과 같거나 작을 때도 큐에 들어가는 데 그걸 안 따져줌 ... (....)...♨
if(queue.isEmpty() || i <= queue.peek()) {
queue.offer(i);
System.out.println(i + " 들어감");
}
최종 코드
queue에 넣는 if문을 하나로 합쳐서 간결하게 했다.
import java.util.*;
class Solution {
public List<Integer> solution(int[] progresses, int[] speeds) {
List<Integer> duration = new ArrayList<>();
double a;
for (int i = 0; i < progresses.length; i++) {
a = Math.ceil((double)( 100 - progresses[i]) / speeds[i]);
duration.add((int) a);
}
List<Integer> answer = new ArrayList<>();
Queue<Integer> queue = new LinkedList<>();
int count;
for(int i : duration){
if(queue.isEmpty() || i <= queue.peek()) {
queue.offer(i);
}
else {
count = 0;
while (!queue.isEmpty()){
queue.poll();
count++;
}
answer.add(count);
queue.offer(i);
}
}
if(!queue.isEmpty()) {
count = 0;
while (!queue.isEmpty()) {
queue.poll();
count++;
}
answer.add(count);
}
return answer;
}
}
'‡ CODING TEST STUDY ‡ > º 프로그래머스' 카테고리의 다른 글
[프로그래머스 Java Lv.1] 추억 점수 (0) | 2023.12.05 |
---|---|
[프로그래머스 Lv.2 Java] 스택/큐 | 같은 숫자는 싫어 (0) | 2023.11.29 |
[프로그래머스 Lv.1 Java] 달리기 경주 (0) | 2023.11.20 |
[프로그래머스 Lv.0 Java] 대소문자 바꿔서 출력하기 (0) | 2023.11.20 |
[SQL Lv.1] 자동차 대여 기록에서 장기/단기 대여 구분하기 (0) | 2023.11.07 |