diff --git a/climbing-stairs/JinuCheon.py b/climbing-stairs/JinuCheon.py new file mode 100644 index 0000000000..e8e6215e45 --- /dev/null +++ b/climbing-stairs/JinuCheon.py @@ -0,0 +1,32 @@ +# 재귀를 만드는 것이 잘 생각나지 않아서, 이렇게 저렇게 시도해보다 결국 LLM 도움. +# 그렇지만 제출 결과 Time Out. +# 2^N 복잡도임. max 45 의 경우 35,184,372,088,832. +class Solution: + def climbStairs(self, n: int) -> int: + # 1 -> 한칸만 가능 + # 2 -> (1,1) or (2) + if n <= 2: + return n; + + # 1칸 진행한 케이스 + 2칸 진행한 케이스 + return self.climbStairs(n-1) + self.climbStairs(n-2); + +# memonization 적용. 실무를 하고나서 이걸 보니, 캐싱이라고 부르고 싶다. +# 1~N 숫자 하나당 한번의 계산을 하게 되니, 시간복잡도는 O(1) 이다. +# 이게 왜 easy 난이도지? 잊어먹었다가 주말에 자력으로 풀어보자. +class Solution: + def climbStairs(self, n: int) -> int: + self.memo = {} + return self.dfs(n) + + def dfs(self, n: int) -> int: + if n <= 2: + return n + + # 이미 해당 수를 계산한 적이 있다면 early return. + if n in self.memo: + return self.memo[n] + + # 새로운 결과가 있으면 무조건 저장. + self.memo[n] = self.dfs(n - 1) + self.dfs(n - 2) + return self.memo[n] \ No newline at end of file diff --git a/valid-anagram/JinuCheon.py b/valid-anagram/JinuCheon.py new file mode 100644 index 0000000000..1966dd6b16 --- /dev/null +++ b/valid-anagram/JinuCheon.py @@ -0,0 +1,39 @@ +# my own solution +# O(n) +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + count1 = {} + for ch in s: + count1[ch] = count1.get(ch, 0) + 1 + + count2 = {} + for ch in t: + count2[ch] = count2.get(ch, 0) + 1 + + return count1 == count2 + +# gpt suggestion +### half space +#### O(n) +class Solution2: + def isAnagram(self, s: str, t: str) -> bool: + count = {} + for ch in s: + count[ch] = count.get(ch, 0) + 1 + for ch in t: + count[ch] = count.get(ch, 0) - 1 + return all(v == 0 for v in count.values()) + +### simple solution (What??) +#### O(n) +#### Counter works like my own solution internally. But I'm ganna forgot this, becuase I'm learning now. +from collections import Counter +class Solution3: + def isAnagram(self, s: str, t: str) -> bool: + return Counter(s) == Counter(t) + +### using sort +#### O(n log n) +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + return sorted(s) == sorted(t) \ No newline at end of file