You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: daily_temperatures.cpp
+41Lines changed: 41 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,43 @@
1
+
classSolution {
2
+
public:
3
+
vector<int> dailyTemperatures(vector<int>& T) {
4
+
5
+
6
+
/*
7
+
Next greater to right and difference.
8
+
Start scanning from right end of array.
9
+
*/
10
+
11
+
12
+
stack<pair<int, int>> S; // S.top()[0] ==> temperature, S.top()[1] ==> next warmer(>) temperature
13
+
14
+
int n = T.size();
15
+
vector<int> result(n, 0);
16
+
17
+
for(int i=n-1; i>=0; i--) {
18
+
19
+
//
20
+
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)
21
+
S.pop();
22
+
}
23
+
24
+
// once we found it, push the difference between current element to its next warmer temperature in result array.
0 commit comments