Skip to content
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

Merged
merged 5 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions combination-sum/HodaeSsi.py
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

모든 합 경우에서 조합 구하는 방식으로 풀면 되는 거군요.
저는 시간이 많이 걸릴 것 같아서 target을 쪼개는 방식으로 했는데,
중복을 제거 하는 게 좀 힘들었던 거 같습니다 ㅜㅜ

좋은 풀이 잘 봤습니다.

Copy link
Contributor Author

@HodaeSsi HodaeSsi Dec 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요즘 정형화된 알고리즘 및 자료구조로 안풀린다 싶으면 바로 DP를 생각해보는 방식으로 해보고 있긴 합니다!


return dp[target]

13 changes: 13 additions & 0 deletions maximum-subarray/HodaeSsi.py
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

30 changes: 30 additions & 0 deletions product-of-array-except-self/HodaeSsi.py
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]
Copy link
Contributor

Choose a reason for hiding this comment

The 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

5 changes: 5 additions & 0 deletions reverse-bits/HodaeSsi.py
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

말씀하신 파이썬의 위대함인 것 같습니다 ㅎㅎ
엄청 간단하게 풀리네요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

동현님도 파이썬 하실래요?! 😆


13 changes: 13 additions & 0 deletions two-sum/HodaeSsi.py
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

되게 가독성 높은 풀이인 거 같습니다.
같은 방법인데 자바로 깔끔하게 풀려면 어떻게 해야 할까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import java.util.HashMap;
import java.util.Map;

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> seen = new HashMap<>(); // {num: idx, ...}

        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (seen.containsKey(complement)) {
                return new int[] {seen.get(complement), i};
            }
            seen.put(nums[i], i);
        }

        return new int[] {}; // 적절한 결과가 없으면 빈 배열 반환
    }
}

*이 코드는 gpt에 의해 작성된 코드입니다. 😉


return []

Loading