문제

  • 링크
    title: "코딩테스트 연습 - 주식가격"
    image: "https://image.freepik.com/free-vector/landing-page-template-for-a-website_23-2147782747.jpg"
    description: "초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요."
    url: "https://school.programmers.co.kr/learn/courses/30/lessons/42584?language=java"
    

문제 풀이

문제 접근

  • Stack

    간단한 수도 코드 정리

    Type/Paste Your Code
    

풀이 코드

class Solution {
    public int[] solution(int[] prices) {
        int[] answer = new int[prices.length];
        
        for (int i = 0; i < prices.length; i++) {
            int time = 0;
            for (int j = i + 1; j < prices.length; j++) {
                time++;
                if (prices[i] > prices[j]) {
                    break; // 가격이 떨어지면 멈춤
                }
            }
            answer[i] = time;
        }
        
        return answer;
    }
}