【LeetCode】739, daily temperature. Difficulty Level: Moderate. A variety of solutions are worth studying.

0. Topic

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 this place.

Example 1:

输入: temperatures = [73,74,75,71,69,72,76,73]
输出: [1,1,4,2,1,1,0,0]

Example 2:

输入: temperatures = [30,40,50,60]
输出: [1,1,1,0]

Example 3:

输入: temperatures = [30,60,90]
输出: [1,1,0]

1. Exposure search: O(n 2 ) over time limit

The simplest method is brute-force search, whose time complexity is O(n 2 ), which will exceed the time limit in individual execution cases. code show as below:

class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        length=len(temperatures)
        ans=[0]*length
        for i in range(length-1):
            if temperatures[i+1]>temperatures[i]:
                ans[i]=1
            else:
                for j in range(i+1,length):
                    if temperatures[j]>temperatures[i]:
                        ans[i]=j-i
                        break
        return ans

2. Exquisite solution: reverse search, single-layer loop O(n)

The problem with forward search is that it requires a second cycle, resulting in a high time complexity.

If you consider the reverse search, you can find the next higher temperature weather in O(1) time complexity. This method is wonderful, the code is as follows:

class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        length = len(temperatures)
        ans= [0] * length
        # 必须逆序
        for i in range(length - 2, -1, -1):
            pos=i+1
            # 只要前一天温度比现在的高
            while temperatures[pos] <= temperatures[i]:
                # 找到下一个比 temperatures[pos] 气温高的天气,与temperatures[i]进行比较
                if ans[pos] > 0:
                    pos += ans[pos]
                # 找不到下一个比 temperatures[pos] 气温高的天气,因为也没有比 temperatures[i] 气温高的天气,所以 ans[i] = 0
                else:
                    pos=i
                    break
            # 找到比i天的温度高的位置,计算结果存储即可
            ans[i] = pos - i
        return ans

3. Official solution: monotonic stack, single-layer loop O(n)

Monotonic stack LeetCode official video explanation: daily temperature

Idea: Maintain a monotonic stack that stores subscripts, and the temperature in the temperature list corresponding to the subscripts from the bottom of the stack to the top of the stack decreases in turn. If a subscript is in the monotonic stack, it means that the next higher temperature subscript has not been found yet.

code:

class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        stack=[]
        length=len(temperatures)
        ans=[0]*length

        for i in range(length):
            if not stack:
                stack.append([i,temperatures[i]])
            else:
                while stack and temperatures[i]>stack[-1][1]:
                    ans[stack[-1][0]]=i-stack[-1][0]
                    stack.pop()
                stack.append([i,temperatures[i]])
        return ans

Guess you like

Origin blog.csdn.net/qq_43799400/article/details/131713370