Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
36 changes: 36 additions & 0 deletions combination-sum/soobing2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 문제 유형
* - DP: DFS + Backtracking
*
* 문제 설명
* - 주어진 숫자 배열에서 조합을 만들어서 합이 target이 되는 경우를 찾기
*
* 아이디어
* 1) Backtracking 활용
* - dfs를 통해 합이 target이 되는 모든 케이스를 탐색한다. (단, 조건 체크 후에 즉시 중단 = 백트래킹)
*/
function combinationSum(candidates: number[], target: number): number[][] {
const result: number[][] = [];

function backtracking(
startIndex: number,
combination: number[],
total: number
) {
if (total === target) {
result.push([...combination]);
return;
}

if (total > target) return;

for (let i = startIndex; i < candidates.length; i++) {
combination.push(candidates[i]);
backtracking(i, combination, total + candidates[i]);
combination.pop();
}
}

backtracking(0, [], 0);
return result;
}
23 changes: 23 additions & 0 deletions maximum-subarray/soobing2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 문제 유형
* - Array, DP
*
* 문제 설명
* - 배열에서 "연속된" 부분 배열의 합 중 가장 큰 값을 구하기
*
* 아이디어
* 1) Bottom-Up 방식
* - dp에는 앞에서부터 이어붙인 값이 큰지, 현재 값에서 다시 시작하는게 클지 비교하여 큰 값 저장 (현재 기준)
* - maxSum은 전체 dp중 가장 큰 값을 저장
*/
function maxSubArray(nums: number[]): number {
const dp = new Array(nums.length);
dp[0] = nums[0];
let maxSum = nums[0];

for (let i = 1; i < nums.length; i++) {
dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
maxSum = Math.max(maxSum, dp[i]);
}
return maxSum;
}
20 changes: 20 additions & 0 deletions number-of-1-bits/soobing2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 문제 유형
* - Binary (개념을 알고있는지), 기본적인 구현 문제
*
* 문제 설명
* - 주어진 정수를 2진수로 변환했을때 1의 갯수를 구하는 문제
*
* 아이디어
* 1) 나누기 2를 통해 2진수로 변환하고 1인 경우 갯수를 카운트한다.
*/
function hammingWeight(n: number): number {
let quotient = n;
let count = 0;

while (quotient) {
if (quotient % 2 === 1) count++;
quotient = Math.floor(quotient / 2);
}
return count;
}