프로그래머스(Programmers)

스택/큐 > 주식가격

cepiloth 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

반응형