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
12 changes: 12 additions & 0 deletions climbing-stairs/deepInTheWoodz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# TC: O(1)
# SC: O(1)
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)인 것으로 보입니다!

class Solution:
def climbStairs(self, n: int) -> int:
dp = [0] * (n+2)
dp[1] = 1
dp[2] = 2

for i in range(3, n+2):
dp[i] = dp[i-1] + dp[i-2]
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[n]
29 changes: 29 additions & 0 deletions valid-anagram/deepInTheWoodz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# TC: O(n)
# SC: O(1)
def isAnagram(s: str, t: str) -> bool:
# TC: O(nlogn)
# SC: O(n)
# if sorted(list(s)) == sorted(list(t)):
# return True
# else:
# return False

chars = dict()

if len(s) != len(t):
return False

for c in s:
chars[c] = chars.get(c, 0) + 1

for c in t:
if c not in chars:
return False
chars[c] -= 1
if chars[c] == 0:
chars.pop(c)

return True

if __name__ == '__main__':
print(isAnagram('ab', 'a'))