Skip to content

Commit dcb5207

Browse files
committed
adding algos
1 parent b061b88 commit dcb5207

File tree

7 files changed

+200
-15
lines changed

7 files changed

+200
-15
lines changed
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 []
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 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

src/my_project/interviews/amazon_high_frequency_23/round_3/number_of_islands.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,45 @@ def numIslands(self, grid: List[List[str]]) -> int:
1111
rows, cols = len(grid), len(grid[0])
1212
islands = 0
1313

14-
def dfs(r: int, c: int):
14+
def bfs(r: int, c: int):
1515
queue = deque([(r,c)])
16-
grid[r][c] = 0
16+
grid[r][c] = '0'
1717

18-
while queue:
18+
while queue:
1919
row, col = queue.popleft()
2020

21-
for dr, dc in [(-1,0), (1,0), (0,-1), (0,1)]:
21+
for dr, dc in [(1,0), (-1,0), (0,1), (0,-1)]:
2222
nr, nc = row + dr, col + dc
23-
if (0 <= nr < rows
24-
and 0 <= nc < cols
25-
and grid[nr][nc] == '1'):
23+
if (0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == '1'):
2624
grid[nr][nc] = '0'
2725
queue.append((nr,nc))
2826

2927
for r in range(rows):
3028
for c in range(cols):
3129
if grid[r][c] == '1':
3230
islands += 1
33-
dfs(r,c)
31+
bfs(r,c)
3432

3533
return islands
3634

3735

3836
def numIslands_DFS(grid: List[List[str]]) -> int:
3937
"""DFS approach - explores island depth-first"""
4038

41-
if not grid:
39+
if not grid:
4240
return 0
43-
41+
4442
rows, cols = len(grid), len(grid[0])
45-
4643
islands = 0
4744

4845
def dfs(r: int, c: int):
4946
if (r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == '0'):
5047
return
5148

52-
grid[r][c] = '0'
49+
grid[r][c] = '0'
5350

54-
dfs(r-1, c)
55-
dfs(r+1, c)
51+
dfs(r-1,c)
52+
dfs(r+1,c)
5653
dfs(r,c-1)
5754
dfs(r,c+1)
5855

@@ -64,4 +61,3 @@ def dfs(r: int, c: int):
6461

6562
return islands
6663

67-
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from collections import deque
3+
4+
class Solution:
5+
def numIslands(self, grid: List[List[str]]) -> int:
6+
"""BFS approach - explores island level by level"""
7+
8+
if not grid:
9+
return 0
10+
11+
rows, cols = len(grid), len(grid[0])
12+
islands = 0
13+
14+
def bfs(r: int, c: int):
15+
queue = deque([(r,c)])
16+
grid[r][c] = '0'
17+
18+
while queue:
19+
row, col = queue.popleft()
20+
21+
for dr, dc in [(1,0), (-1,0), (0,1), (0,-1)]:
22+
nr, nc = row + dr, col + dc
23+
if (0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == '1'):
24+
grid[nr][nc] = '0'
25+
queue.append((nr,nc))
26+
27+
for r in range(rows):
28+
for c in range(cols):
29+
if grid[r][c] == '1':
30+
islands += 1
31+
bfs(r,c)
32+
33+
return islands
34+
35+
36+
def numIslands_DFS(grid: List[List[str]]) -> int:
37+
"""DFS approach - explores island depth-first"""
38+
39+
if not grid:
40+
return 0
41+
42+
rows, cols = len(grid), len(grid[0])
43+
islands = 0
44+
45+
def dfs(r: int, c: int):
46+
if (r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == '0'):
47+
return
48+
49+
grid[r][c] = '0'
50+
51+
dfs(r-1,c)
52+
dfs(r+1,c)
53+
dfs(r,c-1)
54+
dfs(r,c+1)
55+
56+
for r in range(rows):
57+
for c in range(cols):
58+
if grid[r][c] == '1':
59+
islands += 1
60+
dfs(r,c)
61+
62+
return islands
63+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def numberOfWays(self, s: str) -> int:
6+
"""
7+
Count valid 3-building selections forming "010" or "101" patterns.
8+
9+
Key insight: Track how many 2-character patterns we can form,
10+
then extend them with the appropriate third character.
11+
12+
Time: O(n), Space: O(1)
13+
"""
14+
15+
count_0 = 0 # Count of '0' seen so far
16+
count_1 = 0 # Count of '1' seen so far
17+
count_01 = 0 # Count of "01" patterns (0 followed by 1)
18+
count_10 = 0 # Count of "10" patterns (1 followed by 0)
19+
20+
result = 0
21+
22+
for char in s:
23+
if char == '0':
24+
# Current '0' can complete "010": we need "01" before this '0'
25+
result += count_01
26+
27+
# Current '0' can start new "01" patterns: pair with each previous '0'
28+
# Wait no, "01" means 0 then 1, so we can't create "01" with another 0
29+
30+
# Current '0' extends all previous '1' to form "10" pattern
31+
count_10 += count_1
32+
33+
count_0 += 1
34+
else: # char == '1'
35+
# Current '1' can complete "101": we need "10" before this '1'
36+
result += count_10
37+
38+
# Current '1' extends all previous '0' to form "01" pattern
39+
count_01 += count_0
40+
41+
count_1 += 1
42+
43+
return result
44+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def myPow(self, x, n):
6+
7+
if not n:
8+
return 1
9+
if n < 1:
10+
return self.myPow(1/x, -n)
11+
if n % 2:
12+
return x*self.myPow(x,n-1)
13+
else:
14+
return self.myPow(x**2,n//2)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_21\
3+
.pow_x_n import Solution
4+
5+
class PowxnTestCase(unittest.TestCase):
6+
7+
def test_powxn_zero_power(self):
8+
solution = Solution()
9+
output = solution.myPow(x=2, n=0)
10+
target = 1
11+
self.assertEqual(output, target)
12+
13+
def test_powxn_negative_power(self):
14+
solution = Solution()
15+
output = solution.myPow(x=2, n=-1)
16+
target = 0.5
17+
self.assertEqual(output, target)
18+
19+
def test_powxn_odd_power(self):
20+
solution = Solution()
21+
output = solution.myPow(x=2, n=1)
22+
target = 2
23+
self.assertEqual(output, target)
24+
25+
def test_powxn_even_power(self):
26+
solution = Solution()
27+
output = solution.myPow(x=2, n=2)
28+
target = 4
29+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)