취미로 음악을 하는 개발자

[Java] 프로그래머스 스택/큐, 주식가격 본문

공대인/Programmers

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

영월특별시 2019. 12. 3. 17:27
728x90

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
 
// 주식 가격
public class StockPrice {
    public static int[] solution(int[] prices) {
        Queue<Integer> q = new LinkedList<>();    // prices
        Iterator<Integer> it;        // 비교 원소들
        int[] answer = new int[prices.length];    // 가격이 떨어지지 않은 기간
        for (int i : prices)
            q.offer(i);
        
        int price, count;    // price: 현재 주식 가격, count: 얼마나 떨어지지 않았는가
        for (int i = 0; i < prices.length; i++) {
            price = q.poll();
            it = q.iterator();
            count = 0;
            while (it.hasNext()) {
                count++;
                if (price > it.next()) {    // 현재 주식 가격이 해당 시간에 떨어진다면
                    answer[i] = count;
                    break;
                }                
            }
            
            if (i == prices.length-1)        // 마지막 시점은 예측 불가
                answer[i] = 0;
            else if (answer[i] == 0)        // 끝까지 가격이 떨어지지 않았을 때
                answer[i] = count;
        }
        return answer;
    }
    
    public static void main(String[] args) {
        System.out.println(solution(new int[] {1,2,3,2,3}));
    }
}
cs


https://programmers.co.kr/learn/courses/30/lessons/42584



Comments