티스토리 뷰

1. 문제

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

2. 설명

배열을 비교하면서 1씩 증가해주고, 가격이 떨어지면 for문을 나가도록 함.

스택, 큐 문젠데 배열로 풀었다. 스택으로 풀면 효율성이 통과안된다던데 음?

 

3. 코드

class Solution {
    public 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] += 1;
                if(prices[i] > prices[j])
                    break;
            }
        }
        return answer;
    }
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday