Skip to content
Merged
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
17 changes: 17 additions & 0 deletions container-with-most-water/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import List


class Solution:
def maxArea(self, height: List[int]) -> int:
left, right = 0, len(height) - 1
answer = 0

while left < right:
answer = max(answer, (right - left) * min(height[left], height[right]))

if height[left] < height[right]:
left += 1
else:
right -= 1

return answer
40 changes: 40 additions & 0 deletions design-add-and-search-words-data-structure/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class WordDictionary:

def __init__(self):
self.root = {"$": False}

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.get("$", False)

ch = word[idx]

if ch == ".":
for key in node:
if key == "$":
continue
if dfs(node[key], idx + 1):
return True
return False
else:
if ch in node:
return dfs(node[ch], idx + 1)
else:
return False

return dfs(self.root, 0)

# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)

27 changes: 27 additions & 0 deletions longest-increasing-subsequence/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import List
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
def binary_search(target):
Copy link
Contributor

Choose a reason for hiding this comment

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

오호 직접 바이너리 서치 구현을 하셔서 target 이 들어갈 위치를 리턴하는거군용 👍

left, right = 0, len(result)

while left < right:
mid = (left + right) // 2
if result[mid] < target:
left = mid + 1
else:
right = mid
return left

result = []

for num in nums:
idx = binary_search(num)
if idx == len(result):
result.append(num)
else:
result[idx] = num

return len(result)



28 changes: 28 additions & 0 deletions spiral-matrix/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import List


class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
dr = (0, 1, 0, -1)
dc = (1, 0, -1, 0)
r, c, d = 0, 0, 0

n, m = len(matrix), len(matrix[0])

result = []
VISITED = None
for _ in range(n * m):
result.append(matrix[r][c])
matrix[r][c] = VISITED

nr, nc = r + dr[d], c + dc[d]
Copy link
Contributor

Choose a reason for hiding this comment

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

그냥 사실 제 개인적인 생각인데ㅎㅎ;
저는 코드 작성할 때 변수명이 nr, nc, dr, dc 보다는 의미를 담는게 좋더라구요
뭔가 저렇게 축약이 되어있으면 코드 보면서 저게 무슨 의미였었지 헷갈릴때도 있어서ㅎㅎ 물론 개취이지만 한번 말씀드려보았습니다~~


if 0 <= nr < n and 0 <= nc < m and matrix[nr][nc] != VISITED:
r, c = nr, nc
continue

d = (d + 1) % 4
r += dr[d]
c += dc[d]

return result
15 changes: 15 additions & 0 deletions valid-parentheses/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def isValid(self, s: str) -> bool:
characters_dict = {')': '(', '}': '{', ']': '['}

stack = []
for ch in s:
if ch in characters_dict.values():
stack.append(ch)
elif ch in characters_dict:
if not stack or stack[-1] != characters_dict[ch]:
return False
stack.pop()

return not stack