darklight

sublimevimemacs

Java

문제 설명

초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요.

제한사항

입출력 예

Untitled

입출력 예 설명

※ 공지 - 2019년 2월 28일 지문이 리뉴얼되었습니다.

import java.util.*;

class Solution {
    
    public class stock{
        int time;
        int price;
        public stock(int time, int price){
            this.time = time;
            this.price = price;
        }
    }
    
    public int[] solution(int[] prices) {
        int length = prices.length;
        int[] answer = new int[length];
        
        Queue <stock> queue = new LinkedList<>();
        
        for(int i = 0; i < length; i++){
            queue.offer(new stock(i, prices[i]));
        }
        
        while(!queue.isEmpty()){
            stock s = queue.poll();
            int count = 0;
            if(s.time != (length - 1)){
                for(int i = s.time + 1; i < length; i++){
                    if(s.price <= prices[i]){
                        count++;
                    }else{
                        count++;
                        break;
                    }
                }         
            }
            answer[s.time] = count;
        }
        
        return answer;
    }
}

풀이