Skip to content

Commit a724a34

Browse files
authored
Merge pull request #374 from TonyKim9401/main
[TONY] WEEK 03 Solutions
2 parents 34f0816 + daf037d commit a724a34

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

climbing-stairs/TonyKim9401.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int climbStairs(int n) {
3+
// Time complexity: O(n);
4+
// Space complexity: O(n);
5+
int[] dp = new int[n+1];
6+
7+
if (n >= 1) dp[1] = 1;
8+
if (n >= 2) dp[2] = 2;
9+
10+
for (int i = 3; i <= n; i++) {
11+
dp[i] = dp[i-1] + dp[i-2];
12+
}
13+
return dp[n];
14+
}
15+
}

coin-change/TonyKim9401.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int coinChange(int[] coins, int amount) {
3+
// TC: O(amount*n)
4+
// SC: O(amount)
5+
int[] dp = new int[amount + 1];
6+
Arrays.fill(dp, amount + 1);
7+
8+
dp[0] = 0;
9+
10+
for (int i = 1; i <= amount; i++) {
11+
for (int j = 0; j < coins.length; j++) {
12+
if (i - coins[j] >= 0) {
13+
dp[i] = Math.min(dp[i], 1 + dp[i - coins[j]]);
14+
}
15+
}
16+
}
17+
return dp[amount] != amount + 1 ? dp[amount] : -1;
18+
}
19+
}

combination-sum/TonyKim9401.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
// time complexity: O(2^n);
3+
// space complecity: O(n*m);
4+
private List<List<Integer>> output = new ArrayList<>();
5+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
6+
backtracking(candidates, 0, target, new ArrayList<>());
7+
return output;
8+
}
9+
10+
public void backtracking(int[] candidates, int idx, int target, List<Integer> inside) {
11+
if (target == 0) {
12+
output.add(new ArrayList<>(inside));
13+
return;
14+
}
15+
16+
if (idx > candidates.length - 1 || target < 0) return;
17+
18+
inside.add(candidates[idx]);
19+
backtracking(candidates, idx, target - candidates[idx], inside);
20+
inside.remove(inside.size()-1);
21+
backtracking(candidates, idx+1, target, inside);
22+
}
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int[] productExceptSelf(int[] nums) {
3+
// time complexity: O(n)
4+
// space complexity: O(n)
5+
int[] left = new int[nums.length];
6+
int[] right = new int[nums.length];
7+
8+
// nums = [1, 2, 3, 4]
9+
10+
// left = [1, 1, 2, 6]
11+
// right = [24, 12, 4, 1]
12+
13+
// nums => [24, 12, 8, 6]
14+
left[0] = 1;
15+
for (int i = 1; i < nums.length; i++) {
16+
left[i] = left[i-1] * nums[i-1];
17+
}
18+
19+
right[right.length-1] = 1;
20+
for (int i = nums.length - 2; i >= 0; i--) {
21+
right[i] = right[i+1] * nums[i+1];
22+
}
23+
24+
for (int i = 0; i < nums.length; i++) {
25+
nums[i] = left[i] * right[i];
26+
}
27+
return nums;
28+
}
29+
}

two-sum/TonyKim9401.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int[] twoSum(int[] nums, int target) {
3+
// time complexity: O(n);
4+
// space complexity: O(n);
5+
Map<Integer, Integer> map = new HashMap<>();
6+
for (int i = 0; i < nums.length; i++) map.put(nums[i], i);
7+
8+
for (int i = 0; i < nums.length; i++) {
9+
int candidateKey = target - nums[i];
10+
if (map.containsKey(candidateKey) && i != map.get(candidateKey))
11+
return new int[]{i, map.get(candidateKey)};
12+
}
13+
return new int[]{-1, -1};
14+
}
15+
}

0 commit comments

Comments
 (0)