Skip to content

Commit bf0a103

Browse files
authored
Merge pull request #1534 from ivanpenaloza/february01
adding algo
2 parents 2dece8c + 9ba58c4 commit bf0a103

5 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
7+
answer = dict()
8+
9+
for k, v in enumerate(nums):
10+
11+
if v in answer:
12+
return [answer[v], k]
13+
else:
14+
answer[target - v] = k
15+
16+
return []
17+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import re
4+
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
8+
# To lowercase
9+
s = s.lower()
10+
11+
# Remove non-alphanumeric characters
12+
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)
13+
14+
# Determine if s is palindrome or not
15+
16+
len_s = len(s)
17+
18+
for i in range(len_s//2):
19+
20+
if s[i] != s[len_s - 1 - i]:
21+
return False
22+
23+
return True
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
6+
max_candies = max(candies)
7+
return [candy + extraCandies >= max_candies for candy in candies]
8+
9+
10+
11+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
30+
31+

0 commit comments

Comments
 (0)