day48-mono stack-part1-8.19

tasks for today

1. 739.每日温度

2. 497.下一个最大元素I

3. 503.下一个最大元素II

------------------------------------------------------------------------------

1. 739.每日温度

This practice should be solved with mono stack, such practices which are suitable for mono stack has a feature of: (1) 1-dim list; (2) finding the bigger element in the right/left hand side of the current element;

when using mono stack, several key points should be noted: (1) the recorded element in the mono stack is the index of the elements in the given list; (2) the order of the stack should be ascending or decending (based on the value of elements in the given list; the order means from the top of the stack to the bottom of the stack.), if find the bigger elements, that should be ascending from top to bottom, while finding the smaller elements leads to a descending from top to bottom.

class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        if len(temperatures) == 1: return [0]

        result = [0] * len(temperatures)
        monoStack = [0]

        for i in range(1, len(temperatures)):
            if temperatures[i] <= temperatures[monoStack[-1]]:
                monoStack.append(i)
            else:
                while len(monoStack) != 0 and temperatures[i] > temperatures[monoStack[-1]]:
                    eleInd = monoStack.pop()
                    result[eleInd] = i - eleInd
                monoStack.append(i)

        return result 

2. 497.下一个最大元素I

In this practice, there should be some judgement accross the process, only need to update the result of the value in nums1 which show up in nums2.

class Solution:
    def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
        result = [-1] * len(nums1)
        monoStack = [0]

        for i in range(1, len(nums2)):
            if nums2[i] <= nums2[monoStack[-1]]:
                monoStack.append(i)
            else:
                while len(monoStack) != 0 and nums2[i] > nums2[monoStack[-1]]:
                    eleInd = monoStack.pop()
                    if nums2[eleInd] in nums1:
                        correspondInd = nums1.index(nums2[eleInd])
                        result[correspondInd] = nums2[i]
                monoStack.append(i)
            
        return result

        # result = [-1]*len(nums1)
        # stack = [0]
        # for i in range(1,len(nums2)):
        #     # 情况一情况二
        #     if nums2[i]<=nums2[stack[-1]]:
        #         stack.append(i)
        #     # 情况三
        #     else:
        #         while len(stack)!=0 and nums2[i]>nums2[stack[-1]]:
        #             if nums2[stack[-1]] in nums1:
        #                 index = nums1.index(nums2[stack[-1]])
        #                 result[index]=nums2[i]
        #             stack.pop()                 
        #         stack.append(i)
        # return result

3. 503.下一个最大元素II

In this practice, the key is to note the feature of the nums, which is a recursive list. Before using the findbigger, there should be some operation on the list.

For recursive list, concatanating two list together can solve the problem.

class Solution:
    def nextGreaterElements(self, nums: List[int]) -> List[int]:
        if len(nums) == 1: return [-1]

        numnew = nums + nums

        result = self.findBigger(numnew)

        return result[:len(nums)]

    def findBigger(self, nums):
        result = [-1] * len(nums)
        monStack = [0]

        for i in range(1, len(nums)):
            if nums[i] <= nums[monStack[-1]]:
                monStack.append(i)
            else:
                while len(monStack) != 0 and nums[i] > nums[monStack[-1]]:
                    eleInd = monStack.pop()
                    result[eleInd] = nums[i]
                monStack.append(i)
        
        return result
  • 17
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值