From 38bf6a30849fc81eb530d516bf51c65238f730bb Mon Sep 17 00:00:00 2001 From: strNophix Date: Wed, 31 Aug 2022 17:50:53 +0200 Subject: [PATCH] Added leetcode 383 and 733 --- leetcode/383_ransom_note.py | 17 ++++++++++++++ leetcode/733_flood_fill.py | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 leetcode/383_ransom_note.py create mode 100644 leetcode/733_flood_fill.py diff --git a/leetcode/383_ransom_note.py b/leetcode/383_ransom_note.py new file mode 100644 index 0000000..6a04473 --- /dev/null +++ b/leetcode/383_ransom_note.py @@ -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 diff --git a/leetcode/733_flood_fill.py b/leetcode/733_flood_fill.py new file mode 100644 index 0000000..8fd1755 --- /dev/null +++ b/leetcode/733_flood_fill.py @@ -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