-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
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,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; | ||
|
||
for (int i = 3; i <= n; i++) { | ||
dp[i] = dp[i-1] + dp[i-2]; | ||
} | ||
return dp[n]; | ||
} | ||
} |
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; | ||
} | ||
} |
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); | ||
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.
(공간 복잡도에 더해 ㅎㅎ..) 재귀가 사용 됐을 때 시간 복잡도를 어떻게 판단하시는지 과정이 궁금합니다..! 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. @DaleSeo @taekwon-dev 정정하겠습니다!
공간 복잡도: O(n * m)
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. @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); | ||
} | ||
} |
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; | ||
} | ||
} |
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
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. @bky373 다음주부터 적용하도록 하겠습니다 감사합니다! |
||
Map<Integer, Integer> map = new HashMap<>(); | ||
for (int i = 0; i < nums.length; i++) map.put(nums[i], 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. 시간 복잡도는 동일하지만 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}; | ||
} | ||
} |
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.
n 이 1 또는 2 일 때는 아래 반복문을 수행하지 않아서 if 조건이 없어도 될 것 같아요!
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.
아 지금 보니 인덱스 처리때문에 추가하신 거군요
그럼에도 문제에서 n 은 1 이상이라고 했으니, if 조건은 2에만 있어도 될 것 같네요!