ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 스택/큐 > 주식가격
    프로그래머스(Programmers) 2018. 9. 26. 21:47
    반응형

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


    1. 문제


    2. 알고리즘

    키워드 - 구현


    3. 코드


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    // 그지 같은 풀이
    #include <string>
    #include <vector>
     
    using namespace std;
     
    vector<int> solution(vector<int> prices) {
        vector<int> answer;
     
        const int size = prices.size();
     
        for (int i = 0; i < size; i++) {
     
            int cand = prices[i];
            int second = 0;
            bool down = false;
            for (int j = i; j < size; j++) {
                if (cand > prices[j]) {
                    down = true;
                    break;
                }
                second++;
            }
     
            if (down)
                answer.push_back(second);
            else if (down == false && second) {
                answer.push_back(second - 1);
            } else {
                answer.push_back(0);
            }
        }
        return answer;
    }
    cs

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    #include <string>
    #include <vector>
     
    using namespace std;
     
    vector<int> solution(vector<int> prices) {
        vector<int> answer;
     
        const int size = prices.size();
     
        for (int i = 0; i < size; i++) {
     
            int cand = prices[i];
            int second = 0;
            for (int j = i; j < size-1; j++) {
                if (cand <= prices[j]) {
                    second++;        
                } else {
                    break;    
                }
            }
            answer.push_back(second);    
        }
        return answer;
    }
    cs

    반응형

    '프로그래머스(Programmers)' 카테고리의 다른 글

    정렬 > H-Index  (0) 2018.09.30
    해시 > 전화번호 목록  (0) 2018.09.30
    스택/큐 > 쇠막대기  (0) 2018.09.26
    스택/큐 > 탑  (0) 2018.09.26
    정렬 > 가장 큰 수  (0) 2018.09.20

    댓글

Designed by Tistory.