Skip to content

Commit 54d00fb

Browse files
authored
Merge pull request #1450 from ivan1016017/november09
adding algo
2 parents 5ef583d + 0b9885d commit 54d00fb

5 files changed

Lines changed: 107 additions & 0 deletions

File tree

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='[^a-zA-Z0-9]', repl='', string=s)
13+
14+
# Determine if it 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: 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: 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 minSwaps(self, data: List[int]) -> int:
5+
6+
# Window size
7+
k = sum(data)
8+
9+
answer = val = 0
10+
11+
for i, v in enumerate(data):
12+
13+
val += v
14+
15+
if i >= k:
16+
val -= data[i - k]
17+
18+
if i >= k - 1:
19+
answer = max(answer, val)
20+
21+
return k - answer
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 -1
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_21\
3+
.two_sum import Solution
4+
5+
class TwoSumTestCase(unittest.TestCase):
6+
7+
def test_is_two_sum(self):
8+
solution = Solution()
9+
output = solution.twoSum(nums=[2,7,11,15], target=9)
10+
target = [0,1]
11+
for k, v in enumerate(target):
12+
self.assertEqual(v, output[k])
13+
14+
def test_is_no_two_sum(self):
15+
solution = Solution()
16+
output = solution.twoSum(nums=[2,7,11,15], target=0)
17+
target = -1
18+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)