File tree Expand file tree Collapse file tree
product-of-array-except-self Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ class Solution :
2+
3+ def __init__ (self ):
4+ self .memo = dict ()
5+
6+ def climbStairs (self , n : int ) -> int :
7+
8+ # ๊ณต๊ฐ๋ณต์ก๋ : n์ ๊ฐ์ ๋งํผ memo๊ฐ ํ ๋น๋๋ฏ๋ก o(n)
9+ # ์๊ฐ๋ณต์ก๋ : ๊ฐ ์นธ์ ๋ํด์ ๊ณ์ฐ์ด 1๋ฒ์ฉ๋ง ๋๋ฏ๋ก o(n)
10+
11+ # base
12+ if n <= 2 :
13+ return n
14+
15+ # ์ด๋ฏธ ๊ณ์ฐ ํ ๊ฐ์ด๋ผ๋ฉด ๋ฐํ
16+ if n in self .memo :
17+ return self .memo [n ]
18+
19+ # 1์นธ ์ ๊ณผ 2์นธ ์ ์ ๊ฒฐ๊ณผ๋ฅผ ํฉํ ๊ฒ์ ๋ฐํ.
20+ result = self .climbStairs (n - 2 ) + self .climbStairs (n - 1 )
21+ self .memo [n ] = result
22+
23+ return result
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
3+
4+ # ๊ณต๊ฐ ๋ณต์ก๋ : 2๊ฐ์ list๋ฅผ ์ฌ์ฉํ๋ฏ๋ก O(2n)=O(n)
5+ # ์๊ฐ ๋ณต์ก๋ : nums๋ฅผ ์ธ๋ฒ ์ํ o(n)
6+ n = len (nums )
7+ left = [1 ] * n
8+ right = [1 ] * n
9+
10+ for idx in range (1 , n ):
11+ left [idx ] = left [idx - 1 ] * nums [idx - 1 ]
12+
13+ for idx in range (n - 2 , - 1 , - 1 ):
14+ right [idx ] = right [idx + 1 ] * nums [idx + 1 ]
15+
16+ return [left [i ] * right [i ] for i in range (n )]
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def isAnagram (self , s : str , t : str ) -> bool :
3+ hashmap = dict ()
4+
5+ # ๊ณต๊ฐ๋ณต์ก๋ : hashmap์ ์ฌ์ฉํ๋ฏ๋ก ๊ณต๊ฐ๋ณต์ก๋๋ O(N)์
๋๋ค.
6+ # ์๊ฐ๋ณต์ก๋ : ๋ฌธ์์ด s์ ๊ธธ์ด๋งํผ ํ๋ฒ ์ํํ๊ณ , ๋ค์ ๋ฌธ์์ด t์ ๊ธธ์ด๋งํผ ์ํํ๋ฏ๋ก O(s+t) => O(N)์
๋๋ค.
7+ for ch in s :
8+ hashmap [ch ] = hashmap .get (ch , 0 ) + 1
9+
10+ for ch in t :
11+ if ch in hashmap :
12+ hashmap [ch ] -= 1
13+ if hashmap [ch ] == 0 :
14+ del hashmap [ch ]
15+ else :
16+ return False
17+
18+ return not hashmap
19+
You canโt perform that action at this time.
0 commit comments