[프로그래머스 - Java] 주식가격

민채·2021년 7월 8일
0

문제

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

설명

스택/큐를 이용하지 않고 이중 for문을 이용해 문제를 해결했다.

소스코드

public class StockPrice {

    public static void main(String[] args) {
        int[] prices = {1, 2, 3, 2, 3};
        int[] result = solution(prices);
		
        for (int r : result) {
            System.out.print(r + " ");
        }
    }

    public static int[] solution(int[] prices) {
        int[] answer =  new int[prices.length];
		
        for (int i = 0; i < prices.length; i++) {
            for (int j = i + 1; j < prices.length; j++) {
                answer[i]++;
				
                // 가격이 떨어지는 경우 두 번째 for문 종료
                if (prices[i] > prices[j]) {
                    break;
                }
            }
        }
		
        return answer;
    }
}

GITHUB

https://github.com/MinchaeKwon/Programmers/blob/master/Level2/src/StockPrice.java

profile
코딩계의 떠오르는 태양☀️

0개의 댓글

관련 채용 정보