Day 58 單調(diào)棧:739. 每日溫度, 496. 下一個更大元素 I

739. 每日溫度

  • 思路
    • example
    • 一維數(shù)組,要尋找任一個元素的右邊或者左邊第一個比自己大或者小的元素的位置
    • 單調(diào)棧

[73]
[74], 73: 1
[75], 74: 1
[75, 71, 69]
[75, 72], 69:1, 71: 2
[76], 72: 1, 75: 4
[76, 73], end, 73: 0, 76: 0

  • 單調(diào)下降順序
  • 復(fù)雜度. 時間:O(n), 空間: O(n)
class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        n = len(temperatures)
        res = [0 for _ in range(n)]
        stack = [0]
        for i in range(1, n):
            if temperatures[i] <= temperatures[stack[-1]]:
                stack.append(i) 
            else:
                while stack and temperatures[i] > temperatures[stack[-1]]:
                    idx = stack.pop()  
                    res[idx] = i - idx  
                stack.append(i)  
        return res   
class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        n = len(temperatures)
        res = [0 for _ in range(n)] 
        stack = [] 
        for i in range(n):
            if not stack or temperatures[stack[-1]] >= temperatures[i]:
                stack.append(i) 
            else:
                while stack and temperatures[stack[-1]] < temperatures[i]:
                    idx = stack.pop()
                    res[idx] = i - idx  
                stack.append(i) 
        return res  
class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        n = len(temperatures) 
        stack = [0] 
        res = [0 for _ in range(n)]
        for i in range(1, n):
            while stack and temperatures[i] > temperatures[stack[-1]]:
                idx = stack.pop()
                res[idx] = i - idx  
            stack.append(i)  
        return res   

496. 下一個更大元素 I

  • 思路
    • example
    • 沒有重復(fù)元素 的數(shù)組
    • 兩個數(shù)組, nums1是nums2子集。在nums2中找到nums1相同元素,然后在nums2中找更大的元素。

nums2: 對每一個元素,找右邊第一個更大元素
hash上面結(jié)果
遍歷 nums1

  • 復(fù)雜度. 時間:O(m+n), 空間: O(n)
class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        m, n = len(nums1), len(nums2)
        res = [-1] * m
        stack = [0]
        hashmap = collections.defaultdict(int)
        for j in range(m):
            hashmap[nums1[j]] = j
        for i in range(1, n):
            while stack and nums2[i] > nums2[stack[-1]]:
                index = stack.pop()
                if nums2[index] in hashmap: # nums1 is only a subset of nums2
                    res[hashmap[nums2[index]]] = nums2[i]
            stack.append(i)
        return res  
class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        m, n = len(nums1), len(nums2)
        table = collections.defaultdict(int)   
        for i in range(m):
            table[nums1[i]] = i   
        stack = [0]
        res = [-1 for _ in range(m)]
        for j in range(1, n):
            while stack and nums2[j] > nums2[stack[-1]]:
                idx = stack.pop()  
                if nums2[idx] in table:
                    res[table[nums2[idx]]] = nums2[j]  
            stack.append(j)   
        return res   
class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        m, n = len(nums1), len(nums2) 
        table = collections.defaultdict(int)  
        for i in range(m):
            table[nums1[i]] = i
        res = [-1 for _ in range(m)] 
        stack = []   
        for j in range(n):
            if not stack or nums2[stack[-1]] >= nums2[j]:
                stack.append(j)  
            else:
                while stack and nums2[stack[-1]] < nums2[j]:
                    idx = stack.pop()
                    if nums2[idx] in table:  # !!!
                        res[table[nums2[idx]]] = nums2[j]
                stack.append(j)    
        return res   
class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        m, n = len(nums1), len(nums2) 
        res = [-1 for _ in range(m)]
        nxt = collections.defaultdict(int)
        stack = [0] 
        for j in range(1, n):
            while stack and nums2[j] > nums2[stack[-1]]:
                idx = stack.pop() 
                nxt[nums2[idx]] = nums2[j]  
            stack.append(j)  
        for i in range(m):
            res[i] = nxt[nums1[i]] if nxt[nums1[i]] != 0 else -1
        return res 
class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        m, n = len(nums1), len(nums2) 
        pos = collections.defaultdict(int)
        for i in range(n):
            pos[nums2[i]] = i 
        res = [0 for _ in range(m)] 
        nxt = [-1 for _ in range(n)]
        stack = [0]
        for i in range(1, n):
            while stack and nums2[i] > nums2[stack[-1]]:
                idx = stack.pop()
                nxt[idx] = nums2[i] 
            stack.append(i)
        for i in range(m):
            res[i] = nxt[pos[nums1[i]]]
        return res 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容