https://leetcode.com/problems/daily-temperatures/
아래와 같이 첫번째 인자로 초기화할 vector 길이 int를 넣고, 두번째 인자로 초기화할 type에 맞는 data 를 넣는다.
vector<int> answer('초기화할 vector 길이 int', '초기화할 data (int)');
아래와 같이 back(), front() function들을 이용한다.
deque<int> q;
q.back(); // 가장 마지막 원소 접근
q.front(); // 가장 처음 원소 접근
Solution의 Approach #2: Stack의 알고리즘을 이용해서 C++로 문제를 풀었다.
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& T) {
vector<int> answer(T.size(), 0);
deque<int> q;
for(int i=T.size()-1; i>-1; i--) {
while(q.size() != 0 && T[q.back()] <= T[i]) {
q.pop_back();
}
answer[i] = q.size() == 0 ? 0 : q.back() - i;
q.push_back(i);
}
return answer;
}
};