Skip to content

Commit 15dd9e5

Browse files
committed
Adding solution os 105, 106, 112, 113, 115, 123, 125, 139 problems
1 parent 93ac861 commit 15dd9e5

File tree

9 files changed

+379
-0
lines changed

9 files changed

+379
-0
lines changed

100-200q/105.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
3+
4+
Note:
5+
6+
The same word in the dictionary may be reused multiple times in the segmentation.
7+
You may assume the dictionary does not contain duplicate words.
8+
Example 1:
9+
10+
Input: s = "leetcode", wordDict = ["leet", "code"]
11+
Output: true
12+
Explanation: Return true because "leetcode" can be segmented as "leet code".
13+
'''
14+
15+
# Definition for a binary tree node.
16+
# class TreeNode(object):
17+
# def __init__(self, x):
18+
# self.val = x
19+
# self.left = None
20+
# self.right = None
21+
22+
class Solution(object):
23+
def buildTree(self, preorder, inorder):
24+
"""
25+
:type preorder: List[int]
26+
:type inorder: List[int]
27+
:rtype: TreeNode
28+
"""
29+
self.index = 0
30+
def recursive(preorder, inorder, start, end):
31+
if start > end:
32+
return None
33+
34+
node = TreeNode(preorder[self.index])
35+
self.index += 1
36+
if start == end:
37+
return node
38+
39+
search_index = 0
40+
for i in range(start, end+1):
41+
if inorder[i] == node.val:
42+
search_index = i
43+
break
44+
45+
node.left = recursive(preorder, inorder, start, search_index-1)
46+
node.right = recursive(preorder, inorder, search_index+1, end)
47+
return node
48+
49+
return recursive(preorder, inorder, 0, len(inorder)-1)

100-200q/106.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
Given inorder and postorder traversal of a tree, construct the binary tree.
3+
4+
Note:
5+
You may assume that duplicates do not exist in the tree.
6+
7+
For example, given
8+
9+
inorder = [9,3,15,20,7]
10+
postorder = [9,15,7,20,3]
11+
Return the following binary tree:
12+
13+
3
14+
/ \
15+
9 20
16+
/ \
17+
15 7
18+
'''
19+
20+
# Definition for a binary tree node.
21+
# class TreeNode(object):
22+
# def __init__(self, x):
23+
# self.val = x
24+
# self.left = None
25+
# self.right = None
26+
27+
class Solution(object):
28+
def buildTree(self, inorder, postorder):
29+
"""
30+
:type inorder: List[int]
31+
:type postorder: List[int]
32+
:rtype: TreeNode
33+
"""
34+
self.index = len(inorder)-1
35+
def recursive(postorder, inorder, start, end):
36+
if start > end:
37+
return None
38+
39+
node = TreeNode(postorder[self.index])
40+
self.index -= 1
41+
if start == end:
42+
return node
43+
44+
search_index = 0
45+
for i in range(start, end+1):
46+
if inorder[i] == node.val:
47+
search_index = i
48+
break
49+
node.right = recursive(postorder, inorder, search_index+1, end)
50+
node.left = recursive(postorder, inorder, start, search_index-1)
51+
return node
52+
53+
return recursive(postorder, inorder, 0, len(inorder)-1)

100-200q/112.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
3+
4+
Note: A leaf is a node with no children.
5+
6+
Example:
7+
8+
Given the below binary tree and sum = 22,
9+
10+
5
11+
/ \
12+
4 8
13+
/ / \
14+
11 13 4
15+
/ \ \
16+
7 2 1
17+
'''
18+
19+
20+
# Definition for a binary tree node.
21+
# class TreeNode(object):
22+
# def __init__(self, x):
23+
# self.val = x
24+
# self.left = None
25+
# self.right = None
26+
27+
class Solution(object):
28+
def hasPathSum(self, root, sum):
29+
"""
30+
:type root: TreeNode
31+
:type sum: int
32+
:rtype: bool
33+
"""
34+
if not root:
35+
return False
36+
37+
if not root.left and not root.right and root.val == sum:
38+
return True
39+
40+
return self.hasPathSum(root.left, sum-root.val) or self.hasPathSum(root.right, sum-root.val)

100-200q/113.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'''
2+
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
3+
4+
Note: A leaf is a node with no children.
5+
6+
Example:
7+
8+
Given the below binary tree and sum = 22,
9+
10+
5
11+
/ \
12+
4 8
13+
/ / \
14+
11 13 4
15+
/ \ / \
16+
7 2 5 1
17+
Return:
18+
19+
[
20+
[5,4,11,2],
21+
[5,8,4,5]
22+
]
23+
24+
'''
25+
26+
# Definition for a binary tree node.
27+
# class TreeNode(object):
28+
# def __init__(self, x):
29+
# self.val = x
30+
# self.left = None
31+
# self.right = None
32+
33+
class Solution(object):
34+
def pathSum(self, root, sum):
35+
"""
36+
:type root: TreeNode
37+
:type sum: int
38+
:rtype: List[List[int]]
39+
"""
40+
41+
result = []
42+
43+
def dfs(root, curr_sum, sum, path, result):
44+
if not root:
45+
return
46+
47+
curr_sum += root.val
48+
if curr_sum == sum and not root.left and not root.right:
49+
result.append(path + [root.val])
50+
return
51+
52+
if root.left:
53+
dfs(root.left, curr_sum, sum, path + [root.val], result)
54+
if root.right:
55+
dfs(root.right, curr_sum, sum, path + [root.val], result)
56+
57+
dfs(root, 0, sum, [], result)
58+
return result

100-200q/115.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Given a string S and a string T, count the number of distinct subsequences of S which equals T.
3+
4+
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).
5+
6+
Example 1:
7+
8+
Input: S = "rabbbit", T = "rabbit"
9+
Output: 3
10+
Explanation:
11+
12+
As shown below, there are 3 ways you can generate "rabbit" from S.
13+
(The caret symbol ^ means the chosen letters)
14+
15+
rabbbit
16+
^^^^ ^^
17+
rabbbit
18+
^^ ^^^^
19+
rabbbit
20+
^^^ ^^^
21+
'''
22+
23+
class Solution(object):
24+
def numDistinct(self, s, t):
25+
"""
26+
:type s: str
27+
:type t: str
28+
:rtype: int
29+
"""
30+
31+
row, col = len(s), len(t)
32+
33+
if col > row:
34+
return 0
35+
36+
dp = [[0 for _ in range(col+1)] for _ in range(row+1)]
37+
38+
for r in range(row+1):
39+
for c in range(col+1):
40+
if r == 0 and c == 0:
41+
dp[r][c] = 1
42+
elif r == 0:
43+
dp[r][c] = 0
44+
elif c == 0:
45+
dp[r][c] = 1
46+
else:
47+
dp[r][c] = dp[r-1][c]
48+
if s[r-1] == t[c-1]:
49+
dp[r][c] += dp[r-1][c-1]
50+
return dp[row][col]

