[LeetCode]일일 온도

Inhwan98·2023년 3월 30일
0

PTU STUDY_leetcode

목록 보기
21/24

문제

매일의 화씨 온도(F) 리스트 T를 입력받아서, 더 따뜻한 날씨를 위해서는 며칠을 더 기다려야 하는지를 출력하라.

예제

  • Example 1:
Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
  • Example 2:
Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]
  • Example 3:
Input: temperatures = [30,60,90]
Output: [1,1,0]

Constraints:

  • 1 <= temperatures.length <= 105
  • 30 <= temperatures[i] <= 100

코드

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& v) {
    vector<int> answer(v.size(), 0);
	stack<int> st;
	for (int i = 0; i < v.size(); i++)
	{
		while (!st.empty() && v[st.top()] < v[i])
		{
			answer[st.top()] = i - st.top();
			st.pop();
		}
		st.push(i);
	}

        return answer;
    }
};

결과

Runtime 202 ms / Memory 103.2 MB
https://leetcode.com/problems/daily-temperatures/submissions/925089117/


https://leetcode.com/problems/daily-temperatures/

profile
코딩마스터

0개의 댓글