Open zwkcoding opened 5 years ago
Solution:
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& T) {
stack<int> sta;
vector<int> ans;
ans.resize(T.size(), 0);
for(int i=0;i<T.size();i++){
while(sta.empty() != true) {
if(T[sta.top()] < T[i]) {
ans[sta.top()] = i - sta.top();
sta.pop();
} else {
break;
}
}
sta.push(i);
}
return ans;
}
};
Question: Problem see here : https://leetcode.com/explore/learn/card/queue-stack/230/usage-stack/1363/
leave the hole: use the Data Structure
Stack
,initial ideas: