-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
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,33 @@ | ||
from typing import Optional | ||
|
||
|
||
class ListNode: | ||
def __init__(self, val=0, next=None): | ||
self.val = val | ||
self.next = next | ||
|
||
class Solution: | ||
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: | ||
head = ListNode() | ||
current = head | ||
|
||
while list1 and list2: | ||
if list1.val <= list2.val: | ||
current.next = list1 | ||
list1 = list1.next | ||
else: | ||
current.next = list2 | ||
list2 = list2.next | ||
|
||
current = current.next | ||
|
||
current.next = list1 or list2 | ||
|
||
return head.next | ||
|
||
# ์๊ฐ ๋ณต์ก๋: | ||
# - ๋ ๋ฆฌ์คํธ์ ๋ชจ๋ ๋ ธ๋๋ฅผ ์ํํ๋ฉฐ ๋ณํฉํ๋ฏ๋ก O(n + m) => O(n) ์ผ๋ก ํํ | ||
# ์ฌ๊ธฐ์ n์ list1์ ๊ธธ์ด, m์ list2์ ๊ธธ์ด. | ||
# | ||
# ๊ณต๊ฐ ๋ณต์ก๋: | ||
# - ๊ธฐ์กด ๋ ธ๋๋ฅผ ์ฌ์ฌ์ฉํ๋ฏ๋ก O(1) |
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,13 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def missingNumber(self, nums: List[int]) -> int: | ||
n = len(nums) | ||
# 0๋ถํฐ n๊น์ง์ ์ซ์์ ํฉ์ ์ํ์ ํฉ ๊ณต์์ ์ฌ์ฉํด ๊ณ์ฐ | ||
total_sum = n * (n + 1) // 2 | ||
|
||
return total_sum - sum(nums) | ||
|
||
# ์๊ฐ ๋ณต์ก๋ O(n) | ||
# ๊ณต๊ฐ ๋ณต์ก๋ O(1) |
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,106 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def exist(self, board: List[List[str]], word: str) -> bool: | ||
n, m, word_length = len(board), len(board[0]), len(word) | ||
|
||
def search(row, col, word_idx, visited): | ||
# ๊ฒฝ๊ณ ์ฒดํฌ | ||
if not (0 <= row < n and 0 <= col < m): | ||
return False | ||
# ์ด๋ฏธ ๋ฐฉ๋ฌธํ๊ฑฐ๋, ๋ฌธ์๊ฐ ์ผ์นํ์ง ์๋ ๊ฒฝ์ฐ | ||
if (row, col) in visited or board[row][col] != word[word_idx]: | ||
return False | ||
|
||
# ๋ชจ๋ ๋ฌธ์๋ฅผ ์ฐพ์ ๊ฒฝ์ฐ | ||
if word_idx == word_length - 1: | ||
return True | ||
|
||
# ํ์ฌ ์ ์ ๋ฐฉ๋ฌธํ ๊ฒ์ผ๋ก ํ์ | ||
visited.add((row, col)) | ||
|
||
# ์ธ์ ํ ์ ํ์ธ | ||
found = ( | ||
search(row - 1, col, word_idx + 1, visited) or | ||
search(row + 1, col, word_idx + 1, visited) or | ||
search(row, col - 1, word_idx + 1, visited) or | ||
search(row, col + 1, word_idx + 1, visited) | ||
) | ||
# ํ์ฌ ์ ๋ฐฉ๋ฌธ ํด์ (๋ฐฑํธ๋ํน) | ||
visited.remove((row, col)) | ||
|
||
return found | ||
|
||
# ๋ชจ๋ ์ ์์ ํ์ ์์ | ||
for row in range(n): | ||
for col in range(m): | ||
if board[row][col] == word[0]: | ||
if search(row, col, 0, set()): | ||
return True | ||
|
||
return False | ||
|
||
# ํ์ด 1: ๋ฐฉ๋ฌธ ๊ธฐ๋ก์ Set์ผ๋ก ๊ด๋ฆฌํ๋ ๋ฐฉ์ | ||
# ์๊ฐ ๋ณต์ก๋: | ||
# - ๊ฐ ์ ์์ DFS๋ฅผ ์์ํ๋ฉฐ, ๊ฐ DFS๋ ์ต๋ ๋ค ๋ฐฉํฅ์ผ๋ก ์ด๋ํ๋ฉฐ word์ ๊ธธ์ด๋งํผ ์ฌ๊ท ํธ์ถ์ ์งํํจ. | ||
# - ์ต์ ์ ๊ฒฝ์ฐ O(n * 4^k), ์ฌ๊ธฐ์ n์ ์ ์ฒด ์ ์ ๊ฐ์, k๋ word์ ๊ธธ์ด. | ||
# ๊ณต๊ฐ ๋ณต์ก๋: | ||
# - visited Set ์ฌ์ฉ: O(k), ์ฌ๊ธฐ์ k๋ word์ ๊ธธ์ด. | ||
# - ์ฌ๊ท ํธ์ถ ์คํ: O(k), word์ ๊ธธ์ด๋งํผ ์ฌ๊ท ํธ์ถ์ด ์์. | ||
# => ์ด ๊ณต๊ฐ ๋ณต์ก๋: O(k) | ||
|
||
|
||
class Solution: | ||
def exist(self, board: list[list[str]], word: str) -> bool: | ||
n, m = len(board), len(board[0]) | ||
word_length = len(word) | ||
|
||
# ์กฐ๊ธฐ ์ข ๋ฃ: board์ word๋ฅผ ๊ตฌ์ฑํ ์ถฉ๋ถํ ๋ฌธ์๊ฐ ์๋์ง ํ์ธ | ||
from collections import Counter | ||
board_counter = Counter(char for row in board for char in row) | ||
word_counter = Counter(word) | ||
if any(word_counter[char] > board_counter[char] for char in word_counter): | ||
return False | ||
|
||
def search(row, col, idx): | ||
# ๊ธฐ๋ณธ ์กฐ๊ฑด: ๋ชจ๋ ๋ฌธ์๊ฐ ์ผ์นํ ๊ฒฝ์ฐ | ||
if idx == word_length: | ||
return True | ||
|
||
# ๊ฒฝ๊ณ ์กฐ๊ฑด ๋ฐ ๋ฌธ์ ์ผ์น ์ฌ๋ถ ํ์ธ | ||
if row < 0 or row >= n or col < 0 or col >= m or board[row][col] != word[idx]: | ||
return False | ||
|
||
# ํ์ฌ ์ ์ ๋ฐฉ๋ฌธํ ๊ฒ์ผ๋ก ์์ ํ์ | ||
temp = board[row][col] | ||
board[row][col] = "#" | ||
|
||
# ๋ชจ๋ ๋ฐฉํฅ ํ์ | ||
found = ( | ||
search(row - 1, col, idx + 1) or | ||
search(row + 1, col, idx + 1) or | ||
search(row, col - 1, idx + 1) or | ||
search(row, col + 1, idx + 1) | ||
) | ||
|
||
# ํ์ ํ ์ ๋ณต์ | ||
board[row][col] = temp | ||
return found | ||
|
||
# ์ฒซ ๋ฒ์งธ ๋ฌธ์์ ์ผ์นํ๋ ๋ชจ๋ ์ ์์ DFS ์์ | ||
for i in range(n): | ||
for j in range(m): | ||
if board[i][j] == word[0] and search(i, j, 0): | ||
return True | ||
|
||
return False | ||
|
||
# ํ์ด 2: Board๋ฅผ ์ง์ ์์ ํด ๋ฐฉ๋ฌธ ๊ธฐ๋ก ๊ด๋ฆฌ | ||
# ์๊ฐ ๋ณต์ก๋: | ||
# - ๊ฐ ์ ์์ DFS๋ฅผ ์์ํ๋ฉฐ, ์ต๋ ๋ค ๋ฐฉํฅ์ผ๋ก ์ด๋ํ๋ฉฐ word์ ๊ธธ์ด๋งํผ ์ฌ๊ท ํธ์ถ์ ์งํํจ. | ||
# - ์ต์ ์ ๊ฒฝ์ฐ O(n * 4^k), ์ฌ๊ธฐ์ n์ ์ ์ฒด ์ ์ ๊ฐ์, k๋ word์ ๊ธธ์ด. | ||
# ๊ณต๊ฐ ๋ณต์ก๋: | ||
# - ์ถ๊ฐ ๊ณต๊ฐ ์ฌ์ฉ ์์ด Board๋ฅผ ์ง์ ์์ : O(1). | ||
# - ์ฌ๊ท ํธ์ถ ์คํ: O(k), word์ ๊ธธ์ด๋งํผ ์ฌ๊ท ํธ์ถ์ด ์์. | ||
# => ์ด ๊ณต๊ฐ ๋ณต์ก๋: O(k) |