From 94e3f5047884f77ea8db5933ca8fa55557dd86b3 Mon Sep 17 00:00:00 2001 From: soobing Date: Tue, 5 Aug 2025 22:36:11 +0900 Subject: [PATCH 1/5] feat(soobing): week3 > maximum-subarray --- maximum-subarray/soobing2.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 maximum-subarray/soobing2.ts diff --git a/maximum-subarray/soobing2.ts b/maximum-subarray/soobing2.ts new file mode 100644 index 0000000000..05f1efccf3 --- /dev/null +++ b/maximum-subarray/soobing2.ts @@ -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; +} From 2bf8e97b0e2fe9834662f9fffc91b0c34d4f5769 Mon Sep 17 00:00:00 2001 From: soobing Date: Wed, 6 Aug 2025 00:16:23 +0900 Subject: [PATCH 2/5] feat(soobing): week3 > combination-sum --- combination-sum/soobing2.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 combination-sum/soobing2.ts diff --git a/combination-sum/soobing2.ts b/combination-sum/soobing2.ts new file mode 100644 index 0000000000..5c0fbb6523 --- /dev/null +++ b/combination-sum/soobing2.ts @@ -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; +} From c8ef9a68079f5f8b9e000cdef61ff8ede7c8fad9 Mon Sep 17 00:00:00 2001 From: soobing Date: Thu, 7 Aug 2025 23:50:27 +0900 Subject: [PATCH 3/5] feat(soobing): week3 > number-of-1-bits --- number-of-1-bits/soobing2.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 number-of-1-bits/soobing2.ts diff --git a/number-of-1-bits/soobing2.ts b/number-of-1-bits/soobing2.ts new file mode 100644 index 0000000000..36d94b0959 --- /dev/null +++ b/number-of-1-bits/soobing2.ts @@ -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; +} From c9f69f4f58975729a6142ef3dc576d26342a9cc0 Mon Sep 17 00:00:00 2001 From: soobing Date: Sat, 9 Aug 2025 21:04:19 +0900 Subject: [PATCH 4/5] feat(soobing): week3 > decode-ways --- decode-ways/soobing2.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 decode-ways/soobing2.ts diff --git a/decode-ways/soobing2.ts b/decode-ways/soobing2.ts new file mode 100644 index 0000000000..d3c577162c --- /dev/null +++ b/decode-ways/soobing2.ts @@ -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]; +} From 3c937f61ad388324a73df43cf61a9688ce53e05a Mon Sep 17 00:00:00 2001 From: soobing Date: Sat, 9 Aug 2025 21:24:59 +0900 Subject: [PATCH 5/5] feat(soobing): week3 > valid-palindrome --- valid-palindrome/soobing2.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 valid-palindrome/soobing2.ts diff --git a/valid-palindrome/soobing2.ts b/valid-palindrome/soobing2.ts new file mode 100644 index 0000000000..48cc6129ad --- /dev/null +++ b/valid-palindrome/soobing2.ts @@ -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; +}