Skip to content

Commit

Permalink
Batching question in 100
Browse files Browse the repository at this point in the history
  • Loading branch information
Garvit244 committed May 11, 2018
1 parent dd4c06d commit aca9b50
Show file tree
Hide file tree
Showing 65 changed files with 190 additions and 1 deletion.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion 45.py → 1-100q/45.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'''
'''
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
61 changes: 61 additions & 0 deletions 1-100q/95.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'''
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.
Example:
Input: 3
Output:
[
[1,null,3,2],
[3,2,null,1],
[3,1,null,null,2],
[2,1,3],
[1,null,2,null,3]
]
Explanation:
The above output corresponds to the 5 unique BST's shown below:
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
'''

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def generateTrees(self, n):
"""
:type n: int
:rtype: List[TreeNode]
"""
if n == 0:
return []


def generate(start, end):
result = []
if start > end:
result.append(None)
return result

for index in range(start, end+1):
left = generate(start, index-1)
right = generate(index+1, end)

for l in left:
for r in right:
current = TreeNode(index)
current.left = l
current.right = r
result.append(current)

return result

return generate(1, n)
41 changes: 41 additions & 0 deletions 1-100q/97.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'''
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
Example 1:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
Example 2:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
Output: false
'''

class Solution(object):
def isInterleave(self, s1, s2, s3):
"""
:type s1: str
:type s2: str
:type s3: str
:rtype: bool
"""

if len(s3) != len(s1) + len(s2):
return False

dp = [[False for _ in range(len(s2)+1)] for _ in range(len(s1)+1)]
for row in range(len(s1)+1):
for col in range(len(s2)+1):
if row == 0 and col == 0:
dp[row][col] = True
elif row == 0:
dp[row][col] =dp[row][col-1] and s2[col-1] == s3[row+col-1]
elif col == 0:
dp[row][col] = dp[row-1][col] and s1[row-1] == s3[row+col-1]
else:
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])

return dp[len(s1)][len(s2)]

# Time: O(m*n)
# Space: O(m*n)
42 changes: 42 additions & 0 deletions 1-100q/98.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'''
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.
'''

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root:
return True

stack, result = [], []
while stack or root:
if root:
stack.append(root)
root = root.left
else:
root = stack.pop()
result.append(root.val)
root = root.right

previous = result[0]
for index in range(1, len(result)):
if previous >= result[index]:
return False
previous = result[index]
return True
File renamed without changes.
45 changes: 45 additions & 0 deletions 100-200q/100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'''
Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
Example 1:
Input: 1 1
/ \ / \
2 3 2 3
[1,2,3], [1,2,3]
Output: true
'''

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if not p and not q:
return True

stack = [(p, q)]

while stack:
node1, node2 = stack.pop()
if node1 and node2 and node1.val == node2.val:
stack.append((node1.left, node2.left))
stack.append((node1.right, node2.right))
else:
if not node1 == node2:
return False

return True

0 comments on commit aca9b50

Please sign in to comment.