-
Notifications
You must be signed in to change notification settings - Fork 6
/
daily_temperatures.cpp
57 lines (42 loc) · 1.58 KB
/
daily_temperatures.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& T) {
/*
Next greater to right and difference.
Start scanning from right end of array.
*/
stack<pair<int, int>> S; // S.top()[0] ==> temperature, S.top()[1] ==> next warmer(>) temperature
int n = T.size();
vector<int> result(n, 0);
for(int i=n-1; i>=0; i--) {
//
while(!S.empty() and S.top().first <= T[i]) { //if current element is greater than element at top of stack..pop the element until we find next greater element in stack(next warmer temperature)
S.pop();
}
// once we found it, push the difference between current element to its next warmer temperature in result array.
if(!S.empty() and T[i] < S.top().first) {
result[i] = S.top().second - i;
}
S.push({T[i], i});
}
return result;
}
};
/*
More optimized in terms of time and space.
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& T) {
vector<int> next_greatest(T.size(), 0);
stack<int> temperatures;
for(int i = T.size()-1; i>=0; i--){
while(!temperatures.empty() && T[i] >= T[temperatures.top()]){
temperatures.pop();
}
next_greatest[i] = temperatures.empty() ? 0 : temperatures.top() - i;
temperatures.push(i);
}
return next_greatest;
}
};
*/