-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdailyTemp.cpp
41 lines (39 loc) · 917 Bytes
/
dailyTemp.cpp
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
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
vector<int> dailyTemperatures(vector<int>& temperatures) {
stack<int> s1;
vector <int> ans(temperatures.size());
for (int i = temperatures.size() - 1; i >= 0; i--) {
while (!s1.empty() && temperatures[i] >= temperatures[s1.top()]) {
s1.pop();
}
if (s1.empty()) {
ans[i] = 0;
}
else {
ans[i] = (s1.top() - i);
}
s1.push(i);
}
return ans;
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector <int> temperatures(n);
for (int i = 0; i < n; i++) {
cin >> temperatures[i];
}
vector <int> ans = dailyTemperatures(temperatures);
for (auto i :ans) {
cout << i << "\t";
}
cout << endl;
}
return 0;
}