Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions container-with-most-water/Chanz82.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers, Greedy
  • 설명: 높은 벽돌 양쪽에서 시작해 더 작은 쪽으로 포인터를 이동시키며 최댓값을 갱신하는 방식으로 문제를 풀이하므로 Two Pointers와 Greedy 패턴이 모두 적용됩니다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 4가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: Solution.maxArea — Time: O(n) / Space: O(1)
복잡도
Time O(n)
Space O(1)

피드백: 양 끝 포인터를 좁혀가며 현재 넓이와 남은 영역의 가능성을 비교하여 최댓값을 갱신합니다.

개선 제안: 현재 구현이 적절해 보입니다.

풀이 2: WordDictionary.addWord — Time: O(len(word)) / Space: O(total_chars_stored)
복잡도
Time O(len(word))
Space O(total_chars_stored)

피드백: 각 글자마다 노드를 생성하고 끝에 '#'를 두어 단어 종료를 표시합니다.

개선 제안: 현재 구현이 적절해 보입니다.

풀이 3: WordDictionary.search — Time: O(branching_factor^length) / Space: O(length_of_word_depth)
복잡도
Time O(branching_factor^length)
Space O(length_of_word_depth)

피드백: 점 와일드카드를 처리하기 위해 분기 탐색을 재귀적으로 수행합니다.

개선 제안: 현재 구현이 적절해 보입니다.

풀이 4: Solution.isValid — Time: O(n) / Space: O(n)
복잡도
Time O(n)
Space O(n)

피드백: 닫는 괄호를 만났을 때 스택이 비어있거나 매칭되지 않으면 실패하고, 끝난 후 스택이 비어있어야 유효합니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def maxArea(self, height: List[int]) -> int:
# 두 점의 거리가 길어야 하고, 높은 막대기들끼리 있어야 많은 물을 채울 수 있음.
# 양쪽의 점부터 시작해서 더 줄여볼지 말지를 판단 해 볼 수 있을 것 같음.

def caculate_water_container(left, right):
return (right - left) * min(height[left], height[right])

left = 0
right = len(height) - 1
max_water = 0

while left <= right:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left == right인 경우는 필요 없어서 while left < right로 해줘도 될 거 같습니다.

max_water = max(max_water, caculate_water_container(left, right))
if height[left] > height[right] :
right -= 1
else :
left += 1

return max_water

40 changes: 40 additions & 0 deletions design-add-and-search-words-data-structure/Chanz82.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Trie, Hash Map / Hash Set, Backtracking
  • 설명: 단어 트리(Trie) 구조를 사용해 단어를 저장하고, 점(.) 와일드카드를 재귀적으로 탐색하는 방식으로 구현되어 있습니다. 해시 맵으로 트리의 간선을 표현하고, 부분 검색 시 백트래킹으로 가능한 경로를 모두 시도합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class WordDictionary:

def __init__(self):
self.word_tree = {}

def addWord(self, word: str) -> None:
curr_branch = self.word_tree
for ch in word:
if not curr_branch.get(ch, None):
curr_branch[ch] = {}
curr_branch = curr_branch[ch]
curr_branch["#"] = {} # end of word

def search(self, word: str) -> bool:
curr_branch = self.word_tree

def nested_search(start_branch: dict, word: str) -> bool:

for idx, ch in enumerate(word):
if ch in start_branch:
start_branch = start_branch[ch]
elif ch == ".":
for branch in start_branch.values():
if nested_search(branch, word[idx+1:]):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slicing을 쓰면 O(n)이라 slicing을 안 쓰도록 풀어보셔도 좋을 거 같아요.

return True
return False
else:
return False

if "#" in start_branch:
return True

return False

return nested_search(curr_branch, word)

# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)
20 changes: 20 additions & 0 deletions valid-parentheses/Chanz82.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Stack
  • 설명: 주어진 코드는 스택을 사용해 여는 괄호를 push하고 닫는 괄호를 팝하며 매칭 여부를 확인한다. 한 번에 한 문자씩 순회하며 유효한 괄호 쌍인지 검증하므로 스택 기반의 패턴에 해당한다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def isValid(self, s: str) -> bool:
ch_stack = []
ch_map = {}

ch_map[")"] = "("
ch_map["}"] = "{"
ch_map["]"] = "["
Comment on lines +4 to +8

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

선언 시점에 할당하면 좀 더 간결해 질 거 같네요.

Suggested change
ch_map = {}
ch_map[")"] = "("
ch_map["}"] = "{"
ch_map["]"] = "["
ch_map = {
")": "(",
"}": "{",
"]": "["
}


for ch in s:
if ch == ")" or ch == "}" or ch == "]":
if not ch_stack:
return False
if ch_stack.pop() != ch_map[ch]:
return False
else:
ch_stack.append(ch)

return not ch_stack

Loading