Skip to content

Commit aca9b50

Browse files
committed
Batching question in 100
1 parent dd4c06d commit aca9b50

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+190
-1
lines changed

03.py 1-100q/03.py

File renamed without changes.

05.py 1-100q/05.py

File renamed without changes.

06.py 1-100q/06.py

File renamed without changes.

10.py 1-100q/10.py

File renamed without changes.

11.py 1-100q/11.py

File renamed without changes.

15.py 1-100q/15.py

File renamed without changes.

16.py 1-100q/16.py

File renamed without changes.

17.py 1-100q/17.py

File renamed without changes.

18.py 1-100q/18.py

File renamed without changes.

19.py 1-100q/19.py

File renamed without changes.

22.py 1-100q/22.py

File renamed without changes.

23.py 1-100q/23.py

File renamed without changes.

24.py 1-100q/24.py

File renamed without changes.

25.py 1-100q/25.py

File renamed without changes.

26.py 1-100q/26.py

File renamed without changes.

30.py 1-100q/30.py

File renamed without changes.

31.py 1-100q/31.py

File renamed without changes.

32.py 1-100q/32.py

File renamed without changes.

33.py 1-100q/33.py

File renamed without changes.

34.py 1-100q/34.py

File renamed without changes.

38.py 1-100q/38.py

File renamed without changes.

39.py 1-100q/39.py

File renamed without changes.

40.py 1-100q/40.py

File renamed without changes.

41.py 1-100q/41.py

File renamed without changes.

42.py 1-100q/42.py

File renamed without changes.

44.py 1-100q/44.py

File renamed without changes.

45.py 1-100q/45.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'''
1+
'''
22
Given an array of non-negative integers, you are initially positioned at the first index of the array.
33
44
Each element in the array represents your maximum jump length at that position.

46.py 1-100q/46.py

File renamed without changes.

48.py 1-100q/48.py

File renamed without changes.

53.py 1-100q/53.py

File renamed without changes.

54.py 1-100q/54.py

File renamed without changes.

56.py 1-100q/56.py

File renamed without changes.

57.py 1-100q/57.py

File renamed without changes.

60.py 1-100q/60.py

File renamed without changes.

61.py 1-100q/61.py

File renamed without changes.

62.py 1-100q/62.py

File renamed without changes.

63.py 1-100q/63.py

File renamed without changes.

64.py 1-100q/64.py

File renamed without changes.

65.py 1-100q/65.py

File renamed without changes.

66.py 1-100q/66.py

File renamed without changes.

67.py 1-100q/67.py

File renamed without changes.

70.py 1-100q/70.py

File renamed without changes.

71.py 1-100q/71.py

File renamed without changes.

72.py 1-100q/72.py

File renamed without changes.

73.py 1-100q/73.py

File renamed without changes.

74.py 1-100q/74.py

File renamed without changes.

75.py 1-100q/75.py

File renamed without changes.

78.py 1-100q/78.py

File renamed without changes.

79.py 1-100q/79.py

File renamed without changes.

80.py 1-100q/80.py

File renamed without changes.

81.py 1-100q/81.py

File renamed without changes.

82.py 1-100q/82.py

File renamed without changes.

83.py 1-100q/83.py

File renamed without changes.

85.py 1-100q/85.py

File renamed without changes.

86.py 1-100q/86.py

File renamed without changes.

87.py 1-100q/87.py

File renamed without changes.

90.py 1-100q/90.py

File renamed without changes.

91.py 1-100q/91.py

File renamed without changes.

92.py 1-100q/92.py

File renamed without changes.

93.py 1-100q/93.py

File renamed without changes.

1-100q/95.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'''
2+
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.
3+
4+
Example:
5+
6+
Input: 3
7+
Output:
8+
[
9+
[1,null,3,2],
10+
[3,2,null,1],
11+
[3,1,null,null,2],
12+
[2,1,3],
13+
[1,null,2,null,3]
14+
]
15+
Explanation:
16+
The above output corresponds to the 5 unique BST's shown below:
17+
18+
1 3 3 2 1
19+
\ / / / \ \
20+
3 2 1 1 3 2
21+
/ / \ \
22+
2 1 2 3
23+
'''
24+
25+
# Definition for a binary tree node.
26+
# class TreeNode(object):
27+
# def __init__(self, x):
28+
# self.val = x
29+
# self.left = None
30+
# self.right = None
31+
32+
class Solution(object):
33+
def generateTrees(self, n):
34+
"""
35+
:type n: int
36+
:rtype: List[TreeNode]
37+
"""
38+
if n == 0:
39+
return []
40+
41+
42+
def generate(start, end):
43+
result = []
44+
if start > end:
45+
result.append(None)
46+
return result
47+
48+
for index in range(start, end+1):
49+
left = generate(start, index-1)
50+
right = generate(index+1, end)
51+
52+
for l in left:
53+
for r in right:
54+
current = TreeNode(index)
55+
current.left = l
56+
current.right = r
57+
result.append(current)
58+
59+
return result
60+
61+
return generate(1, n)

