Skip to content

Commit 1924490

Browse files
committed
Adding solutions for Assignment 6
1 parent 2bff469 commit 1924490

13 files changed

+232
-0
lines changed

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,25 @@ I've done four Dynamic Programming problems on InterviewBit so far:
206206
And here's a GIF of my InterviewBit Dynamic Programming topic page:
207207

208208
* [GIF of my InterviewBit Dynamic Programming topic page](https://github.com/tachyonlabs/CodePath-Alumni-Professional-Interview-Prep-Course/blob/master/interviewbit-dynamic-programming-topic.gif)
209+
210+
## Assignment 6
211+
212+
I did eleven more Dynamic Programming problems on InterviewBit:
213+
214+
* Length of Longest Subsequence
215+
* Ways to Decode
216+
* Best Time to Buy and Sell Stocks III
217+
* Best Time to Buy and Sell Stocks I
218+
* Best Time to Buy and Sell Stocks II
219+
* Max Sum Without Adjacent Elements
220+
* Edit Distance
221+
* Longest Increasing Subsequence
222+
* Max Sum Path in Binary Tree
223+
* Unique Binary Search Trees II
224+
* Word Break
225+
226+
And here's a GIF of my updated InterviewBit Dynamic Programming topic page:
227+
228+
* GIF of my updated InterviewBit Dynamic Programming topic page
229+
230+
I took the "CodePath Interview Prep - Unit 6" test on February 4, so you should have received it.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
# @param A : tuple of integers
3+
# @return an integer
4+
def maxProfit(self, A):
5+
if not A or len(A) < 2:
6+
return 0
7+
8+
max_so_far = max_ending_here = A[1] - A[0]
9+
for i in range(2, len(A)):
10+
max_ending_here = max(max_ending_here + A[i] - A[i - 1], A[i] - A[i - 1])
11+
max_so_far = max(max_so_far, max_ending_here)
12+
13+
return max(max_so_far, 0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
# @param A : tuple of integers
3+
# @return an integer
4+
def maxProfit(self, A):
5+
max_p = 0
6+
own = False
7+
bought_price = None
8+
for i in range(len(A)):
9+
if not own and i != len(A) - 1 and A[i + 1] > A[i]:
10+
own = True
11+
bought_price = A[i]
12+
elif own and (i == len(A) - 1 or A[i + 1] < A[i]):
13+
own = False
14+
max_p += A[i] - bought_price
15+
16+
return max_p
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
# @param A : tuple of integers
3+
# @return an integer
4+
def maxProfit(self, A):
5+
if not A or len(A) < 2:
6+
return 0
7+
8+
maxes = [[0 for i in range(len(A))] for j in range(2)]
9+
10+
# modified Kadane's
11+
maxes[0][1] = max_ending_here = max_so_far = A[1] - A[0]
12+
for i in range(2, len(A)):
13+
max_ending_here = max(max_ending_here + A[i] - A[i - 1], A[i] - A[i -1])
14+
max_so_far = max(max_so_far, max_ending_here)
15+
maxes[0][i] = max_so_far
16+
17+
if maxes[0][-1] <= 0:
18+
return 0
19+
20+
if len(A) < 4:
21+
return maxes[0][-1]
22+
23+
max_diff = -A[0]
24+
25+
for i in range(1, len(A)):
26+
max_for_one_or_two = max(maxes[1][i - 1], A[i] + max_diff)
27+
maxes[1][i] = max_for_one_or_two
28+
max_diff = max(max_diff, maxes[0][i] - A[i])
29+
30+
# a workaround for some kind of bug I still have, maxes[1][-1] should be enough :-(
31+
return max(maxes[0][-1], maxes[1][-1])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
# @param A : string
3+
# @param B : string
4+
# @return an integer
5+
def minDistance(self, A, B):
6+
dp_table = []
7+
dp_table.append([i for i in range(len(A) + 1)])
8+
for i in range(1, len(B) + 1):
9+
dp_table.append([i] + [0] * len(A))
10+
11+
A, B = " " + A, " " + B
12+
13+
for b in range(1, len(B)):
14+
for a in range(1, len(A)):
15+
if A[a] != B[b]:
16+
dp_table[b][a] = min(dp_table[b][a - 1], dp_table[b - 1][a], dp_table[b - 1][a - 1]) + 1
17+
else:
18+
dp_table[b][a] = dp_table[b - 1][a - 1]
19+
20+
return dp_table[-1][-1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
# @param A : tuple of integers
3+
# @return an integer
4+
def longestSubsequenceLength(self, A):
5+
inc = [1] * len(A)
6+
dec = inc[:]
7+
for i in range(1, len(A)):
8+
for j in range(0, i):
9+
if A[j] < A[i]:
10+
inc[i] = max(inc[i], inc[j] + 1)
11+
for i in range(len(A) - 2, -1, -1):
12+
for j in range(len(A) - 1, i, -1):
13+
if A[j] < A[i]:
14+
dec[i] = max(dec[i], dec[j] + 1)
15+
16+
longest = 0
17+
18+
for i in range(len(A)):
19+
longest = max(longest, inc[i] + dec[i] - 1)
20+
21+
return longest
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
# @param A : tuple of integers
3+
# @return an integer
4+
def lis(self, A):
5+
counts = [1] * len(A)
6+
for i in range(1, len(A)):
7+
for j in range(0, i):
8+
if A[j] < A[i]:
9+
counts[i] = max(counts[i], counts[j] + 1)
10+
11+
return max(counts)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Definition for a binary tree node
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
# @param A : root node of tree
10+
# @return an integer
11+
def get_max_path_sum(self, node):
12+
if not node:
13+
return 0
14+
15+
left_max_sum_path = self.get_max_path_sum(node.left)
16+
right_max_sum_path = self.get_max_path_sum(node.right)
17+
18+
max_with_one_or_no_child_paths = max(max(left_max_sum_path, right_max_sum_path) + node.val, node.val)
19+
max_with_both_child_paths = max(max_with_one_or_no_child_paths,
20+
left_max_sum_path + right_max_sum_path + node.val)
21+
self.max_sum_path = max(self.max_sum_path, max_with_both_child_paths)
22+
return max_with_one_or_no_child_paths
23+
24+
def maxPathSum(self, A):
25+
self.max_sum_path = float("-inf")
26+
self.get_max_path_sum(A)
27+
return self.max_sum_path
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
# @param A : list of list of integers
3+
# @return an integer
4+
def adjacent(self, A):
5+
A[0] = [max(A[0][i], A[1][i]) for i in range(len(A[0]))]
6+
inclusive = exclusive = 0
7+
for num in A[0]:
8+
new_inclusive = max(inclusive, exclusive + num)
9+
exclusive = max(exclusive, inclusive)
10+
inclusive = new_inclusive
11+
12+
return max(inclusive, exclusive)
7.43 MB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import math
2+
3+
class Solution:
4+
# @param A : integer
5+
# @return an integer
6+
def numTrees(self, A):
7+
return math.factorial(2 * A) / (math.factorial(A) * math.factorial(A + 1))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import re
2+
3+
class Solution:
4+
# @param A : string
5+
# @return an integer
6+
def count_decodings(self, A, i):
7+
if i >= len(A) - 1:
8+
return 1
9+
if self.counts[i] != -1:
10+
return self.counts[i]
11+
if A[i + 1] == "0":
12+
count = self.count_decodings(A, i + 2)
13+
elif A[i] > "2" or (A[i] == "2" and A[i + 1] > "6") or (i <= len(A) - 3 and A[i + 2] == "0"):
14+
count = self.count_decodings(A, i + 1)
15+
else:
16+
count = self.count_decodings(A, i + 1) + self.count_decodings(A, i + 2)
17+
18+
self.counts[i] = count
19+
return count
20+
21+
def numDecodings(self, A):
22+
if A[0] == "0" or re.search("[0,3-9]0", A):
23+
return 0
24+
25+
self.counts = [-1] * len(A)
26+
return self.count_decodings(A, 0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
# @param A : string
3+
# @param B : list of strings
4+
# @return an integer
5+
def wordBreak(self, A, B):
6+
min_word_len = float("inf")
7+
max_word_len = float("-inf")
8+
dictionary = {}
9+
# putting the words in a hash for faster lookups, plus getting min and max word length
10+
# so we don't waste time looking up substrings that are too small or too large
11+
for word in B:
12+
dictionary[word] = 1
13+
min_word_len = min(min_word_len, len(word))
14+
max_word_len = max(max_word_len, len(word))
15+
16+
# to keep track of positions where the substring to the left can be successfully divided into words
17+
we_can_do_it_to_here = [False] * (len(A) + 1)
18+
we_can_do_it_to_here[0] = True
19+
20+
for i in range(min_word_len, len(A) + 1):
21+
for j in range(max(0, i - max_word_len), i - min_word_len + 1):
22+
if we_can_do_it_to_here[j] and A[j:i] in dictionary:
23+
we_can_do_it_to_here[i] = True
24+
break
25+
26+
return int(we_can_do_it_to_here[-1])

0 commit comments

Comments
 (0)