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