Skip to content

Commit dfd507b

Browse files
authored
Merge pull request #804 from HodaeSsi/main
[HodaeSsi] Week3
2 parents 29a2b40 + ffbcf9a commit dfd507b

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

combination-sum/HodaeSsi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 시간복잡도 : O(n * m) (n: target, m: len(candidates))
2+
# 공간복잡도 : O(n * m)
3+
class Solution:
4+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
5+
dp = [[] for _ in range(target + 1)]
6+
dp[0] = [[]]
7+
8+
for candidate in candidates:
9+
for num in range(candidate, target + 1):
10+
for combination in dp[num - candidate]:
11+
temp = combination.copy()
12+
temp.extend([candidate])
13+
dp[num].append(temp)
14+
15+
return dp[target]
16+

maximum-subarray/HodaeSsi.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 시간복잡도 : O(N)
2+
# 공간복잡도 : O(1)
3+
class Solution:
4+
def maxSubArray(self, nums: List[int]) -> int:
5+
global_sum = nums[0]
6+
local_sum = nums[0]
7+
8+
for i in range(1, len(nums)):
9+
local_sum = max(nums[i], local_sum + nums[i])
10+
global_sum = max(local_sum, global_sum)
11+
12+
return global_sum
13+
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 시간복잡도: O(n)
2+
# 공간복잡도: O(n)
3+
class Solution:
4+
def productExceptSelf(self, nums: List[int]) -> List[int]:
5+
prefix = [1] * len(nums)
6+
suffix = [1] * len(nums)
7+
product = [1] * len(nums)
8+
9+
for idx in range(len(nums)):
10+
if idx == 0:
11+
prefix[idx] = nums[idx]
12+
else:
13+
prefix[idx] = prefix[idx - 1] * nums[idx]
14+
15+
for idx in range(len(nums) - 1, -1, -1):
16+
if idx == len(nums) - 1:
17+
suffix[idx] = nums[idx]
18+
else:
19+
suffix[idx] = suffix[idx + 1] * nums[idx]
20+
21+
for idx in range(len(nums)):
22+
if idx == 0:
23+
product[idx] = suffix[idx + 1]
24+
elif idx == len(nums) - 1:
25+
product[idx] = prefix[idx - 1]
26+
else:
27+
product[idx] = prefix[idx - 1] * suffix[idx + 1]
28+
29+
return product
30+

reverse-bits/HodaeSsi.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 시간복잡도: O(1) (32bit)
2+
class Solution:
3+
def reverseBits(self, n: int) -> int:
4+
return int(bin(n)[2:].zfill(32)[::-1], 2)
5+

two-sum/HodaeSsi.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 시간복잡도 : O(n)
2+
# 공간복잡도 : O(n)
3+
class Solution:
4+
def twoSum(self, nums: List[int], target: int) -> List[int]:
5+
seen = {} # {num: idx, ...}
6+
7+
for i, num in enumerate(nums):
8+
if target - num in seen:
9+
return [seen[target - num], i]
10+
seen[num] = i
11+
12+
return []
13+

0 commit comments

Comments
 (0)