Skip to content

Commit df23eda

Browse files
authored
Merge pull request #1472 from ivan1016017/december01
adding algos
2 parents c1e4345 + b95e2fc commit df23eda

6 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
3+
class Solution:
4+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
5+
6+
# Make a grid of 0's with len(text2) + 1 columns
7+
# and len(text1) + 1 rows.
8+
len_1 = len(text1)
9+
len_2 = len(text2)
10+
dp_grid = [[0]*(len_2+1) for _ in range(len_1+1)]
11+
12+
# Iterate up each column, starting from the last one.
13+
for j in reversed(range(len_2)):
14+
for i in reversed(range(len_1)):
15+
if text1[i] == text2[j]:
16+
dp_grid[i][j] = dp_grid[i+1][j+1] + 1
17+
else:
18+
dp_grid[i][j] = max(dp_grid[i+1][j], dp_grid[i][j+1])
19+
20+
# The original problem's answer is in dp_grid[0][0]. Return it.
21+
return dp_grid[0][0]
22+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
3+
# Definition for a binary tree node.
4+
class TreeNode:
5+
def __init__(self, x):
6+
self.val = x
7+
self.left = None
8+
self.right = None
9+
10+
class Solution:
11+
def lowestCommonAncestor(self, root: TreeNode, nodes: List[TreeNode]) -> 'TreeNode':
12+
13+
node_set = set(nodes)
14+
15+
def dfs(node: TreeNode):
16+
17+
if not node or node in node_set:
18+
return node
19+
20+
left = dfs(node.left)
21+
right = dfs(node.right)
22+
23+
if left and right:
24+
return node
25+
26+
return left if left else right
27+
28+
return dfs(root)
29+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC
3+
import math
4+
5+
class Solution:
6+
def maxProfit(self, prices: List[int]) -> int:
7+
8+
min_price = math.inf
9+
max_profit = 0
10+
len_prices = len(prices)
11+
12+
for i in range(len_prices):
13+
14+
if prices[i] < min_price:
15+
min_price = prices[i]
16+
elif prices[i] - min_price > max_profit:
17+
max_profit = prices[i] - min_price
18+
19+
return max_profit
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC
3+
4+
class Solution:
5+
def maxProfit(self, prices: List[int]) -> int:
6+
7+
i = 0
8+
peak = None
9+
valley = None
10+
len_prices = len(prices)
11+
result = 0
12+
13+
while i < len(prices) - 1:
14+
15+
while i < len_prices - 1 and prices[i] >= prices[i+1]:
16+
i += 1
17+
valley = prices[i]
18+
19+
while i < len_prices - 1 and prices[i] <= prices[i+1]:
20+
i += 1
21+
peak = prices[i]
22+
23+
result += peak - valley
24+
25+
return result
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.best_time_to_sell_stocks_i import Solution
4+
5+
class BestTimeToSellStockTestCase(unittest.TestCase):
6+
7+
def test_best_time_to_sell_stock(self):
8+
solution = Solution()
9+
output = solution.maxProfit(prices=[7,1,5,3,6,4])
10+
target = 5
11+
self.assertEqual(output, target)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import unittest
2+
import unittest
3+
from src.my_project.interviews.top_150_questions_round_22\
4+
.best_time_to_sell_stocks_ii import Solution
5+
6+
7+
class BestTimeToSellStockTestCase(unittest.TestCase):
8+
9+
def test_best_time_to_sell_stock(self):
10+
solution = Solution()
11+
output = solution.maxProfit(prices=[7,1,5,3,6,4])
12+
target = 7
13+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)