Compare commits
8 Commits
65e8af6bb7
...
main
Author | SHA1 | Date | |
---|---|---|---|
3f5146d5a8 | |||
fb0684dd27 | |||
5e3a66aa45 | |||
749f7846a5 | |||
cf785e707d | |||
cc6a5e28b3 | |||
38bf6a3084 | |||
64cb9ea7ae |
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# algo-grind
|
||||
|
||||
Sometimes I'm too lazy to add the exercises to this repo. In that case you can take a look at my [LeetCode profile](https://leetcode.com/strNophix/).
|
24
leetcode/17_letter_combinations_of_a_phone_number.py
Normal file
24
leetcode/17_letter_combinations_of_a_phone_number.py
Normal file
@ -0,0 +1,24 @@
|
||||
from typing import List
|
||||
|
||||
|
||||
class Solution:
|
||||
def letterCombinations(self, digits: str) -> List[str]:
|
||||
if len(digits) == 0:
|
||||
return []
|
||||
|
||||
options = {
|
||||
"2": ("a", "b", "c"),
|
||||
"3": ("d", "e", "f"),
|
||||
"4": ("g", "h", "i"),
|
||||
"5": ("j", "k", "l"),
|
||||
"6": ("m", "n", "o"),
|
||||
"7": ("p", "q", "r", "s"),
|
||||
"8": ("t", "u", "v"),
|
||||
"9": ("w", "x", "y", "z"),
|
||||
}
|
||||
|
||||
pools = (options[digit] for digit in digits)
|
||||
result = [""]
|
||||
for pool in pools:
|
||||
result = [x + y for x in result for y in pool]
|
||||
return result
|
50
leetcode/36_valid_sudoku.py
Normal file
50
leetcode/36_valid_sudoku.py
Normal file
@ -0,0 +1,50 @@
|
||||
from typing import List
|
||||
import pytest
|
||||
|
||||
|
||||
class Solution:
|
||||
def isValidSudoku(self, board: List[List[str]]) -> bool:
|
||||
rows, cols, cubes = set(), set(), set()
|
||||
for i in range(9):
|
||||
for j in range(9):
|
||||
val = board[i][j]
|
||||
if not val.isdigit():
|
||||
continue
|
||||
|
||||
cube = (i // 3) * 3 + (j // 3)
|
||||
if (i, val) in rows or (j, val) in cols or (cube, val) in cubes:
|
||||
return False
|
||||
|
||||
rows.add((i, val))
|
||||
cols.add((j, val))
|
||||
cubes.add((cube, val))
|
||||
|
||||
return True
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def solution():
|
||||
return Solution()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"sudoku, expected",
|
||||
[
|
||||
(
|
||||
[
|
||||
["5", "3", ".", ".", "7", ".", ".", ".", "."],
|
||||
["6", ".", ".", "1", "9", "5", ".", ".", "."],
|
||||
[".", "9", "8", ".", ".", ".", ".", "6", "."],
|
||||
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
|
||||
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
|
||||
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
|
||||
[".", "6", ".", ".", ".", ".", "2", "8", "."],
|
||||
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
|
||||
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
|
||||
],
|
||||
True,
|
||||
)
|
||||
],
|
||||
)
|
||||
def test_search(solution: Solution, sudoku: List[List[str]], expected: bool):
|
||||
assert solution.isValidSudoku(sudoku) == expected
|
17
leetcode/383_ransom_note.py
Normal file
17
leetcode/383_ransom_note.py
Normal file
@ -0,0 +1,17 @@
|
||||
import pytest
|
||||
from collections import Counter
|
||||
|
||||
|
||||
class Solution:
|
||||
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
|
||||
return not Counter(ransomNote) - Counter(magazine)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def solution():
|
||||
return Solution()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("", [])
|
||||
def test_search(solution: Solution):
|
||||
pass
|
28
leetcode/695_max_area_of_island.py
Normal file
28
leetcode/695_max_area_of_island.py
Normal file
@ -0,0 +1,28 @@
|
||||
class Solution:
|
||||
def maxAreaOfIsland(self, grid: list[list[int]]) -> int:
|
||||
biggest_island = 0
|
||||
counter = 0
|
||||
|
||||
def dfs(row: int, col: int):
|
||||
nonlocal counter
|
||||
if not (0 <= row < len(grid)) or not (0 <= col < len(grid[0])):
|
||||
return
|
||||
|
||||
if grid[row][col] != 1:
|
||||
return
|
||||
|
||||
counter += 1
|
||||
grid[row][col] = 0
|
||||
dfs(row + 1, col)
|
||||
dfs(row - 1, col)
|
||||
dfs(row, col + 1)
|
||||
dfs(row, col - 1)
|
||||
|
||||
for row in range(len(grid)):
|
||||
for col in range(len(grid[0])):
|
||||
if grid[row][col] == 1:
|
||||
dfs(row, col)
|
||||
biggest_island = max(biggest_island, counter)
|
||||
counter = 0
|
||||
|
||||
return biggest_island
|
44
leetcode/733_flood_fill.py
Normal file
44
leetcode/733_flood_fill.py
Normal file
@ -0,0 +1,44 @@
|
||||
import pytest
|
||||
|
||||
|
||||
class Solution:
|
||||
def floodFill(
|
||||
self, image: list[list[int]], sr: int, sc: int, color: int
|
||||
) -> list[list[int]]:
|
||||
orig = image[sr][sc]
|
||||
|
||||
def dfs(i: int, j: int):
|
||||
if (
|
||||
0 <= i < len(image)
|
||||
and 0 <= j < len(image[0])
|
||||
and image[i][j] == orig
|
||||
and image[i][j] != color
|
||||
):
|
||||
image[i][j] = color
|
||||
dfs(i + 1, j)
|
||||
dfs(i - 1, j)
|
||||
dfs(i, j + 1)
|
||||
dfs(i, j - 1)
|
||||
|
||||
dfs(sr, sc)
|
||||
return image
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def solution():
|
||||
return Solution()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"image, sr, sc, color, expected",
|
||||
[([[1, 1, 1], [1, 1, 0], [1, 0, 1]], 1, 1, 2, [[2, 2, 2], [2, 2, 0], [2, 0, 1]])],
|
||||
)
|
||||
def test_search(
|
||||
solution: Solution,
|
||||
image: list[list[int]],
|
||||
sr: int,
|
||||
sc: int,
|
||||
color: int,
|
||||
expected: list[list[int]],
|
||||
):
|
||||
assert solution.floodFill(image, sr, sc, color) == expected
|
33
leetcode/74_search_a_2d_matrix.py
Normal file
33
leetcode/74_search_a_2d_matrix.py
Normal file
@ -0,0 +1,33 @@
|
||||
import pytest
|
||||
|
||||
|
||||
class Solution:
|
||||
def searchMatrix(self, matrix: list[list[int]], target: int) -> bool:
|
||||
rows, cols = len(matrix), len(matrix[0])
|
||||
left, right = 0, rows * cols - 1
|
||||
while left <= right:
|
||||
mid = left + (right - left) // 2
|
||||
|
||||
val = matrix[mid // cols][mid % cols]
|
||||
if val == target:
|
||||
return True
|
||||
elif val < target:
|
||||
left = mid + 1
|
||||
else:
|
||||
right = mid - 1
|
||||
return False
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def solution():
|
||||
return Solution()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"matrix, target, expected",
|
||||
[([[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]], 3, True)],
|
||||
)
|
||||
def test_search(
|
||||
solution: Solution, matrix: list[list[int]], target: int, expected: bool
|
||||
):
|
||||
assert solution.searchMatrix(matrix, target) == expected
|
@ -5,18 +5,20 @@ class Solution:
|
||||
def search(self, nums: list[int], target: int) -> bool:
|
||||
low, high = 0, len(nums) - 1
|
||||
while low <= high:
|
||||
breakpoint()
|
||||
mid = low + (high - low) // 2
|
||||
if nums[mid] == target:
|
||||
return True
|
||||
|
||||
while low < mid and nums[low] == nums[mid]:
|
||||
low += 1
|
||||
|
||||
if nums[mid] >= nums[low]:
|
||||
if nums[low] <= target <= nums[mid]:
|
||||
if nums[low] <= target < nums[mid]:
|
||||
high = mid - 1
|
||||
else:
|
||||
low = mid + 1
|
||||
else:
|
||||
if nums[mid] <= target <= nums[high]:
|
||||
if nums[mid] < target <= nums[high]:
|
||||
low = mid + 1
|
||||
else:
|
||||
high = mid - 1
|
||||
@ -32,8 +34,8 @@ def solution():
|
||||
@pytest.mark.parametrize(
|
||||
"nums, target, expected",
|
||||
[
|
||||
# ([2, 5, 6, 0, 0, 1, 2], 0, True),
|
||||
# ([2, 5, 6, 0, 0, 1, 2], 3, False),
|
||||
([2, 5, 6, 0, 0, 1, 2], 0, True),
|
||||
([2, 5, 6, 0, 0, 1, 2], 3, False),
|
||||
([1, 0, 1, 1, 1], 0, True),
|
||||
],
|
||||
)
|
||||
|
@ -1,3 +1,4 @@
|
||||
from typing import List
|
||||
import pytest
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user