Skip to content

[soobing] WEEK 03 Solutions #1794

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

Merged
merged 5 commits into from
Aug 9, 2025
Merged
Show file tree
Hide file tree
Changes from all 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;
}
29 changes: 29 additions & 0 deletions decode-ways/soobing2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 문제 유형
* - DP (점화식 이용, 백트래킹 사용시 TLE 발생)
*
* 문제 설명
* - 주어진 문자열을 숫자로 변환하는 방법의 갯수를 구하는 문제
*
* 아이디어
* 1) f(i) = 마지막 한자리수가 변환 가능하면 f(i-1) + 마지막 두자리수가 가능하면 f(i-2)
* - 한자리수는 0만 아니면 됨, 두자리수는 10~26 사이여야 함.
*
*/
function numDecodings(s: string): number {
const n = s.length;
if (n === 1) return s[0] === "0" ? 0 : 1;

const dp = Array(n + 1).fill(0);
dp[0] = 1;
dp[1] = s[0] === "0" ? 0 : 1;

for (let i = 2; i <= n; i++) {
const one = Number(s[i - 1]);
const two = Number(s[i - 2] + s[i - 1]);
if (one !== 0) dp[i] += dp[i - 1];
if (two >= 10 && two <= 26) dp[i] += dp[i - 2];
}

return dp[n];
}
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;
}
15 changes: 15 additions & 0 deletions valid-palindrome/soobing2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* 문제 유형
* - 문자열 처리, 문자열 비교
*
* 문제 설명
* - 주어진 문자열이 팰린드롬인지 확인하는 문제
*
* 아이디어
* 1) 주어진 문자열에서 숫자, 알파벳만 남겨두고 소문자로 변환 후에 팰린드롬인지 확인
*/
function isPalindrome(s: string): boolean {
const original = s.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
const reverse = original.split("").reverse().join("");
return original === reverse;
}