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
18 changes: 18 additions & 0 deletions linked-list-cycle/hu6r1s.py
Copy link
Contributor

Choose a reason for hiding this comment

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

Set을 활용하면 쉽게 풀리지만, ArrayList, Set, Map(Dict) 사용하지않고 풀이하려면 어떻게 하면 될까요 :)
공간 복잡도를 O(1) 으로 풀이 가능합니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
"""
생각보다 쉬운 문제.
"""
def hasCycle(self, head: Optional[ListNode]) -> bool:
visited = set()
while head:
if head in visited:
return True
visited.add(head)
head = head.next
return False
8 changes: 8 additions & 0 deletions maximum-product-subarray/hu6r1s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def maxProduct(self, nums: List[int]) -> int:
result = nums[0]
min_prod = max_prod = 1
for num in nums:
min_prod, max_prod = min(min_prod * num, max_prod * num, num), max(min_prod * num, max_prod * num, num)
result = max(max_prod, result)
return result
44 changes: 44 additions & 0 deletions minimum-window-substring/hu6r1s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution:
"""
접근 방법: https://www.youtube.com/watch?v=jSto0O4AJbM
- t의 문자 개수를 미리 세어둔 후(t_count),
s에서 두 포인터(left, right)를 이용해 슬라이딩 윈도우를 확장/축소하며
모든 조건을 만족하는 최소 구간을 찾는다.
- have: 현재 window에서 조건을 만족한 문자 종류 개수
- need: t에 있는 문자 종류 개수
- 조건을 만족할 때마다 왼쪽 포인터를 줄여 최소 길이 갱신

시간복잡도:
- 각 문자를 오른쪽 포인터로 한 번, 왼쪽 포인터로 한 번만 방문 → O(|s|)
- t의 해시맵 구성 O(|t|)
- 최종 시간복잡도 = O(|s| + |t|)

공간복잡도:
- window와 t_count 해시맵 → O(|s| + |t|)
"""
def minWindow(self, s: str, t: str) -> str:
t_count, window = {}, {}

for c in t:
t_count[c] = 1 + t_count.get(c, 0)

have, need = 0, len(t_count)
res, len_res = [-1, -1], float("infinity")
left = 0
for right in range(len(s)):
c = s[right]
window[c] = 1 + window.get(c, 0)

if c in t_count and window[c] == t_count[c]:
have += 1

while have == need:
if (right - left + 1) < len_res:
res = [left, right]
len_res = (right - left + 1)
window[s[left]] -= 1
if s[left] in t_count and window[s[left]] < t_count[s[left]]:
have -= 1
left += 1
left, right = res
return s[left:right+1] if len_res != float("infinity") else ""
33 changes: 33 additions & 0 deletions pacific-atlantic-water-flow/hu6r1s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution:
"""
https://www.youtube.com/watch?v=s-VkcjHqkGI
그래프 문제여서 바로 dfs나 bfs로 불면 될 것 같다는 생각이 들었는데 어떻게 해야할지 막막했다. 영상의 방식을 보고 공부
"""
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
rows, cols = len(heights), len(heights[0])
pac, atl = set(), set()

def dfs(r, c, visit, prevHeight):
if ((r, c) in visit or r < 0 or c < 0 or r == rows or c == cols or heights[r][c] < prevHeight):
return
visit.add((r, c))
dfs(r + 1, c, visit, heights[r][c])
dfs(r - 1, c, visit, heights[r][c])
dfs(r, c + 1, visit, heights[r][c])
dfs(r, c - 1, visit, heights[r][c])


for c in range(cols):
dfs(0, c, pac, heights[0][c])
dfs(rows - 1, c, atl, heights[rows-1][c])

for r in range(rows):
dfs(r, 0, pac, heights[r][0])
dfs(r, cols - 1, atl, heights[r][cols - 1])

result = []
for r in range(rows):
for c in range(cols):
if (r, c) in pac and (r, c) in atl:
result.append([r, c])
return results
6 changes: 6 additions & 0 deletions sum-of-two-integers/hu6r1s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution:
def getSum(self, a: int, b: int) -> int:
mask = 0xFFFFFFFF
while b & mask:
a, b = a ^ b, (a & b) << 1
return (a & mask) if b > 0 else a