Skip to content

Commit 33dc6e9

Browse files
authored
Merge pull request DaleStudy#2451 from gcount85/main
2 parents c5ed177 + fcf0f05 commit 33dc6e9

5 files changed

Lines changed: 130 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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

โ€Ždecode-ways/gcount85.pyโ€Ž

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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

0 commit comments

Comments
ย (0)