11
Title description:
Give you n non-negative integers a1, a2,..., an, each of which represents a point (i, ai) in the coordinates. Draw n vertical lines in the coordinates. The two end points of the vertical line i are (i, ai) and (i, 0) respectively. Find two of the lines so that the container that they form with the x-axis can hold the most water.
Example:
Answer:
class Solution:
def maxArea(self, height: List[int]) -> int:
maxv=0
i,j=0,len(height)-1
while i<j:
h=min(height[i],height[j])
maxv=max(maxv,h*(j-i))
if height[i]<height[j]:
i+=1
else:
j-=1
return maxv
21
Title description:
Combine two ascending linked lists into a new ascending linked list and return. The new linked list is composed by splicing all the nodes of the given two linked lists.
Example:
Answer:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
res = ListNode(None)
node = res
while l1 and l2:
if l1.val<l2.val:
node.next,l1 = l1,l1.next
else:
node.next,l2 = l2,l2.next
node = node.next
if l1:
node.next = l1
else:
node.next = l2
return res.next
76
Title description:
Give you a string s and a string t. Returns the smallest substring of s that covers all characters of t. If there is no substring in s that covers all characters of t, an empty string "" is returned.
Example:
Answer:
class Solution:
def minWindow(self, s: str, t: str) -> str:
mem=defaultdict(int)
for char in t:
mem[char]+=1
t_len=len(t)
minLeft,minRight=0,len(s)
left=0
for right,char in enumerate(s):
if mem[char]>0:
t_len-=1
mem[char]-=1
if t_len==0:
while mem[s[left]]<0:
mem[s[left]]+=1
left+=1
if right-left<minRight-minLeft:
minLeft,minRight=left,right
mem[s[left]]+=1
t_len+=1
left+=1
return '' if minRight==len(s) else s[minLeft:minRight+1]
84
Title description:
Given n non-negative integers, they are used to represent the height of each column in the histogram. Each pillar is adjacent to each other and has a width of 1.
Find the maximum area of the rectangle that can be outlined in the histogram.
Example:
Answer:
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
'''
超时
res = 0
n = len(heights)
for i in range(n):
left_i = i
right_i = i
while left_i >= 0 and heights[left_i] >= heights[i]:
left_i -= 1
while right_i < n and heights[right_i] >= heights[i]:
right_i += 1
res = max(res, (right_i - left_i - 1) * heights[i])
return res
'''
#单调栈
stack = []
heights = [0] + heights + [0]
res = 0
for i in range(len(heights)):
#print(stack)
while stack and heights[stack[-1]] > heights[i]:
tmp = stack.pop()
res = max(res, (i - stack[-1] - 1) * heights[tmp])
stack.append(i)
return res
665
Title description:
Give you an integer array of length n. Please judge whether the array can become a non-decreasing sequence when one element is changed at most.
We define a non-decreasing number sequence like this: For all i (0 <= i <= n-2) in the array, nums[i] <= nums[i + 1] is always satisfied.
Example:
Answer:
class Solution:
def checkPossibility(self, nums: List[int]) -> bool:
N = len(nums)
count = 0
for i in range(1, N):
if nums[i] < nums[i - 1]:
count += 1
if i == 1 or nums[i] >= nums[i - 2]:
nums[i - 1] = nums[i]
else:
nums[i] = nums[i - 1]
return count <= 1