Skip to content

[TONY] WEEK 03 Solutions #374

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

Merged
merged 5 commits into from
Aug 31, 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
15 changes: 15 additions & 0 deletions climbing-stairs/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int climbStairs(int n) {
// Time complexity: O(n);
// Space complexity: O(n);
int[] dp = new int[n+1];

if (n >= 1) dp[1] = 1;
if (n >= 2) dp[2] = 2;
Comment on lines +7 to +8
Copy link
Contributor

Choose a reason for hiding this comment

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

n 이 1 또는 2 일 때는 아래 반복문을 수행하지 않아서 if 조건이 없어도 될 것 같아요!

Copy link
Contributor

Choose a reason for hiding this comment

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

아 지금 보니 인덱스 처리때문에 추가하신 거군요
그럼에도 문제에서 n 은 1 이상이라고 했으니, if 조건은 2에만 있어도 될 것 같네요!


for (int i = 3; i <= n; i++) {
dp[i] = dp[i-1] + dp[i-2];
}
return dp[n];
}
}
19 changes: 19 additions & 0 deletions coin-change/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int coinChange(int[] coins, int amount) {
// TC: O(amount*n)
// SC: O(amount)
int[] dp = new int[amount + 1];
Arrays.fill(dp, amount + 1);

dp[0] = 0;

for (int i = 1; i <= amount; i++) {
for (int j = 0; j < coins.length; j++) {
if (i - coins[j] >= 0) {
dp[i] = Math.min(dp[i], 1 + dp[i - coins[j]]);
}
}
}
return dp[amount] != amount + 1 ? dp[amount] : -1;
}
}
23 changes: 23 additions & 0 deletions combination-sum/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
// time complexity: O(2^n);
// space complecity: O(n*m);
Copy link
Member

Choose a reason for hiding this comment

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

어떻게 공간 복잡도가 O(n * m)이 나오셨는지 궁금합니다!

Copy link
Contributor

@taekwon-dev taekwon-dev Aug 29, 2024

Choose a reason for hiding this comment

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

어떻게 공간 복잡도가 O(n * m)이 나오셨는지 궁금합니다!

(공간 복잡도에 더해 ㅎㅎ..) 재귀가 사용 됐을 때 시간 복잡도를 어떻게 판단하시는지 과정이 궁금합니다..!

Copy link
Contributor Author

@TonyKim9401 TonyKim9401 Aug 31, 2024

Choose a reason for hiding this comment

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

@DaleSeo @taekwon-dev 정정하겠습니다!
시간 복잡도: O(2^n * m)

  • 같은 숫자 반속 가능성과 주어진 배열의 길이

공간 복잡도: O(n * m)

  • 가능한 조합의 수와 가능한 조합의 길이

Copy link
Member

Choose a reason for hiding this comment

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

@TonyKim9401 저는 공간 복잡도에서 의문을 제기했는데, 시간 복잡도를 정정해주셨네요 ㅋㅋ 😵

private List<List<Integer>> output = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
backtracking(candidates, 0, target, new ArrayList<>());
return output;
}

public void backtracking(int[] candidates, int idx, int target, List<Integer> inside) {
if (target == 0) {
output.add(new ArrayList<>(inside));
return;
}

if (idx > candidates.length - 1 || target < 0) return;

inside.add(candidates[idx]);
backtracking(candidates, idx, target - candidates[idx], inside);
inside.remove(inside.size()-1);
backtracking(candidates, idx+1, target, inside);
}
}
29 changes: 29 additions & 0 deletions product-of-array-except-self/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public int[] productExceptSelf(int[] nums) {
// time complexity: O(n)
// space complexity: O(n)
int[] left = new int[nums.length];
int[] right = new int[nums.length];

// nums = [1, 2, 3, 4]

// left = [1, 1, 2, 6]
// right = [24, 12, 4, 1]

// nums => [24, 12, 8, 6]
left[0] = 1;
for (int i = 1; i < nums.length; i++) {
left[i] = left[i-1] * nums[i-1];
}

right[right.length-1] = 1;
for (int i = nums.length - 2; i >= 0; i--) {
right[i] = right[i+1] * nums[i+1];
}

for (int i = 0; i < nums.length; i++) {
nums[i] = left[i] * right[i];
}
return nums;
}
}
15 changes: 15 additions & 0 deletions two-sum/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int[] twoSum(int[] nums, int target) {
// time complexity: O(n);
// space complexity: O(n);
Comment on lines +3 to +4
Copy link
Contributor

Choose a reason for hiding this comment

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

(사소) Python 에서는 주석을 메서드 내부에 쓰긴 하나, Java 에서는 이렇게 작성하는 건 많이 보지 못했습니다 (물론 주석 바로 아래 코드에 대해 주석이 필요할 때는 작성하겠지만 요건 그 경우는 아닌 것 같군요 ㅎㅎㅎ) 주석을 메서드 또는 클래스 위로 옮겨주시면 어떨까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bky373 다음주부터 적용하도록 하겠습니다 감사합니다!

Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) map.put(nums[i], i);
Copy link
Contributor

Choose a reason for hiding this comment

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

시간 복잡도는 동일하지만 for 문을 한 번 사용하는 것으로 문제를 해결할 수 있습니다!
코드를 바꿔달라고 말씀드리는 건 아니고 나중에 시간되실 때 확인 한 번 해보시면 좋을 것 같습니다~


for (int i = 0; i < nums.length; i++) {
int candidateKey = target - nums[i];
if (map.containsKey(candidateKey) && i != map.get(candidateKey))
return new int[]{i, map.get(candidateKey)};
}
return new int[]{-1, -1};
}
}