-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaily_temp.cpp
44 lines (35 loc) · 1.03 KB
/
daily_temp.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
#include<iostream>
int main(){
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temp) {
stack <int>s;
//creating vectore of temp size and initializing values for each index to zero
vector<int>diff(temp.size(),0);
//push the index of first temp
s.push(0);
for(int i=1 ;i < temp.size(); i++)
{
//check the stack as it isn't empty
while(!s.empty())
{
bool flag = false;
//check if temp[i] warmer than the temp[i on top of the stack]
// here temp[s.top()]=temp[0]=73 73<74
if(temp[s.top()]<temp[i])
{
diff[s.top()] = i-(s.top());
s.pop();
flag = true;
}
if(!flag || s.size()==0)
{
s.push(i);
break;
}
}
}
return diff;
}
}
);