LeetCode 739: daily temperature Daily Temperatures

topic:

According to the daily 气温list, please re-generate a list, enter the corresponding position is how long you need to wait until the temperature rises more than the number of days of that date. After all, if not increased, please use this location 0instead.

For example, given a list of temperatures = [73, 74, 75, 71, 69, 72, 76, 73]your output should be [1, 1, 4, 2, 1, 1, 0, 0].

Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.

For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].

Tip:气温 In list length is [1, 30000]. Value for each temperature are degrees Fahrenheit, are [30, 100]integers in the range.

Note: The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].

Problem-solving ideas:

Most likely to think and understand is brute force, two pointers, a pointer to the current temperature, the second pointer to traverse backwards to find the nearest one more than the current temperature of the high temperatures recorded two pointers index difference can be. It can be said efficiency is very low.

Another method is by means of the stack in reverse order traversal of the original array, the higher temperature into the index , is also very easy to understand, the time complexity is O (n). Which logic implemented:

原数组:[73, 74, 75, 71, 69, 72, 76, 73],指针 i 从末尾向前遍历,返回数组res=[0,0,0,0,0,0,0]

第一次遍历: i = 7, T[i] = 73, stack = []
        栈为空,res[7] = 0 ,73所在索引7入栈。stack = [7]

第二次遍历: i = 6, T[i] = 76, stack = [7]
        栈顶对应索引7的温度T[7]=76,76>73,索引7出栈,此时栈为空,res[6] = 0。索引6入栈,stack = [6]

第三次遍历: i = 5, T[i] = 72, stack = [6]
        栈顶对应索引6的温度T[6]=76,72<76,满足要求,当前索引5入栈。res[5] = 栈顶索引6 - 当前索引5 = 1, stack = [6,5]

第四次遍历: i = 4, T[i] = 69, stack = [6,5]
        栈顶对应索引5的温度T[5]=72,69<72,满足要求,当前索引4入栈。res[4] = 栈顶索引5-当前索引4=1, stack = [6,5,4]

第五次遍历: i = 3, T[i] = 71, stack = [6,5,4]
        栈顶对应索引的温度T[4]=69,71>69,栈顶元素出栈。stack = [6,5]
        栈顶对应索引的温度T[5]=72,满足要求,当前索引3入栈。res[3] = 栈顶索引5-当前索引3=2, stack = [6,5,3]

第六次遍历: i = 2, T[i] = 75, stack = [6,5,3]
        栈顶对应索引的温度T[3]=71,75>71,栈顶元素出栈。stack = [6,5]
        栈顶对应索引的温度T[5]=72,75>72,栈顶元素出栈。stack = [6]
        栈顶对应索引的温度T[6]=76,75<76,满足要求,当前索引2入栈。res[2] = 栈顶索引6-当前索引2=4, stack = [6,2]

第七次遍历: i = 1, T[i] = 74, stack = [6,2]
        栈顶对应的温度T[2]=75,满足要求,当前索引1入栈。res[1] = 2-1=1, stack = [6,2,1]

第八次遍历: i = 0, T[i] = 73, stack = [6,2,1]
        栈顶对应的温度T[1]=74,满足要求,当前索引0入栈。res[0] = 1-0=1, stack = [6,2,1,0]

遍历结束: res = [1,1,4,2,1,1,0,0]

In this method, the temperature corresponding to the index into the stack value is always ascending order, when the stack is empty, all temperatures demonstrated the temperature from the current temperature of the biggest rearwardly.

Java:

class Solution {
    public int[] dailyTemperatures(int[] T) {
        int len=T.length;
        int[] res = new int[len];
        Stack<Integer> stack = new Stack<>();//初始化栈
        for (int i = len - 1; i >= 0; i--) {
            while (!stack.isEmpty() && T[stack.peek()] <= T[i]) {
                stack.pop();
            }
            if (stack.isEmpty()) res[i] = 0;
            else res[i] = stack.peek() - i;
            stack.push(i);
        }
        return res;
    }
}

In fact on this question in terms of this it is not a good way, because tips have been clearly defined range of data:

In list length is [1, 30000]. Value for each temperature are degrees Fahrenheit, are [30, 100]integers in the range.

Up to 30,000 data volume, data is very small, stack the stack, access to the top element to determine whether an empty stack, these functions are repeatedly invoked in rare cases the amount of data relative to take up a lot of running time, running its final evaluation The total time of 109 ms. So how to optimize?

Since all data values ​​are a range between 30 and 100, in ascending order means the temperature does not exceed the maximum size of the stack 71 (from 30 to 100 since only 71 elements). You can not stack the need to repeatedly call a function of the data structure. Direct sequence of length 71 is stored in the index array to define a pointer index by one instead of the stack, the stack instead of assigning the index is incremented, if the index instead of the overflow determines whether the stack is empty, no virtual function call.

Optimized:

Total run time of 4ms, reduced 105 milliseconds

class Solution {
    public int[] dailyTemperatures(int[] T) {
        int len = T.length;
        int[] res = new int[len], stack = new int[71];
        int index = -1;
        for (int i = len - 1; i >= 0; i--) {
            while (index >= 0 && T[stack[index]] <= T[i]) {
                index--;
            }
            if(index >= 0) res[i] = stack[index] - i;
            stack[++index] = i;
        }
        return res;
    }
}

Python:

python and no queue, stack data structure such as an array can be FIFO, LIFO and other operations.

class Solution:
    def dailyTemperatures(self, T: List[int]) -> List[int]:
        tLen = len(T)
        stack = []
        res = [0] * tLen
        for i in range(tLen - 1, -1, -1):
            while stack and T[i] >= T[stack[-1]]:
                stack.pop()
            if stack: res[i] = stack[-1] - i
            stack.append(i)
        return res

.. Welcome attention to the micro-channel public learning together all the numbers: Love Bug write
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/zhangzhe532/p/11330783.html