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
19 changes: 19 additions & 0 deletions linked-list-cycle/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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:
fast = head
slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next

if fast == slow:
return True

return False

21 changes: 21 additions & 0 deletions maximum-product-subarray/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def maxProduct(self, nums: List[int]) -> int:
answer = max(nums)

cumprod = 1
cumprod_to_first_neg = 1

for num in nums:
if num == 0:
cumprod = 1
cumprod_to_first_neg = 1
else:
cumprod *= num
if cumprod > 0:
answer = max(answer, cumprod)
else:
answer = max(answer, cumprod//cumprod_to_first_neg)
if cumprod_to_first_neg > 0:
cumprod_to_first_neg *= num
return answer

34 changes: 34 additions & 0 deletions minimum-window-substring/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from collections import Counter, defaultdict
class Solution:
def minWindow(self, s: str, t: str) -> str:
answer = ''
counter = Counter(t)
now = defaultdict()

start = 0
end = 0

now[s[start]] = 1

while start<=end and end < len(s):
enough = True
for key in counter.keys():
Copy link
Contributor

Choose a reason for hiding this comment

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

해당 루프를 돌면서 O(n)만큼의 시간복잡도가 곱으로 추가될 것 같습니다.
문제에서 O(m+n)의 복잡도를 원하는데 지금 하고 계신 상태 업데이트와 카운트를 조금 더 고도화 해서 해결하면 좋을 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

좋은 조언 감사합니다!

if key not in now or now[key] < counter[key]:
enough = False

if enough:
if answer == '' or len(answer) > end-start+1:
answer = s[start:end+1]

now[s[start]] -= 1
start += 1
else:
end += 1
if end == len(s):
break
if s[end] not in now:
now[s[end]] = 0
now[s[end]] += 1

return answer

58 changes: 58 additions & 0 deletions pacific-atlantic-water-flow/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from collections import deque

class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
answer = []
h, w = len(heights), len(heights[0])
moves = [[-1, 0], [1, 0], [0, -1], [0, 1]]

visited1 = [[False for _ in range(w)] for _ in range(h)]
visited2 = [[False for _ in range(w)] for _ in range(h)]

q1 = deque()
q2 = deque()

for i in range(w):
q1.append((0, i))
q2.append((h-1, i))
visited1[0][i] = True
visited2[h-1][i] = True

for i in range(h):
q1.append((i, 0))
q2.append((i, w-1))
visited1[i][0] = True
visited2[i][w-1] = True

while q1:
y, x = q1.popleft()

for move in moves:
next_y, next_x = y+move[0], x+move[1]

if 0 <= next_y < h and 0 <= next_x < w:
if visited1[next_y][next_x]:
continue
if heights[next_y][next_x] >= heights[y][x]:
visited1[next_y][next_x] = True
q1.append((next_y, next_x))

while q2:
y, x = q2.popleft()

for move in moves:
next_y, next_x = y+move[0], x+move[1]

if 0 <= next_y < h and 0 <= next_x < w:
if visited2[next_y][next_x]:
continue
if heights[next_y][next_x] >= heights[y][x]:
visited2[next_y][next_x] = True
q2.append((next_y, next_x))

for i in range(h):
for j in range(w):
if visited1[i][j] and visited2[i][j]:
answer.append([i, j])
return answer

5 changes: 5 additions & 0 deletions sum-of-two-integers/8804who.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import math
class Solution:
def getSum(self, a: int, b: int) -> int:
return int(math.log(2**a * 2**b, 2))