-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdditional4.cpp
More file actions
37 lines (31 loc) · 890 Bytes
/
Copy pathAdditional4.cpp
File metadata and controls
37 lines (31 loc) · 890 Bytes
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
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main() {
int n;
cout << "Enter number of days: ";
cin >> n;
vector<int> temp(n);
cout << "Enter temperatures:\n";
for (int i = 0; i < n; i++) {
cin >> temp[i];
}
vector<int> answer(n, 0); // initially all 0
stack<int> s; // will store indices of days
for (int i = 0; i < n; i++) {
// while current temp is warmer than the day at top of stack
while (!s.empty() && temp[i] > temp[s.top()]) {
int prev = s.top();
s.pop();
answer[prev] = i - prev; // days waited
}
s.push(i); // push current day
}
cout << "Answer array (days to wait):\n";
for (int i = 0; i < n; i++) {
cout << answer[i] << " ";
}
cout << endl;
return 0;
}