Skip to content

Commit 01e005a

Browse files
authored
Merge pull request #395 from taekwon-dev/main
[์œคํƒœ๊ถŒ] Week3 ๋ฌธ์ œ ํ’€์ด
2 parents ec9da38 + 4846bd7 commit 01e005a

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

โ€Žclimbing-stairs/taekwon-dev.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
3+
* - n = 5 ๊ฒฝ์šฐ๋ฅผ ๊ฐ€์ •ํ•˜๊ณ  ์ƒ๊ฐํ•ด๋ณด๊ธฐ
4+
* - 3์ธต ๊ณ„๋‹จ -> 2์ธต, 1์ธต ๊ณ„์‚ฐ (์ด ๊ฒฝ์šฐ๋Š” ์ด๋ฏธ ๋ฉ”๋ชจ๋˜์–ด ์žˆ์–ด์„œ ๋ณ„๋„๋กœ ๊ณ„์‚ฐํ•˜์ง„ ์•Š์Œ)
5+
* - 4์ธต ๊ณ„๋‹จ -> 3์ธต, 2์ธต ๊ณ„์‚ฐ (2์ธต์€ ๋ฉ”๋ชจ์— ์žˆ๋Š” ๊ฒƒ ํ™œ์šฉ)
6+
* - 5์ธต ๊ณ„๋‹จ -> 4์ธต, 3์ธต ๊ณ„์‚ฐ (3์ธต์€ ๋ฉ”๋ชจ์— ์žˆ๋Š” ๊ฒƒ ํ™œ์šฉ)
7+
* - ...
8+
* - ๊ฐ ๋‹จ๊ณ„ ๋ณ„๋กœ (๋ฉ”๋ชจ๋ฅผ ํ™œ์šฉํ•ด) ์•„์ง ๊ณ„์‚ฐ๋˜์ง€ ์•Š์€ ๊ฒƒ์„ ํ•œ ๋ฒˆ์”ฉ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ O(n)
9+
* - ๊ทผ๋ฐ ๋งŒ์•ฝ ๋ฉ”๋ชจ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด? (์ค‘๋ณต ํ˜ธ์ถœ์ด ๋งŽ์ด ์ผ์–ด๋‚จ ... O(n^2))
10+
*
11+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
12+
*/
13+
class Solution {
14+
public int climbStairs(int n) {
15+
int[] memo = new int[n + 1];
16+
return climbStairs(n, memo);
17+
}
18+
19+
public int climbStairs(int n, int[] memo) {
20+
if (n == 1) return 1;
21+
if (n == 2) return 2;
22+
23+
if (memo[n] > 0) return memo[n];
24+
25+
memo[n] = climbStairs(n - 1, memo) + climbStairs(n - 2, memo);
26+
return memo[n];
27+
}
28+
}

โ€Žcombination-sum/taekwon-dev.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* ์•Œ๊ณ ๋ฆฌ์ฆ˜: ๋ฐฑํŠธ๋ž™ํ‚น
3+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n^t)
4+
* - n: candidates.lenth
5+
* - t: target / candidates์˜ ์ตœ์†Ÿ๊ฐ’
6+
* - ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ดํ•ดํ•ด๋ณด์ž!
7+
*
8+
* - candidates: [2, 3], target: 6
9+
* - [2] -> [2, 2] -> [2, 2, 2] -> OK (์—ฌ๊ธฐ์„œ ์ƒ๊ฐํ•ด๋ณด๋ฉด, ์•„ ์žฌ๊ท€๋ฅผ ์ตœ๋Œ€ 6/2 ๋งŒํผ ํƒ€๊ณ  ๋“ค์–ด๊ฐ€๊ฒ ๊ตฐ?)
10+
* - [2, 2] -> [2, 2, 3] -> X
11+
* - [2] -> [2, 3] -> [2, 3, 3] -> X
12+
* - [3] -> [3, 3] -> OK
13+
* - ์ค‘๊ฐ„์— sum > target, sum == target ์ธ ๊ฒฝ์šฐ return ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ์ผ€์ด์Šค๋ฅผ ๋‹ค ๊ฒ€์‚ฌํ•˜์ง„ ์•Š์ง€๋งŒ
14+
* - ๊ธฐ๋ณธ์ ์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ฒŒ ๋  ๊ฒฝ์šฐ ์ตœ์•…์˜ ๊ฒฝ์šฐ์—๋Š” O(n^t) ๋งŒํผ ์†Œ์š”๋œ๋‹ค.
15+
*
16+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(t)
17+
* - t: target / candidates์˜ ์ตœ์†Ÿ๊ฐ’
18+
* - ์ด๊ฒƒ๋„ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ๊นŒ ์ฃผ์–ด์ง„ candidates.length (=n) ๊ฐ’์— ๋น„๋ก€ํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ
19+
* - (๊ฐ€๋Šฅํ•œ ์ผ€์ด์Šค์—์„œ) ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์œผ๋กœ ํƒ€๊ฒŸ์„ ์ฑ„์šฐ๋Š” ๊ฒŒ ๊ฐ€์žฅ ๋งŽ์€ ์‚ฌ์ด์ฆˆ๋ฅผ ์ฐจ์ง€ํ•˜๋Š” ๊ฐ’์ด ๋ ํ…๋ฐ, ์ด๊ฒƒ์— ์˜ํ–ฅ์„ ๋ฐ›๊ฒ ๊ตฐ.
20+
*
21+
*/
22+
class Solution {
23+
24+
private List<List<Integer>> answer = new ArrayList<>();
25+
private List<Integer> combination = new ArrayList<>();
26+
27+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
28+
backtracking(candidates, target, 0, 0);
29+
return answer;
30+
}
31+
32+
private void backtracking(int[] candidates, int target, int sum, int start) {
33+
if (sum > target) {
34+
return;
35+
}
36+
if (sum == target) {
37+
answer.add(new ArrayList<>(combination));
38+
return;
39+
}
40+
for (int i = start; i < candidates.length; i++) {
41+
combination.add(candidates[i]);
42+
backtracking(candidates, target, sum + candidates[i], i);
43+
combination.remove(combination.size() - 1);
44+
}
45+
}
46+
}

โ€Žtwo-sum/taekwon-dev.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* ์‹œ๊ฐ„/๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
3+
*/
4+
class Solution {
5+
public int[] twoSum(int[] nums, int target) {
6+
Map<Integer, Integer> map = new HashMap<>();
7+
8+
for (int idx = 0; idx < nums.length; idx++) {
9+
int complement = target - nums[idx];
10+
if (map.containsKey(complement)) {
11+
return new int[]{map.get(complement), idx};
12+
}
13+
map.put(nums[idx], idx);
14+
}
15+
16+
return new int[]{-1, -1};
17+
}
18+
}

0 commit comments

Comments
ย (0)