100-200q/120.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
3+
4+
For example, given the following triangle
5+
6+
[
7+
[2],
8+
[3,4],
9+
[6,5,7],
10+
[4,1,8,3]
11+
]
12+
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
13+
'''
14+
15+
class Solution(object):
16+
def minimumTotal(self, triangle):
17+
"""
18+
:type triangle: List[List[int]]
19+
:rtype: int
20+
"""
21+
length = len(triangle)
22+
columns = len(triangle[length-1])
23+
24+
matrix = [[ 0 for col in range(columns)] for row in range(length)]
25+
row_index = 0
26+
27+
for row in range(length):
28+
elements = triangle[row]
29+
col_index = 0
30+
31+
for val in elements:
32+
matrix[row_index][col_index] = val
33+
col_index += 1
34+
row_index += 1
35+
36+
for row in range(length-2, -1, -1):
37+
for col in range(row+1):
38+
matrix[row][col] += min(matrix[row+1][col+1], matrix[row+1][col])
39+
return matrix[0][0]

100-200q/123.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
Say you have an array for which the ith element is the price of a given stock on day i.
3+
4+
Design an algorithm to find the maximum profit. You may complete at most two transactions.
5+
6+
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
7+
8+
Example 1:
9+
10+
Input: [3,3,5,0,0,3,1,4]
11+
Output: 6
12+
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
13+
Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
14+
15+
'''
16+
17+
class Solution(object):
18+
def maxProfit(self, prices):
19+
"""
20+
:type prices: List[int]
21+
:rtype: int
22+
"""
23+
if len(prices) < 2:
24+
return 0
25+
dp = [[0 for _ in range(len(prices))] for _ in range(3)]
26+
for i in range(1,3):
27+
maxDiff = -prices[0]
28+
for j in range(1,len(prices)):
29+
dp[i][j] = max(dp[i][j-1], prices[j] + maxDiff)
30+
maxDiff = max(maxDiff, dp[i-1][j] -prices[j])
31+
32+
return dp[2][len(prices)-1]
33+

100-200q/125.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution(object):
2+
def numDistinct(self, s, t):
3+
"""
4+
:type s: str
5+
:type t: str
6+
:rtype: int
7+
"""
8+
9+
row, col = len(s), len(t)
10+
11+
if col > row:
12+
return 0
13+
14+
dp = [[0 for _ in range(col+1)] for _ in range(row+1)]
15+
16+
for r in range(row+1):
17+
for c in range(col+1):
18+
if r == 0 and c == 0:
19+
dp[r][c] = 1
20+
elif r == 0:
21+
dp[r][c] = 0
22+
elif c == 0:
23+
dp[r][c] = 1
24+
else:
25+
dp[r][c] = dp[r-1][c]
26+
if s[r-1] == t[c-1]:
27+
dp[r][c] += dp[r-1][c-1]
28+
return dp[row][col]

100-200q/139.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'''
2+
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
3+
4+
Note:
5+
6+
The same word in the dictionary may be reused multiple times in the segmentation.
7+
You may assume the dictionary does not contain duplicate words.
8+
Example 1:
9+
10+
Input: s = "leetcode", wordDict = ["leet", "code"]
11+
Output: true
12+
Explanation: Return true because "leetcode" can be segmented as "leet code".
13+
'''
14+
15+
class Solution(object):
16+
def wordBreak(self, s, wordDict):
17+
"""
18+
:type s: str
19+
:type wordDict: List[str]
20+
:rtype: bool
21+
"""
22+
dp = [False for _ in range(len(s)+1)]
23+
dp[0] = True
24+
for index in range(len(s)):
25+
for j in range(i, -1, -1):
26+
if dp[j] and s[j:i+1] in wordDict:
27+
dp[i+1] = True
28+
wordBreak
29+
return dp[len(s)]

0 commit comments

Comments
 (0)