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
26 changes: 26 additions & 0 deletions container-with-most-water/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'''
이중 For-loop을 사용하면 시간복잡도가 O(n^2)이 되므로 시간초과가 발생
리트코드의 Hint를 참고해 Two Pointer를 사용하는 방식으로 시도했습니다.

Time Complexity: O(n)
- While 루프를 사용해서 인덱스 i와 j가 만날 때까지 반복으로 진행하기에 O(n)

Space Complexity: O(1)
- height 값과, max_area 값 모두 상수 공간을 사용
'''
class Solution:
def maxArea(self, height: List[int]) -> int:
i, j = 0, len(height) - 1
max_area = 0

while i < j:
area = min(height[i], height[j]) * (j-i)
max_area = max(area, max_area)

if height[i] < height[j]:
i += 1
else:
j -= 1

return max_area

28 changes: 28 additions & 0 deletions design-add-and-search-words-data-structure/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'''
이번 문제는 Trie 자료구조를 활용한 문제였습니다.
이를 파악하지 못해서 전체 탐색하는 구조를 만들었다 결국 또 도움을 받아 이해하는 것에 목표를 뒀습니다.
'''
class WordDictionary:
def __init__(self):
self.root = {"$": True}

def addWord(self, word: str) -> None:
node = self.root
for ch in word:
if ch not in node:
node[ch] = {"$": False}
node = node[ch]
node["$"] = True

def search(self, word: str) -> bool:
def dfs(node, idx):
if idx == len(word):
return node["$"]
ch = word[idx]
if ch in node:
return dfs(node[ch], idx + 1)
if ch == ".":
return any(dfs(node[k], idx + 1) for k in node if k != "$")
return False
return dfs(self.root, 0)

12 changes: 12 additions & 0 deletions longest-increasing-subsequence/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'''
Dynamic Programming 문제로 누적합을 이용해 풀어보려 했으나 끝내 해결을 못해 참고했습니다.
'''
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
dp = [1] * len(nums)
for cur in range(1, len(nums)):
for prev in range(len(nums)):
if nums[prev] < nums[cur]:
dp[cur] = max(1+dp[prev], dp[cur])
return max(dp)

38 changes: 38 additions & 0 deletions spiral-matrix/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'''
해당 문제를 확인하고
상단 -> 우측 -> 하단 -> 좌측 방향으로 나선형으로 접근하는 것을 우선순위로 생각했습니다.
pop을 통해서 matrix의 행과 열을 제거하면서 접근하는 방식을 사용해 해결했습니다.

이렇게 문제를 풀었을때 pop을 사용하여 행과 열을 제거하기 때문에
비싼 계산 비용이 발생하는 부분을 고려하지 못했다는 점이 아쉽게 느껴지네요.

Time Complexity: O(mxn)
- m: 행의 수만큼 접근
- n: 열의 수만큼 접근

Space Complexity: O(mxn)
- result 리스트가 전체 요소를 저장
'''
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
result = []
idx = 0 # 처음 값
while matrix:
result += matrix.pop(idx)
for i in range(len(matrix)):
if matrix[i]:
result.append(matrix[i].pop())

# 빈 행 제거 (중요!)
matrix = [row for row in matrix if row]
Copy link
Member

Choose a reason for hiding this comment

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

여기서 왜 빈행이 왜 생기고 왜 제거를 제거해줘야 하는지 여쭤보고싶습니다

Copy link
Contributor Author

Choose a reason for hiding this comment

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

테스트하는 과정에서 maxtirx가 [[],[]] 이런식으로 이중 빈배열인 경우가 발생하는데요,
시스템에서 내부의 빈배열들이 모두 비어 있으면 반복문을 탈출 하도록 의도하며 작성했는데, 정작 내부 배열은 처리하지 못하더라고요.
그래서 에러가 발생해 중간에 해당 로직을 추가해뒀습니다.

if not matrix:
break

result += matrix.pop()[::-1]

for j in range(len(matrix)-1, -1, -1):
if matrix[j]:
result.append(matrix[j].pop(0))

return result

18 changes: 18 additions & 0 deletions valid-parentheses/Seoya0512.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'''
20. Valid Parentheses
Stack을 사용하는 문제임을 파악하지 못해서 결국 알고달레님의 도움을 받았습니다.

Time Complexity: O(n)
Space Complexity: O(n)
'''
class Solution:
def isValid(self, s: str) -> bool:
parens = {"(": ")", "{": "}", "[": "]"}
stack = []
for val in s:
if val in parens:
stack.append(val)
else:
if not stack or val != parens[stack.pop()]:
return False
return not stack
Copy link
Member

Choose a reason for hiding this comment

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

저는 마지막에 return true 라고만 했더니 실패케이스들이 발생하여 (s len == 1 or "((" 따로 조건문을 추가해주었었습니다.
달레님 풀이만 참고하셨으면 엣지 케이스를 놓치셨을까봐 남겨드립니다 !