Skip to content

[HoonDongKang] WEEK 03 solutions #1326

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
Apr 19, 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
54 changes: 54 additions & 0 deletions combination-sum/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* [Problem]: [39] Combination Sum
*
* (https://leetcode.com/problems/combination-sum/description/)
*/
function combinationSum(candidates: number[], target: number): number[][] {
// 시간복잡도: O(c^t)
// 공간복잡도 O(t)
function dfsFunc(candidates: number[], target: number): number[][] {
let result: number[][] = [];
let nums: number[] = [];

function dfs(start: number, total: number): void | number[][] {
if (total > target) return;
if (total === target) {
result.push([...nums]);
return result;
}
for (let i = start; i < candidates.length; i++) {
let num = candidates[i];
nums.push(num);
dfs(i, total + num);
nums.pop();
}
}

dfs(0, 0);
return result;
}

// 시간복잡도: O(c*t)
// 공간복잡도 O(c*t)
function dpFunc(candidates: number[], target: number): number[][] {
const dp: number[][][] = Array(target + 1)
.fill(null)
.map(() => []);

dp[0] = [[]];

for (const num of candidates) {
for (let t = num; t <= target; t++) {
for (const comb of dp[t - num]) {
dp[t].push([...comb, num]);
}
}
}

return dp[target];
}

return dpFunc(candidates, target);
}

console.log(combinationSum([2, 3, 6, 7], 7));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 백트레킹 방식을 썼는데 dp 방식으로도 할 수 있네요!!
백트레킹 보다 코드 이해는 쉽네요!

51 changes: 51 additions & 0 deletions decode-ways/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* [Problem]: [91] Decode Ways
*
* (https://leetcode.com/problems/decode-ways/description/)
*/
function numDecodings(s: string): number {
//시간복잡도 O(n)
//공간복잡도 O(n)
function memoizationFunc(s: string): number {
const memoization: Record<number, number> = {};

function dfs(index: number): number {
if (index in memoization) return memoization[index];
if (index === s.length) return 1;
if (s[index] === "0") return 0;

let result = dfs(index + 1);
if (index + 1 < s.length && +s.slice(index, index + 2) <= 26) {
result += dfs(index + 2);
}

memoization[index] = result;
return result;
}

return dfs(0);
}

//시간복잡도 O(n)
//공간복잡도 O(1)
function optimizedFunc(s: string): number {
let prev2 = 1;
let prev1 = s[0] === "0" ? 0 : 1;

for (let i = 1; i < s.length; i++) {
let curr = 0;

const one = +s.slice(i, i + 1);
const two = +s.slice(i - 1, i + 1);

if (one >= 1 && one <= 9) curr += prev1;
if (two >= 10 && two <= 26) curr += prev2;

prev2 = prev1;
prev1 = curr;
}

return prev1;
}
return optimizedFunc(s);
Comment on lines +31 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 방식으로 공간 복잡도도 줄일 수 있네요
저는 이것도 dp를 사용해서 공간 복잡도가 O(n)인데 저장하지 않고 바로 비교하면 더 줄이게 되네요

}
36 changes: 36 additions & 0 deletions maximum-subarray/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* [Problem]: [53] Maximum Subarray
*
* (https://leetcode.com/problems/maximum-subarray/description/)
*/

function maxSubArray(nums: number[]): number {
//시간복잡도 O(n)
//공간복잡도 O(1)
function getMax(nums: number[]): number {
let result = nums[0];
let sum = 0;

nums.forEach((num) => {
sum = Math.max(num, sum + num);
result = Math.max(sum, result);
});

return result;
}

//시간복잡도 O(n)
//공간복잡도 O(1)
function dpFunc(nums: number[]): number {
const dp = Array(nums.length).fill(0);
dp[0] = nums[0];

for (let i = 1; i < nums.length; i++) {
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
}

return Math.max(...dp);
}

return dpFunc(nums);
}
31 changes: 31 additions & 0 deletions number-of-1-bits/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* [Problem]: [191] Number of 1 Bits
*
* (https://leetcode.com/problems/number-of-1-bits/description/)
*/
function hammingWeight(n: number): number {
// 시간 복잡도 O(log n)
// 공간 복잡도 O(1)
function divisionFunc(n: number): number {
let count = 0;
while (n > 0) {
count += n % 2;
n = Math.floor(n / 2);
}

return count;
}

// 시간 복잡도 O(log n)
// 공간 복잡도 O(1)
function bitwiseFunc(n: number): number {
let count = 0;
while (n !== 0) {
count += n & 1;
n = n >>> 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

논리 연산자를 사용하는 방법이 char를 하나씩 비교하는 것보다 더 직관적인 방법 같아요!
배우고 갑니다!

}

return count;
}
return divisionFunc(n);
}
49 changes: 49 additions & 0 deletions valid-palindrome/HoonDongKang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* [Problem]: [125] Valid Palindrome
*
* (https://leetcode.com/problems/valid-palindrome/description/)
*/

function isPalindrome(s: string): boolean {
// 시간복잡도: O(n)
// 공간복잡도: O(n)
function twoPointerFunc(s: string): boolean {
let stringArr: string[] = [...s.replace(/[^a-zA-Z0-9]/g, "")];
let left = 0;
let right = stringArr.length - 1;

while (left < right) {
if (stringArr[left].toLowerCase() === stringArr[right].toLowerCase()) {
left++;
right--;
} else {
return false;
}
}

return true;
}

// 시간복잡도: O(n)
// 공간복잡도: O(1)
function optimizedTwoPointerFunc(s: string): boolean {
let left = 0;
let right = s.length - 1;

while (left < right) {
while (left < right && !isLetterOrDigit(s[left])) left++;
while (left < right && !isLetterOrDigit(s[right])) right--;

if (s[left].toLowerCase() !== s[right].toLowerCase()) return false;
left++;
right--;
}

return true;

function isLetterOrDigit(c: string): boolean {
return /[a-zA-Z0-9]/.test(c);
}
}
return optimizedTwoPointerFunc(s);
}