https://leetcode.com/problems/daily-temperatures/
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
std::list<pair<int, int>>::iterator it;
vector<int> ans(temperatures.size(), 0);
list<pair<int/*value*/, int/*index*/>> l;
for (int i = 0; i < temperatures.size(); i++) {
pair<int,int> now = make_pair(temperatures[i], i);
for (it = l.begin(); it != l.end(); ) {
if (it->first < temperatures[i]) {
ans[it->second] = i - it->second;
it = l.erase(it);
} else {
break;
}
}
l.push_front(now);
}
return ans;
}
};
留言
張貼留言