-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy path12.cpp
More file actions
48 lines (38 loc) · 1.19 KB
/
Copy path12.cpp
File metadata and controls
48 lines (38 loc) · 1.19 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <string>
#include <vector>
#include <stack>
using namespace std;
vector<int> solution(vector<int> prices) {
//❶ 가격이 떨어지지 않은 기간을 저장한 벡터
vector<int> answer(prices.size());
// 스택에는 prices의 인덱스가 들어감, 이전 가격과 현재 가격을 비교하기 위한 용도로 사용됨
stack<int> s;
int priceNum = prices.size();
for(int i=0;i<priceNum;i++){
while(!s.empty()&&prices[s.top()]>prices[i]){
//❷ 가격이 떨어졌으므로 이전 가격의 기간 계산
answer[s.top()] = i-s.top();
s.pop();
}
s.push(i);
}
//❸ 스택에 남아있는 가격들은 가격이 떨어지지 않은 경우
while(!s.empty()){
answer[s.top()] = priceNum-s.top()-1;
s.pop();
}
return answer;
}
//아래 코드는 테스트 코드 입니다.
#include <iterator>
#include <iostream>
void print(vector<int> vec)
{
copy(vec.begin(), vec.end(), std::ostream_iterator<int>(cout, " "));
cout << endl;
}
int main()
{
print(solution({1, 2, 3, 2, 3})); // 4 3 1 1 0
return 0;
}