-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[HodaeSsi] Week3 #804
[HodaeSsi] Week3 #804
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 시간복잡도 : O(n * m) (n: target, m: len(candidates)) | ||
# 공간복잡도 : O(n * m) | ||
class Solution: | ||
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
dp = [[] for _ in range(target + 1)] | ||
dp[0] = [[]] | ||
|
||
for candidate in candidates: | ||
for num in range(candidate, target + 1): | ||
for combination in dp[num - candidate]: | ||
temp = combination.copy() | ||
temp.extend([candidate]) | ||
dp[num].append(temp) | ||
|
||
return dp[target] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# 시간복잡도 : O(N) | ||
# 공간복잡도 : O(1) | ||
class Solution: | ||
def maxSubArray(self, nums: List[int]) -> int: | ||
global_sum = nums[0] | ||
local_sum = nums[0] | ||
|
||
for i in range(1, len(nums)): | ||
local_sum = max(nums[i], local_sum + nums[i]) | ||
global_sum = max(local_sum, global_sum) | ||
|
||
return global_sum | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# 시간복잡도: O(n) | ||
# 공간복잡도: O(n) | ||
class Solution: | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
prefix = [1] * len(nums) | ||
suffix = [1] * len(nums) | ||
product = [1] * len(nums) | ||
|
||
for idx in range(len(nums)): | ||
if idx == 0: | ||
prefix[idx] = nums[idx] | ||
else: | ||
prefix[idx] = prefix[idx - 1] * nums[idx] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이거는 사소한 거지만, 반복문에서 1부터 시작하면 조건문이 생략될 수 있지 않을까 합니다 |
||
|
||
for idx in range(len(nums) - 1, -1, -1): | ||
if idx == len(nums) - 1: | ||
suffix[idx] = nums[idx] | ||
else: | ||
suffix[idx] = suffix[idx + 1] * nums[idx] | ||
|
||
for idx in range(len(nums)): | ||
if idx == 0: | ||
product[idx] = suffix[idx + 1] | ||
elif idx == len(nums) - 1: | ||
product[idx] = prefix[idx - 1] | ||
else: | ||
product[idx] = prefix[idx - 1] * suffix[idx + 1] | ||
|
||
return product | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# 시간복잡도: O(1) (32bit) | ||
class Solution: | ||
def reverseBits(self, n: int) -> int: | ||
return int(bin(n)[2:].zfill(32)[::-1], 2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 말씀하신 파이썬의 위대함인 것 같습니다 ㅎㅎ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 동현님도 파이썬 하실래요?! 😆 |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# 시간복잡도 : O(n) | ||
# 공간복잡도 : O(n) | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
seen = {} # {num: idx, ...} | ||
|
||
for i, num in enumerate(nums): | ||
if target - num in seen: | ||
return [seen[target - num], i] | ||
seen[num] = i | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 되게 가독성 높은 풀이인 거 같습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
*이 코드는 gpt에 의해 작성된 코드입니다. 😉 |
||
|
||
return [] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
모든 합 경우에서 조합 구하는 방식으로 풀면 되는 거군요.
저는 시간이 많이 걸릴 것 같아서 target을 쪼개는 방식으로 했는데,
중복을 제거 하는 게 좀 힘들었던 거 같습니다 ㅜㅜ
좋은 풀이 잘 봤습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요즘 정형화된 알고리즘 및 자료구조로 안풀린다 싶으면 바로 DP를 생각해보는 방식으로 해보고 있긴 합니다!