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
35 changes: 35 additions & 0 deletions 3sum/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
# ans = set()
# for i in range(len(n)):
# seen = {}
# for j in range(i+1, len(n)):
# complement = -(nums[i] + nums[j])
# if complement in seen:
# ans.add(tuple(sorted([nums[i], nums[j], complement])))
# seen[nums[j]] = j
# return [list(triplet) for triplet in ans]
# hash + 이중 loop -> O(n^2)


## 투포인터 활용
# sort + loop -> O(nlogn)
Copy link
Contributor

Choose a reason for hiding this comment

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

sort + 이중 loop이므로 시간 복잡도가 O(n^2)이 될 것 같습니다!

ans_set = set()
nums.sort()
for i in range(len(nums)):
l = i + 1
r = len(nums) - 1
while l < r:
total = nums[i] + nums[l] + nums[r]
if total < 0:
l += 1
elif total > 0:
r -= 1
else:
# 중복제거
ans_set.add((nums[i], nums[l], nums[r]))
Copy link
Contributor

Choose a reason for hiding this comment

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

set을 활용해 중복 제거를 하셨네요! 저는 포인터 기반으로 루프 내에서 중복을 제거하도록 풀어봤었는데, 이렇게 하면 set을 유지하기 위한 추가적인 공간 복잡도를 최적화 할 수 있더라구요! 한 번 포인터 기반으로도 풀어보시는 걸 추천드립니다~~ 🤩

l += 1
r -= 1
return list(ans_set)

Copy link
Contributor

Choose a reason for hiding this comment

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

파일 마지막에 빈 개행이 한 줄만 들어가도록 수정 부탁드립니다~!


12 changes: 12 additions & 0 deletions climbing-stairs/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def climbStairs(self, n: int) -> int:
if n <= 1:
return 1
dp = [0] * n
print(dp)
dp[0] = 1
dp[1] = 2

for i in range(2, n):
dp[i] = dp[i-2] + dp[i-1]
Copy link
Contributor

Choose a reason for hiding this comment

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

이렇게 DP 테이블의 가장 최근 n개의 원소만 사용하는 경우에는 O(n) space의 DP 테이블 대신 O(1)space의 변수를 사용해서 공간 복잡도를 한 단계 최적화 할 수 있을 것 같아요~!

return dp[-1]
22 changes: 22 additions & 0 deletions product-of-array-except-self/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
# ans = [1] * len(nums)
# for i in range(len(nums)):
# for j in range(len(nums)):
# if i != j:
# ans[i] = ans[i] * nums[j]
# return ans
# 시간복잡도를 줄이기 위해서 이중루프를 안쓰는 방법으로
left = [1] * len(nums)
right = [1] * len(nums)
for i in range(len(nums) - 1):
left[i+1] = left[i] * nums[i]
print(left)
for j in range(len(nums) - 1, 0, -1):
right[j-1] = right[j] * nums[j]
print(right)

answer = []
for k in range(len(left)):
answer.append(left[k]*right[k])
return answer
Copy link
Contributor

Choose a reason for hiding this comment

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

merge를 위해 파일 마지막에 빈 개행 추가 부탁드립니다!

26 changes: 26 additions & 0 deletions valid-anagram/changhyumm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
# sorted_s = sorted(s)
# sorted_t = sorted(t)
# for i in range(len(s)):
# if sorted_s[i] != sorted_t[i]:
# return False
# return True
# 시간복잡도가 O(nlog(n))

# Hashmap 사용하는 방법으로 변경
counter = {}
for char in s:
counter[char] = counter.get(char, 0) + 1
for char in t:
if char in counter and counter[char] != 0:
counter[char] -= 1
elif char in counter and counter[char] == 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

if-elif 문에서 char in counter 확인 로직이 중복되므로 이를 분리한다면 코드가 조금 더 단순해질 것 같습니다!

return False
else:
return False
# 시간복잡도 O(n)
# 공간복잡도 O(n)
Copy link
Contributor

Choose a reason for hiding this comment

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

저도 다시 풀어보면서 알게 된 사실인데요, 이 문제에서는 입력값이 모두 영어 소문자로만 구성되어 있으므로 공간 복잡도가 O(26) = O(1)이 된다고 합니다!

return True
Loading