10.Daily Temperatures
Solution: (Finding the next greater element)
class Solution
{
public:
vector<int> dailyTemperatures(vector<int> &t)
{
stack<int> s;
vector<int> v(t.size(), 0);
for (int i = 0; i < t.size(); i++)
{
while (!s.empty() && t[i] > t[s.top()])
{
v[s.top()] = i - s.top();
s.pop();
}
s.push(i);
}
return v;
}
};Last updated