[프로그래머스] 스택/큐 - 주식가격

김준영·2023년 3월 20일
1

코딩테스트

목록 보기
16/22

풀이

import java.util.*;
import java.util.stream.Collectors;
class Solution {
    public int[] solution(int[] prices) {
        List<Integer> answer = new ArrayList<>();
        Queue<Integer> queue = new LinkedList<>(Arrays.stream(prices).boxed().collect(Collectors.toList()));
        while (!queue.isEmpty()){
            int first = queue.poll();
            int time = 0;
            for (int a : queue){
                time++;
                if(first > a){
                    break;
                }
            }
            answer.add(time);
        }
        return answer.stream().mapToInt(i -> i).toArray();
    }
}

뽑은 주식 가격을 반복문을 통해 비교하면서 시간은 ++해주다가

if → 떨어지는 시점이 생기는 순간 break을 통해 반복문 탈출.

배열 변환 후 반환.

profile
ㅎㅎ

0개의 댓글