-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #884 from hlee2052/main
- Loading branch information
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
21 changes: 21 additions & 0 deletions
21
...ek9 - backtracking/2023-11-12-1593-split_a_string_into_max_number_of_unique_substrings.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution: | ||
def maxUniqueSplit(self, s: str) -> int: | ||
|
||
def dfs(i): | ||
|
||
res = 0 | ||
|
||
if i >= len(s): | ||
return 0 | ||
|
||
for j in range(i + 1, len(s) + 1): | ||
tmp = s[i:j] | ||
if tmp not in visited: | ||
visited.add(tmp) | ||
res = max(res, 1 + dfs(j)) | ||
visited.remove(tmp) | ||
|
||
return res | ||
|
||
visited = set() | ||
return dfs(0) |
20 changes: 20 additions & 0 deletions
20
HyunWooLee/week9 - backtracking/2023-11-12-77_Combinations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution: | ||
|
||
def combine(self, n: int, k: int) -> List[List[int]]: | ||
|
||
def dfs(start, arr): | ||
if start > n + 1: | ||
return | ||
|
||
if len(arr) == k: | ||
result.append(arr[:]) | ||
return | ||
|
||
for i in range(start, n + 1): | ||
arr.append(i) | ||
dfs(i + 1, arr) | ||
arr.pop() | ||
|
||
result = [] | ||
dfs(1, []) | ||
return result |
39 changes: 39 additions & 0 deletions
39
HyunWooLee/week9 - backtracking/2023-11-12-79_word_search.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class Solution(object): | ||
def exist(self, board, word): | ||
""" | ||
:type board: List[List[str]] | ||
:type word: str | ||
:rtype: bool | ||
""" | ||
|
||
def dfs(row, col, curr_len): | ||
if row < 0 or len(board) <= row or col < 0 or len(board[0]) <= col: | ||
return False | ||
|
||
curr_word = word[len(word) - curr_len] | ||
|
||
if board[row][col] != curr_word: | ||
return False | ||
|
||
if curr_len == 1: | ||
return True | ||
|
||
prev = board[row][col] | ||
|
||
board[row][col] = None | ||
|
||
top = dfs(row - 1, col, curr_len - 1) | ||
down = dfs(row + 1, col, curr_len - 1) | ||
left = dfs(row, col - 1, curr_len - 1) | ||
right = dfs(row, col + 1, curr_len - 1) | ||
|
||
board[row][col] = prev | ||
|
||
return top or down or left or right | ||
|
||
for i in range(len(board)): | ||
for j in range(len(board[0])): | ||
if dfs(i, j, len(word)): | ||
return True | ||
|
||
return False |