주식가격

김태선·2021년 10월 2일

프로그래머스 문제

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

제한사항
prices의 각 가격은 1 이상 10,000 이하인 자연수입니다.
prices의 길이는 2 이상 100,000 이하입니다.
입출력 예
prices return
[1, 2, 3, 2, 3][4, 3, 1, 1, 0]
입출력 예 설명
1초 시점의 ₩1은 끝까지 가격이 떨어지지 않았습니다.
2초 시점의 ₩2은 끝까지 가격이 떨어지지 않았습니다.
3초 시점의 ₩3은 1초뒤에 가격이 떨어집니다. 따라서 1초간 가격이 떨어지지 않은 것으로 봅니다.
4초 시점의 ₩2은 1초간 가격이 떨어지지 않았습니다.
5초 시점의 ₩3은 0초간 가격이 떨어지지 않았습니다.
※ 공지 - 2019년 2월 28일 지문이 리뉴얼되었습니다.

내 답안

class Solution {
    public int[] solution(int[] prices) {
    //답안 배열 생성
        int[] answer = new int[prices.length];
        int length = 0;
        
        for(int i = 0 ; i<prices.length; i++){
            length = 0;
            for(int y = i; y<prices.length; y++){
                if(prices[i]>prices[y]){
                 answer[i] = length;
                    break;
                }
                length++; 
            }
            if(answer[i] == 0){
                answer[i]=length-1;
            }
        }
        
        return answer;
    }
}

채점 결과
정확성: 66.7
효율성: 33.3
합계: 100.0 / 100.0

가장 많이 좋아요를 받은 코딩

class Solution {
    public int[] solution(int[] prices) {
        int len = prices.length;
        int[] answer = new int[len];
        int i, j;
        for (i = 0; i < len; i++) {
            for (j = i + 1; j < len; j++) {
                answer[i]++;
                if (prices[i] > prices[j])
                    break;
            }
        }
        return answer;
    }
}

ㅎ..

i, y를 둘다 같은 값으로 시작했던걸 0값에 대한 걸로 했는데 그부분에 대해서 생각이 짧았던 것 같다.

class Solution {
    public int[] solution(int[] prices) {
        int[] answer = new int[prices.length];
        
        for(int i = 0 ; i<prices.length; i++){
            for(int y = i+1; y<prices.length; y++){
              answer[i]++;
                if(prices[i]>prices[y]){
                    break;
                }
            }
        }
        return answer;
    }
}

수정후 코드

난이도 자체는 낮았는데.. 주먹구구식으로 풀었던 것 같다.

profile
개발하자!

0개의 댓글