File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ """
2+ # Intuition
3+ backtracking
4+
5+ # Complexity
6+ - Time complexity: N์ ๊น์ด๋งํผ ๊ณฑํ๋ค. ๋ฐ๋ผ์ ์๋์ ๊ฐ์ ๋, O(N^(T/M))
7+ N = candidates ๊ฐ์
8+ T = target
9+ M = candidates ์ค ์ต์๊ฐ
10+
11+ - Space complexity: O(T/M)
12+ """
13+
14+
15+ class Solution :
16+ def combinationSum (self , candidates : List [int ], target : int ) -> List [List [int ]]:
17+ candidates .sort ()
18+ answer = []
19+ output = []
20+
21+ def dfs (cur_i , cur_sum ):
22+ if cur_sum == target :
23+ answer .append (output [:])
24+ return
25+
26+ for i in range (cur_i , len (candidates )):
27+ num = candidates [i ]
28+
29+ if cur_sum + num > target :
30+ break
31+
32+ output .append (num )
33+ dfs (i , cur_sum + num )
34+ output .pop ()
35+
36+ dfs (0 , 0 )
37+ return answer
Original file line number Diff line number Diff line change 1+ """
2+ # Intuition
3+ dp[n]์ s์ n๋ฒ์งธ ๊ธ์๊น์ง ๋์ฝ๋ฉํ ์ ์๋ ๊ฒฝ์ฐ์ ์
4+ dp[n] = dp[n-1](if can be decoded) + dp[n-2](if can be decoded)
5+
6+ # Approach
7+ i-1 ~ i๊น์ง์ ์ซ์, i-2 ~ i๊น์ง์ ์ซ์๊ฐ ๊ฐ๊ฐ ๋์ฝ๋ฉ ๋ ์ ์๋์ง ํ์ธํ๋ค.
8+ ๋์ฝ๋ฉ ๊ฐ๋ฅํ๋ฉด ๊ทธ ์์น์ dp ๋ฐฐ์ด์ ๊ฐ์ ๊ฐ๊ฐ ๋ํ๊ณ , ๋์ฝ๋ฉ ๋ถ๊ฐ๋ฅํ๋ฉด ๋ํ์ง ์๋๋ค.
9+
10+ # Complexity
11+ - Time complexity: N์ s์ ๊ธธ์ด๋ผ๊ณ ํ ๋, ๋ฐ๋ณต๋ฌธ์ผ๋ก O(N)
12+
13+ - Space complexity: dp ๋ฐฐ์ด ๋ง๋๋ ๋ฐ์ O(N)
14+ """
15+
16+
17+ class Solution :
18+ def numDecodings (self , s : str ) -> int :
19+ n = len (s )
20+ dp = [0 ] * (n + 1 )
21+ dp [1 ] = 1 if s [0 ] != "0" else 0
22+ if dp [1 ] == 0 or n == 1 :
23+ return dp [1 ]
24+ dp [2 ] = 1 if int (s [1 ]) > 0 else 0
25+ dp [2 ] += 1 if int (s [:2 ]) > 9 and int (s [:2 ]) < 27 else 0
26+ for i in range (3 , n + 1 ):
27+ dp [i ] = dp [i - 1 ] if s [i - 1 : i ] != "0" else 0
28+ dp [i ] += dp [i - 2 ] if s [i - 2 : i ] > "09" and s [i - 2 : i ] < "27" else 0
29+ return dp [n ]
Original file line number Diff line number Diff line change 1+ """
2+ # Intuition
3+ max(์ ์์น๊น์ง์ sum + ์ง๊ธ ์์น ๊ฐ, ์ง๊ธ ์์น์ ๊ฐ)
4+
5+ # Complexity
6+ - Time complexity: nums์ ๊ธธ์ด๋ฅผ N์ด๋ผ๊ณ ํ ๋ O(N)
7+
8+ - Space complexity: O(1)
9+ """
10+
11+
12+ class Solution :
13+ def maxSubArray (self , nums : List [int ]) -> int :
14+ a = nums [0 ]
15+ best = a
16+ for i in range (1 , len (nums )):
17+ a = max (a + nums [i ], nums [i ])
18+ best = max (a , best )
19+ return best
Original file line number Diff line number Diff line change 1+ """
2+ # Intuition
3+ n์ n-1๊ณผ AND ๋นํธ ์ฐ์ฐํ๋ฉด์ 1์ ์ ๊ฑฐํด๋๊ฐ๋ฉฐ ์นด์ดํธํ๋ค.
4+
5+ # Complexity
6+ - Time complexity: n์ ์ด์ง์ ๋ณํ์์ 1์ ๊ฐ์๋ฅผ K๋ผ๊ณ ํ ๋, O(K)
7+
8+ - Space complexity: O(1)
9+ """
10+
11+
12+ class Solution :
13+ def hammingWeight (self , n : int ) -> int :
14+ count = 0
15+ while n :
16+ n &= n - 1
17+ count += 1
18+ return count
Original file line number Diff line number Diff line change 1+ """
2+ # Approach
3+ ์ ๋์ ํฌ์ธํฐ๋ฅผ ๋๊ณ ํ์ด์ฌ์ isalnum(), lower() ๋ฌธ์์ด ๋ฉ์๋๋ก ๊ฒ์ฌํฉ๋๋ค.
4+
5+ # Complexity
6+ - Time complexity: s์ ๊ธธ์ด๋ฅผ N์ด๋ผ๊ณ ํ ๋, O(N)
7+
8+ - Space complexity: O(1)
9+ """
10+
11+
12+ class Solution :
13+ def isPalindrome (self , s : str ) -> bool :
14+ l = 0
15+ r = len (s ) - 1
16+ while l < r :
17+ if not s [l ].isalnum ():
18+ l += 1
19+ continue
20+ if not s [r ].isalnum ():
21+ r -= 1
22+ continue
23+ if s [l ].lower () != s [r ].lower ():
24+ return False
25+ l += 1
26+ r -= 1
27+ return True
You canโt perform that action at this time.
0 commit comments