Skip to content

Commit

Permalink
solve 3
Browse files Browse the repository at this point in the history
  • Loading branch information
pmjuu committed Dec 31, 2024
1 parent f9cabb5 commit a353a02
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
33 changes: 33 additions & 0 deletions merge-two-sorted-lists/pmjuu.py
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)
13 changes: 13 additions & 0 deletions missing-number/pmjuu.py
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)
106 changes: 106 additions & 0 deletions word-search/pmjuu.py
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)

0 comments on commit a353a02

Please sign in to comment.