[LeetCode daily question] - 739. Daily temperature

One [topic category]

  • the stack

Two [question difficulty]

  • medium

Three [topic number]

  • 739. Daily temperature

Four [title description]

  • Given an integer array temperatures, representing the temperature of each day, return an array answer, where answer[i] means that for the i-th day, the next higher temperature will appear in a few days. If the temperature doesn't rise after that, substitute 0 in that place.

Five [topic examples]

  • 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]

Six [topic prompt]

  • 1 < = t e m p e r a t u r e s . l e n g t h < = 1 0 5 1 <= temperatures.length <= 10^5 1<=temperatures.length<=105
  • 30 < = t e m p e r a t u r e s [ i ] < = 100 30 <= temperatures[i] <= 100 30<=temperatures[i]<=100

Seven [problem-solving ideas]

  • Use a stack to save the subscript of each element in the temperature array. This stack is a monotonically decreasing stack. We need to maintain this stack
  • When traversing to a new element, first push its subscript onto the stack
  • Then, use a while loop to compare the temperature corresponding to the top element of the stack with the current temperature. If the current temperature is higher than the temperature at the top of the stack, it means that the current temperature is the first temperature on the right of the top of the stack that is higher than it. You can calculate the number of days between them and save the result in the result array
  • Then, pop the top element of the stack and continue to compare the next top element until the stack is empty or the current temperature is lower than the top temperature
  • Finally, push the subscript of the current element onto the stack
  • After the traversal, what is saved in the result array is the number of days between the first higher temperature on the right of each temperature and it. If there is no temperature higher than it to the right of a temperature, the corresponding value in the result array is 0

Eight 【Time Frequency】

  • Time complexity: O ( n ) O(n)O(n) n n n is the length of the incoming array
  • Space Complexity: O ( n ) O(n)O(n) n n n is the length of the incoming array

Nine [code implementation]

  1. Java language version
class Solution {
    
    
    public int[] dailyTemperatures(int[] temperatures) {
    
    
        int len = temperatures.length;
        Deque<Integer> stack = new LinkedList<Integer>();
        int[] res = new int[len];
        for(int i = 0;i<len;i++){
    
    
            int temp = temperatures[i];
            while(!stack.isEmpty() && temp > temperatures[stack.peek()]){
    
    
                int preIndex = stack.pop();
                res[preIndex] = i - preIndex;
            }
            stack.push(i);
        }
        return res;
    }
}
  1. C language version
int* dailyTemperatures(int* temperatures, int temperaturesSize, int* returnSize)
{
    
    
    int len = temperaturesSize;
    int* stack = (int*)calloc(len,sizeof(int));
    int top = -1;
    int* res = (int*)calloc(len,sizeof(int));
    for(int i = 0;i < len;i++)
    {
    
    
        int temp = temperatures[i];
        while(top != -1 && temp > temperatures[stack[top]])
        {
    
    
            int preIndex = stack[top--];
            res[preIndex] = i - preIndex;
        }
        stack[++top] = i;
    }
    *returnSize = temperaturesSize;
    free(stack);
    return res;
}
  1. Python language version
class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        length = len(temperatures)
        stack = []
        res = [0] * length
        for i in range(0,length):
            temp = temperatures[i]
            while(len(stack) != 0 and temp > temperatures[stack[-1]]):
                preIndex = stack.pop()
                res[preIndex] = i - preIndex
            stack.append(i)
        return res
  1. C++ language version
class Solution {
    
    
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
    
    
        int len = temperatures.size();
        stack<int> st;
        vector<int> res(len);
        for(int i = 0;i<len;i++){
    
    
            int temp = temperatures[i];
            while(!st.empty() && temp > temperatures[st.top()]){
    
    
                int preIndex = st.top();
                st.pop();
                res[preIndex] = i - preIndex;
            }
            st.push(i);
        }
        return res;
    }
};

Ten【Submission Results】

  1. Java language version
    insert image description here

  2. C language version
    insert image description here

  3. Python language version
    insert image description here

  4. C++ language version
    insert image description here

Guess you like

Origin blog.csdn.net/IronmanJay/article/details/131916052