Skip to content

Commit b5e48b0

Browse files
committed
[main] Added dp problems
1 parent 8e74c0d commit b5e48b0

16 files changed

+389
-0
lines changed

pythonProblems/dp/4sum.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
6+
# b - c - d = a
7+
# a - c - d = b
8+
# a - b - d = c
9+
# a - b - c = d
10+
nums_clone = []
11+
pairs = []
12+
i, j, k, l = 0, 0, 0, 0
13+
while i < len(nums):
14+
a = abs(nums[i] - target)
15+
nums_clone = nums[0:i] + nums[i + 1:]
16+
while j < len(nums_clone):
17+
b = a - nums_clone[j]
18+
j += 1
19+
if b in nums_clone:
20+
b_ind = nums_clone.index(b)
21+
nums_clone = nums_clone[0:b_ind] + nums_clone[b_ind + 1:]
22+
while k < len(nums_clone):
23+
c = nums_clone[k] - b
24+
k += 1
25+
if c in nums_clone:
26+
c_ind = nums_clone.index(c)
27+
nums_clone = nums_clone[0: c_ind] + nums_clone[c_ind:]
28+
while l < len(nums_clone):
29+
d = nums_clone[l] - c
30+
l += 1
31+
if d in nums_clone:
32+
## found pair
33+
if sorted([a, b, c, d]) not in pairs and a + b + c + d == target:
34+
print(
35+
a, b, c, d, nums_clone)
36+
pairs.append(sorted([a, b, c, d]))
37+
j, k, l = 0, 0, 0
38+
break
39+
l = 0
40+
k = 0
41+
j = 0
42+
i += 1
43+
return pairs
44+
45+
46+
if __name__ == '__main__':
47+
sol = Solution()
48+
print(sol.fourSum([2,2,2,2,2], 8))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
dp = [0, 1]
4+
for i in range(1, n + 1):
5+
dp.append(dp[i - 1] + dp[i - 2])
6+
print(dp)
7+
8+
9+
if __name__ == '__main__':
10+
sol = Solution()
11+
print(sol.climbStairs(5))

pythonProblems/dp/cutrod.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import random
2+
3+
4+
def memo_cut_rod_aux(p, n, r):
5+
q = -1
6+
if r[n] >= 0:
7+
return r[n]
8+
if n == 0:
9+
q = 0
10+
else:
11+
q = -1
12+
for i in range(1, n + 1):
13+
q = max(q, p[i] + memo_cut_rod_aux(p, n - i, r))
14+
r[n] = q
15+
return q
16+
17+
18+
def memo_cut_rod(p, n):
19+
r = [0] * (n + 1)
20+
for i in range(n + 1):
21+
r[i] = -1
22+
return memo_cut_rod_aux(p, n, r)
23+
24+
25+
if __name__ == '__main__':
26+
range_ = 20
27+
prices = {}
28+
for i in range(1, range_ + 1):
29+
prices[i] = random.randint(10, 200)
30+
print(memo_cut_rod(prices, range_))

pythonProblems/dp/divide.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import math
2+
3+
4+
class Solution:
5+
neg_lim = -2147483648
6+
pos_lim = 2147483648 - 1
7+
8+
def divide(self, dividend: int, divisor: int) -> int:
9+
if divisor == 1:
10+
return dividend
11+
elif divisor == -1:
12+
if dividend < 0:
13+
## flips to positive, account for case
14+
return abs(dividend) if abs(dividend) < Solution.pos_lim else Solution.pos_lim
15+
else:
16+
return -1 * dividend
17+
dividend = dividend if dividend >= Solution.neg_lim and dividend <= Solution.pos_lim else Solution.neg_lim if dividend < 0 else Solution.pos_lim
18+
is_negative = (dividend < 0 and divisor > 0) or (
19+
divisor < 0 and dividend > 0)
20+
res = math.exp(math.log(abs(dividend)) -
21+
math.log(abs(divisor))) + 0.0000000001
22+
return int(res) if not is_negative else -1 * int(res)
23+
24+
25+
if __name__ == '__main__':
26+
sol = Solution()
27+
print(sol.divide(-231, 3))

pythonProblems/dp/dominantIndex.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def dominantIndex(self, nums: List[int]) -> int:
6+
nums.sort()
7+
return nums[-1] >= (nums[-2] * 2)

pythonProblems/dp/fib.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
memo = {}
3+
4+
def fib(self, n: int) -> int:
5+
if n in Solution.memo:
6+
return Solution.memo[n]
7+
elif n == 1 or n == 0:
8+
return n
9+
else:
10+
result = Solution.fib(self, n - 2) + Solution.fib(self, n - 1)
11+
Solution.memo[n] = result
12+
return result
13+
14+
15+
if __name__ == '__main__':
16+
sol = Solution()
17+
sol.fib(2)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
from copy import deepcopy
3+
from typing import List
4+
5+
6+
class Solution:
7+
def generateParenthesis(self, n: int, generated_parens: List[str] = None, open_count=0, close_count=0, curr_pair: str = '') -> List[str]:
8+
if generated_parens == None:
9+
generated_parens = []
10+
if open_count == close_count and open_count + close_count == (n * 2):
11+
generated_parens.append(curr_pair)
12+
return generated_parens
13+
else:
14+
if close_count > open_count or open_count + close_count > (n * 2):
15+
return ['']
16+
self.generateParenthesis(n, generated_parens, open_count, close_count + 1,
17+
curr_pair + ')')
18+
self.generateParenthesis(n, generated_parens, open_count + 1, close_count,
19+
curr_pair + '(')
20+
return generated_parens
21+
22+
23+
if __name__ == '__main__':
24+
sol = Solution()
25+
print(sol.generateParenthesis(2))
26+
print(sol.generateParenthesis(1))

pythonProblems/dp/longestsub.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def longest_sub(sub1: str, sub2: str) -> str:
2+
print('sub1 = {} and sub2 = {}'.format(sub1, sub2))
3+
4+
5+
if __name__ == '__main__':
6+
longest_sub('ABCBDAB', 'BDCABA')

pythonProblems/dp/longestvalid.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def longestValidParentheses(self, s: str) -> int:
3+
dp = [0] * len(s)
4+
for i in range(len(s)):
5+
if s[i] == ')':
6+
if i - 1 < 0:
7+
continue
8+
elif s[i - 1] == '(':
9+
dp[i] = dp[i - 2] + 2
10+
elif i - dp[i - 1] - 1 < 0:
11+
continue
12+
elif s[i - dp[i - 1] - 1] == '(':
13+
## matching over the current pair
14+
dp[i] = dp[i - 1] + 2 + dp[i - dp[i - 1] - 2]
15+
return max(dp)
16+
17+
18+
if __name__ == '__main__':
19+
sol = Solution()
20+
print(sol.longestValidParentheses("(()))())("
21+
))

pythonProblems/dp/minOperations.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
class Solution:
3+
4+
def __init__(self):
5+
self.min_op_hash = {}
6+
7+
def minOperation(self, n, targ=1, steps=1):
8+
if targ in self.min_op_hash:
9+
return self.min_op_hash[targ]
10+
elif targ > n:
11+
self.min_op_hash[targ] = steps + 1
12+
return steps + 1
13+
elif targ == n:
14+
self.min_op_hash[targ] = steps
15+
return steps
16+
else:
17+
print(self.min_op_hash)
18+
return min(self.minOperation(n, targ * 2, steps + 1), self.minOperation(n, targ + 1, steps + 1))
19+
20+
21+
if __name__ == '__main__':
22+
sol = Solution()
23+
print(sol.minOperation(142421))

0 commit comments

Comments
 (0)