Skip to content

Commit 024435e

Browse files
authored
Merge pull request #1556 from ivanpenaloza/february22
adding algo
2 parents cc1e74e + f92548b commit 024435e

5 files changed

Lines changed: 211 additions & 0 deletions

File tree

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, 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+
18+
19+
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+
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+
len_s = len(s)
16+
17+
for i in range(len_s//2):
18+
19+
if s[i] != s[len_s - 1 - i]:
20+
return False
21+
22+
return True
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
from collections import deque
4+
5+
class Solution:
6+
def numIslands(self, grid: List[List[str]]) -> int:
7+
"""BFS approach - explores island level by level"""
8+
9+
if not grid:
10+
return 0
11+
12+
rows, cols = len(grid), len(grid[0])
13+
islands = 0
14+
15+
def bfs(r: int, c: int):
16+
queue = deque([(r,c)])
17+
grid[r][c] = '0'
18+
19+
while queue:
20+
row, col = queue.popleft()
21+
22+
for dr, dc in [(1,0), (-1,0), (0,1), (0,-1)]:
23+
nr, nc = row + dr, col + dc
24+
if (0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == '1'):
25+
grid[nr][nc] = '0'
26+
queue.append((nr,nc))
27+
28+
for r in range(rows):
29+
for c in range(cols):
30+
if grid[r][c] == '1':
31+
islands += 1
32+
bfs(r,c)
33+
34+
return islands
35+
36+
37+
def numIslands_DFS(self, grid: List[List[str]]) -> int:
38+
"""DFS approach - explores island depth-first"""
39+
40+
if not grid:
41+
return 0
42+
43+
rows, cols = len(grid), len(grid[0])
44+
islands = 0
45+
46+
def dfs(r: int, c: int):
47+
if (r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == '0'):
48+
return
49+
50+
grid[r][c] = '0'
51+
52+
dfs(r-1,c)
53+
dfs(r+1,c)
54+
dfs(r,c-1)
55+
dfs(r,c+1)
56+
57+
for r in range(rows):
58+
for c in range(cols):
59+
if grid[r][c] == '1':
60+
islands += 1
61+
dfs(r,c)
62+
63+
return islands
64+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
let m: number, n: number
2+
function numIslands(grid: string[][]): number {
3+
let count: number = 0
4+
if (!grid || !grid[0]) return count
5+
m = grid.length
6+
n = grid[0].length
7+
for (let i = 0; i < m; i++) {
8+
for (let j = 0; j < n; j++) {
9+
if (grid[i][j] === "1") {
10+
count++
11+
dfs(grid, i, j)
12+
}
13+
}
14+
}
15+
return count
16+
};
17+
18+
function dfs(grid: string[][], i: number, j: number) {
19+
if (i >= m || j >= n || i < 0 || j < 0 || grid[i][j] === '0') return
20+
grid[i][j] = '0'
21+
dfs(grid, i + 1, j)
22+
dfs(grid, i - 1, j)
23+
dfs(grid, i, j - 1)
24+
dfs(grid, i, j + 1)
25+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_84_number_of_islands import Solution
4+
5+
class NumberOfIslandsTescaseCase(unittest.TestCase):
6+
7+
def test_example_1(self):
8+
# Example 1: Single island
9+
# Input: grid = [
10+
# ["1","1","1","1","0"],
11+
# ["1","1","0","1","0"],
12+
# ["1","1","0","0","0"],
13+
# ["0","0","0","0","0"]
14+
# ]
15+
# Output: 1
16+
solution = Solution()
17+
grid = [
18+
["1", "1", "1", "1", "0"],
19+
["1", "1", "0", "1", "0"],
20+
["1", "1", "0", "0", "0"],
21+
["0", "0", "0", "0", "0"]
22+
]
23+
result = solution.numIslands(grid)
24+
self.assertEqual(result, 1)
25+
26+
def test_example_2(self):
27+
# Example 2: Three islands
28+
# Input: grid = [
29+
# ["1","1","0","0","0"],
30+
# ["1","1","0","0","0"],
31+
# ["0","0","1","0","0"],
32+
# ["0","0","0","1","1"]
33+
# ]
34+
# Output: 3
35+
solution = Solution()
36+
grid = [
37+
["1", "1", "0", "0", "0"],
38+
["1", "1", "0", "0", "0"],
39+
["0", "0", "1", "0", "0"],
40+
["0", "0", "0", "1", "1"]
41+
]
42+
result = solution.numIslands(grid)
43+
self.assertEqual(result, 3)
44+
45+
def test_example_1_dfs(self):
46+
# Example 1: Single island (DFS approach)
47+
# Input: grid = [
48+
# ["1","1","1","1","0"],
49+
# ["1","1","0","1","0"],
50+
# ["1","1","0","0","0"],
51+
# ["0","0","0","0","0"]
52+
# ]
53+
# Output: 1
54+
solution = Solution()
55+
grid = [
56+
["1", "1", "1", "1", "0"],
57+
["1", "1", "0", "1", "0"],
58+
["1", "1", "0", "0", "0"],
59+
["0", "0", "0", "0", "0"]
60+
]
61+
result = solution.numIslands_DFS(grid)
62+
self.assertEqual(result, 1)
63+
64+
def test_example_2_dfs(self):
65+
# Example 2: Three islands (DFS approach)
66+
# Input: grid = [
67+
# ["1","1","0","0","0"],
68+
# ["1","1","0","0","0"],
69+
# ["0","0","1","0","0"],
70+
# ["0","0","0","1","1"]
71+
# ]
72+
# Output: 3
73+
solution = Solution()
74+
grid = [
75+
["1", "1", "0", "0", "0"],
76+
["1", "1", "0", "0", "0"],
77+
["0", "0", "1", "0", "0"],
78+
["0", "0", "0", "1", "1"]
79+
]
80+
result = solution.numIslands_DFS(grid)
81+
self.assertEqual(result, 3)

0 commit comments

Comments
 (0)