diff --git a/climbing-stairs/Chanz82.py b/climbing-stairs/Chanz82.py new file mode 100644 index 0000000000..b82ed88989 --- /dev/null +++ b/climbing-stairs/Chanz82.py @@ -0,0 +1,23 @@ +class Solution: + + def __init__(self): + self.memo = dict() + + def climbStairs(self, n: int) -> int: + + # 공간복잡도 : n의 개수 만큼 memo가 할당되므로 o(n) + # 시간복잡도 : 각 칸에 대해서 계산이 1번씩만 되므로 o(n) + + # base + if n <= 2 : + return n + + # 이미 계산 한 값이라면 반환 + if n in self.memo: + return self.memo[n] + + # 1칸 전과 2칸 전의 결과를 합한 것을 반환. + result = self.climbStairs(n-2) + self.climbStairs(n-1) + self.memo[n] = result + + return result diff --git a/product-of-array-except-self/Chanz82.py b/product-of-array-except-self/Chanz82.py new file mode 100644 index 0000000000..ff54ece5bc --- /dev/null +++ b/product-of-array-except-self/Chanz82.py @@ -0,0 +1,16 @@ +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + + # 공간 복잡도 : 2개의 list를 사용하므로 O(2n)=O(n) + # 시간 복잡도 : nums를 세번 순회 o(n) + n = len(nums) + left = [1] * n + right = [1] * n + + for idx in range(1, n): + left[idx] = left[idx-1] * nums[idx-1] + + for idx in range(n-2, -1, -1): + right[idx] = right[idx+1] * nums[idx+1] + + return [left[i] * right[i] for i in range(n)] diff --git a/valid-anagram/Chanz82.py b/valid-anagram/Chanz82.py new file mode 100644 index 0000000000..af5f63c81f --- /dev/null +++ b/valid-anagram/Chanz82.py @@ -0,0 +1,19 @@ +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + hashmap = dict() + + # 공간복잡도 : hashmap을 사용하므로 공간복잡도는 O(N)입니다. + # 시간복잡도 : 문자열 s의 길이만큼 한번 순회하고, 다시 문자열 t의 길이만큼 순회하므로 O(s+t) => O(N)입니다. + for ch in s: + hashmap[ch] = hashmap.get(ch, 0) + 1 + + for ch in t: + if ch in hashmap: + hashmap[ch] -= 1 + if hashmap[ch] == 0: + del hashmap[ch] + else: + return False + + return not hashmap +