Skip to content

Commit 3932e72

Browse files
committed
2 parents 86be455 + 8f0e973 commit 3932e72

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+2595
-17
lines changed

climbing-stairs/hongseoupyun.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def climbStairs(self, n: int) -> int:
2020

2121
prev1 = 1
2222
prev2 = 2
23-
for i in range(n,n+1):
23+
for i in range(3,n+1):
2424
current = prev2 + prev1
2525
prev1 = prev2
2626
prev2 = current

climbing-stairs/ymir0804.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int climbStairs(int n) {
3+
int result = 0;
4+
int first = 1;
5+
int second = 2;
6+
int third = 3;
7+
if (n == 1) {
8+
return first;
9+
} else if (n == 2) {
10+
return second;
11+
} else if (n == 3) {
12+
return third;
13+
}
14+
15+
for (int i = 4; i <= n; i++) {
16+
result = second + third;
17+
second = third;
18+
third = result;
19+
}
20+
return result;
21+
}
22+
}

climbing-stairs/youngDaLee.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package youngDaLee
2+
3+
func climbStairs(n int) int {
4+
if n <= 2 {
5+
return n
6+
}
7+
8+
dp := make([]int, n+1)
9+
dp[1] = 1
10+
dp[2] = 2
11+
12+
for i := 3; i <= n; i++ {
13+
dp[i] = dp[i-1] + dp[i-2]
14+
}
15+
return dp[n]
16+
}
17+
18+
/*
19+
Limit Exceeded
20+
func climbStairs(n int) int {
21+
return dfs(0, n)
22+
}
23+
24+
func dfs(now, n int) int {
25+
if now > n {
26+
return 0
27+
}
28+
if now == n {
29+
return 1
30+
}
31+
32+
return dfs(now+1, n) + dfs(now+2, n)
33+
}
34+
*/

combination-sum/1lsang.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function combinationSum(candidates: number[], target: number): number[][] {
2+
// arr: index까지 갈 수 있는 combinationSum
3+
const arr:number[][][] = Array.from({ length: target + 1 }, () => [] as number[][]);
4+
// 0을 만들 수 있는 방법은 숫자가 없는 것
5+
arr[0].push([] as number[]);
6+
7+
for (const candidate of candidates) {
8+
for (let n = candidate; n <= target; n++) {
9+
for (const combination of arr[n-candidate]) {
10+
arr[n].push([...combination, candidate]);
11+
}
12+
}
13+
}
14+
console.log(arr);
15+
return arr[target];
16+
};

combination-sum/Blossssom.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param - candidates: 정수 배열, target: 대상 정수
3+
* @returns - 선택한 숫자의 합이 해당하는 모든 고유 조합 반환
4+
* @description
5+
* 1. 요소는 여러번 사용 가능
6+
*/
7+
8+
function combinationSum(candidates: number[], target: number): number[][] {
9+
const result: number[][] = [];
10+
11+
function recursive(remain: number, idx: number, path: number[]) {
12+
if (remain === 0) {
13+
result.push([...path]);
14+
return;
15+
}
16+
17+
for (let i = idx; i < candidates.length; i++) {
18+
const currentNum = candidates[i];
19+
if (currentNum > remain) {
20+
break;
21+
}
22+
23+
path.push(currentNum);
24+
recursive(remain - currentNum, i, path);
25+
path.pop();
26+
}
27+
}
28+
29+
recursive(target, 0, []);
30+
return result;
31+
}
32+
33+
const candidates = [2, 3, 5];
34+
const target = 8;
35+
combinationSum(candidates, target);
36+

combination-sum/Donghae0230.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 문제 풀이
2+
# 모든 경우의 수를 탐색하기 위해 백트래킹 사용
3+
# - 현재 조합의 합이 target보다 크면 종료
4+
# - 현재 조합의 합이 target과 같으면 결과에 추가
5+
6+
class Solution:
7+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
8+
def backtrack(start, combination):
9+
if sum(combination) > target:
10+
return
11+
if sum(combination) == target:
12+
result.append(combination[:])
13+
return
14+
for i in range(start, len(candidates)):
15+
combination.append(candidates[i])
16+
backtrack(i, combination)
17+
combination.pop()
18+
19+
result = []
20+
backtrack(0, [])
21+
return result

combination-sum/JangAyeon.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} candidates
3+
* @param {number} target
4+
* @return {number[][]}
5+
*/
6+
var combinationSum = function (arr, target) {
7+
const N = arr.length;
8+
const answer = [];
9+
function dfs(total, idx, route) {
10+
if (total >= target) {
11+
if (total == target) {
12+
answer.push(route);
13+
}
14+
return;
15+
}
16+
for (let i = idx; i < N; i++) {
17+
dfs(total + arr[i], i, [...route, arr[i]]);
18+
}
19+
}
20+
21+
dfs(0, 0, []);
22+
return answer;
23+
};

combination-sum/Seoya0512.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
Approach
3+
- target값에서 candidate 값을 빼고 그 누적합을 사용해야한다는 흐름은 파악했지지만 구현이 어려웠습니다.
4+
-그래서 알고달레를 참고해서 최대한 이해하고 혼자 작성해보려고 했습니다....
5+
'''
6+
class Solution:
7+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
8+
dp = [[] for _ in range(target + 1)]
9+
dp[0] = [[]]
10+
11+
for candidate in candidates:
12+
for num in range(candidate, target +1):
13+
for combination in dp[num - candidate]:
14+
dp[num].append(combination + [candidate])
15+
return dp[target]

combination-sum/casentino.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function combinationSum(candidates: number[], target: number): number[][] {
2+
const results: number[][] = [];
3+
4+
function comb(index: number, arr: number[], sum: number) {
5+
if (sum === target) {
6+
results.push([...arr]);
7+
return;
8+
}
9+
if (sum > target || candidates.length <= index) {
10+
return;
11+
}
12+
arr.push(candidates[index]);
13+
comb(index, arr, sum + candidates[index]);
14+
arr.pop();
15+
comb(index + 1, arr, sum);
16+
}
17+
18+
comb(0, [], 0);
19+
return results;
20+
}

combination-sum/changhyumm.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
ans = []
4+
def combination(index, cur_comb, cur_sum):
5+
if cur_sum == target:
6+
ans.append(cur_comb[:])
7+
return
8+
if cur_sum > target or index >= len(candidates):
9+
return
10+
cur_comb.append(candidates[index])
11+
combination(index, cur_comb, cur_sum + candidates[index])
12+
# 합이 target이랑 같던지, 크던지, 길이를 넘던지하면 return으로 탈출
13+
# 그 외에 다른 경우의 수를 봐야하므로
14+
# 마지막꺼는 다시 빼고 어쨌든 index 넘겨서 다시 combination 확인해봐야함
15+
cur_comb.pop()
16+
combination(index + 1, cur_comb, cur_sum)
17+
return ans
18+
19+
return combination(0, [], 0)

0 commit comments

Comments
 (0)