leetcodeweekly_3.17 Container with Most Water

Symmetric Tree

Tag: Recursive Method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class (object):
def isMirror(self, t1, t2):
if t1 == None and t2 == None:
return True
elif t1 == None or t2 == None:
return False
return (t1.val == t2.val) and self.isMirror(t1.left, t2.right) and self.isMirror(t1.right, t2.left)

def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
return self.isMirror(root, root)

Iterative Method

Container with Most Water

Tag: Array, Two Pointers

Given/n/non-negative integers/a//1/,/a//2/, …,/a//n/, where each represents a point at coordinate (/i/,/a//i/)./n/vertical lines are drawn such that the two endpoints of line/i/is at (/i/,/a//i/) and (/i/, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note:You may not slant the container and/n/is at least 2.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class (object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
max_area = 0
l, r = 0, len(height) - 1
while l < r:
area = 0
if height[l] > height[r]:
area = (r-l)*height[r]
r -= 1
else:
area = (r-l)*height[l]
l += 1
max_area = max(area, max_area)
return max_area