1-100q/97.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
3+
4+
Example 1:
5+
6+
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
7+
Output: true
8+
Example 2:
9+
10+
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
11+
Output: false
12+
'''
13+
14+
class Solution(object):
15+
def isInterleave(self, s1, s2, s3):
16+
"""
17+
:type s1: str
18+
:type s2: str
19+
:type s3: str
20+
:rtype: bool
21+
"""
22+
23+
if len(s3) != len(s1) + len(s2):
24+
return False
25+
26+
dp = [[False for _ in range(len(s2)+1)] for _ in range(len(s1)+1)]
27+
for row in range(len(s1)+1):
28+
for col in range(len(s2)+1):
29+
if row == 0 and col == 0:
30+
dp[row][col] = True
31+
elif row == 0:
32+
dp[row][col] =dp[row][col-1] and s2[col-1] == s3[row+col-1]
33+
elif col == 0:
34+
dp[row][col] = dp[row-1][col] and s1[row-1] == s3[row+col-1]
35+
else:
36+
dp[row][col] = (dp[row][col-1] and s2[col-1] == s3[row+col-1]) or (dp[row-1][col] and s1[row-1] == s3[row+col-1])
37+
38+
return dp[len(s1)][len(s2)]
39+
40+
# Time: O(m*n)
41+
# Space: O(m*n)

1-100q/98.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
Given a binary tree, determine if it is a valid binary search tree (BST).
3+
4+
Assume a BST is defined as follows:
5+
6+
The left subtree of a node contains only nodes with keys less than the node's key.
7+
The right subtree of a node contains only nodes with keys greater than the node's key.
8+
Both the left and right subtrees must also be binary search trees.
9+
'''
10+
11+
# Definition for a binary tree node.
12+
# class TreeNode(object):
13+
# def __init__(self, x):
14+
# self.val = x
15+
# self.left = None
16+
# self.right = None
17+
18+
class Solution(object):
19+
def isValidBST(self, root):
20+
"""
21+
:type root: TreeNode
22+
:rtype: bool
23+
"""
24+
if not root:
25+
return True
26+
27+
stack, result = [], []
28+
while stack or root:
29+
if root:
30+
stack.append(root)
31+
root = root.left
32+
else:
33+
root = stack.pop()
34+
result.append(root.val)
35+
root = root.right
36+
37+
previous = result[0]
38+
for index in range(1, len(result)):
39+
if previous >= result[index]:
40+
return False
41+
previous = result[index]
42+
return True

TwoSum.py 1-100q/TwoSum.py

File renamed without changes.

100-200q/100.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''
2+
Given two binary trees, write a function to check if they are the same or not.
3+
4+
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
5+
6+
Example 1:
7+
8+
Input: 1 1
9+
/ \ / \
10+
2 3 2 3
11+
12+
[1,2,3], [1,2,3]
13+
14+
Output: true
15+
'''
16+
17+
# Definition for a binary tree node.
18+
# class TreeNode(object):
19+
# def __init__(self, x):
20+
# self.val = x
21+
# self.left = None
22+
# self.right = None
23+
24+
class Solution(object):
25+
def isSameTree(self, p, q):
26+
"""
27+
:type p: TreeNode
28+
:type q: TreeNode
29+
:rtype: bool
30+
"""
31+
if not p and not q:
32+
return True
33+
34+
stack = [(p, q)]
35+
36+
while stack:
37+
node1, node2 = stack.pop()
38+
if node1 and node2 and node1.val == node2.val:
39+
stack.append((node1.left, node2.left))
40+
stack.append((node1.right, node2.right))
41+
else:
42+
if not node1 == node2:
43+
return False
44+
45+
return True

0 commit comments

Comments
 (0)