From 26dc9bfac2606049ef5322b6688cddfb915b0d67 Mon Sep 17 00:00:00 2001 From: hoyeongkwak Date: Sun, 20 Apr 2025 20:52:42 +0900 Subject: [PATCH 1/2] week3 solution --- combination-sum/hoyeongkwak.ts | 17 +++++++++++++++++ decode-ways/hoyeongkwak.ts | 20 ++++++++++++++++++++ maximum-subarray/hoyeongkwak.ts | 21 +++++++++++++++++++++ number-of-1-bits/hoyeongkwak.ts | 20 ++++++++++++++++++++ valid-palindrome/hoyeongkwak.ts | 10 ++++++++++ 5 files changed, 88 insertions(+) create mode 100644 combination-sum/hoyeongkwak.ts create mode 100644 decode-ways/hoyeongkwak.ts create mode 100644 maximum-subarray/hoyeongkwak.ts create mode 100644 number-of-1-bits/hoyeongkwak.ts create mode 100644 valid-palindrome/hoyeongkwak.ts diff --git a/combination-sum/hoyeongkwak.ts b/combination-sum/hoyeongkwak.ts new file mode 100644 index 000000000..99285f0b4 --- /dev/null +++ b/combination-sum/hoyeongkwak.ts @@ -0,0 +1,17 @@ +function combinationSum(candidates: number[], target: number): number[][] { + const dp: number[][][] = Array(target + 1).fill(null).map(() => []) + dp[0] = [[]] + + for (let i = 1; i <= target; i++){ + for (const num of candidates) { + if (i - num >= 0 && dp[i - num].length > 0) { + for (const combo of dp[i - num]) { + if (combo.length === 0 || num >= combo[combo.length - 1]) { + dp[i].push([...combo, num]) + } + } + } + } + } + return dp[target] +}; \ No newline at end of file diff --git a/decode-ways/hoyeongkwak.ts b/decode-ways/hoyeongkwak.ts new file mode 100644 index 000000000..0b5221d38 --- /dev/null +++ b/decode-ways/hoyeongkwak.ts @@ -0,0 +1,20 @@ +/* + time complexity : O(n) + space complexity : O(n) +*/ +function numDecodings(s: string): number { + const memo = new Map() + const decode = (index: number): number => { + if (index === s.length) return 1 + if (s[index] === '0') return 0 + if (memo.has(index)) return memo.get(index) + + let ways = decode(index + 1) + if (index + 1 < s.length && (s[index] === '1' || (s[index] === '2' && parseInt(s[index + 1]) <= 6))) { + ways += decode(index + 2) + } + memo.set(index, ways) + return ways + } + return decode(0) +} \ No newline at end of file diff --git a/maximum-subarray/hoyeongkwak.ts b/maximum-subarray/hoyeongkwak.ts new file mode 100644 index 000000000..9966deada --- /dev/null +++ b/maximum-subarray/hoyeongkwak.ts @@ -0,0 +1,21 @@ +/* + time complexity : O(n) + space complexity : O(1) +*/ +function maxSubArray(nums: number[]): number { + let maxSum = -Infinity + let currSum = 0 + + for (let i = 0; i < nums.length; i++) { + currSum += nums[i] + + if (currSum > maxSum) { + maxSum = currSum + } + + if (currSum < 0) { + currSum = 0 + } + } + return maxSum +}; \ No newline at end of file diff --git a/number-of-1-bits/hoyeongkwak.ts b/number-of-1-bits/hoyeongkwak.ts new file mode 100644 index 000000000..81cb718bf --- /dev/null +++ b/number-of-1-bits/hoyeongkwak.ts @@ -0,0 +1,20 @@ +/* + time complexity : O(log n) + space complexity : O(1) +*/ +function hammingWeight(n: number): number { + let count = 0 + while ( n != 0) { + count += n & 1 + n >>>= 1 + } + return count + + /* + time complexity : O(log n) + space complexity : O(log n) + */ + // const twoBits = n.toString(2) + // const bitCount = twoBits.split('').filter((s) => s === '1').length + // return bitCount +}; \ No newline at end of file diff --git a/valid-palindrome/hoyeongkwak.ts b/valid-palindrome/hoyeongkwak.ts new file mode 100644 index 000000000..269fade64 --- /dev/null +++ b/valid-palindrome/hoyeongkwak.ts @@ -0,0 +1,10 @@ +/* +time complexity : O(n) +space complexity : O(n) +*/ +function isPalindrome(s: string): boolean { + if (s.length === 1) return true + const splitS = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase() + const revS = splitS.split('').reverse().join('') + return splitS === revS +}; \ No newline at end of file From 8a1541dce562f29a492c02e40e4ac1f26df8a095 Mon Sep 17 00:00:00 2001 From: hoyeongkwak Date: Sun, 20 Apr 2025 21:14:51 +0900 Subject: [PATCH 2/2] lint modify --- combination-sum/hoyeongkwak.ts | 2 +- decode-ways/hoyeongkwak.ts | 2 +- maximum-subarray/hoyeongkwak.ts | 2 +- number-of-1-bits/hoyeongkwak.ts | 2 +- valid-palindrome/hoyeongkwak.ts | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/combination-sum/hoyeongkwak.ts b/combination-sum/hoyeongkwak.ts index 99285f0b4..0c26d64ea 100644 --- a/combination-sum/hoyeongkwak.ts +++ b/combination-sum/hoyeongkwak.ts @@ -14,4 +14,4 @@ function combinationSum(candidates: number[], target: number): number[][] { } } return dp[target] -}; \ No newline at end of file +}; diff --git a/decode-ways/hoyeongkwak.ts b/decode-ways/hoyeongkwak.ts index 0b5221d38..511ceca1a 100644 --- a/decode-ways/hoyeongkwak.ts +++ b/decode-ways/hoyeongkwak.ts @@ -17,4 +17,4 @@ function numDecodings(s: string): number { return ways } return decode(0) -} \ No newline at end of file +} diff --git a/maximum-subarray/hoyeongkwak.ts b/maximum-subarray/hoyeongkwak.ts index 9966deada..751dc04b7 100644 --- a/maximum-subarray/hoyeongkwak.ts +++ b/maximum-subarray/hoyeongkwak.ts @@ -18,4 +18,4 @@ function maxSubArray(nums: number[]): number { } } return maxSum -}; \ No newline at end of file +}; diff --git a/number-of-1-bits/hoyeongkwak.ts b/number-of-1-bits/hoyeongkwak.ts index 81cb718bf..e80020c56 100644 --- a/number-of-1-bits/hoyeongkwak.ts +++ b/number-of-1-bits/hoyeongkwak.ts @@ -17,4 +17,4 @@ function hammingWeight(n: number): number { // const twoBits = n.toString(2) // const bitCount = twoBits.split('').filter((s) => s === '1').length // return bitCount -}; \ No newline at end of file +}; diff --git a/valid-palindrome/hoyeongkwak.ts b/valid-palindrome/hoyeongkwak.ts index 269fade64..5660a678e 100644 --- a/valid-palindrome/hoyeongkwak.ts +++ b/valid-palindrome/hoyeongkwak.ts @@ -7,4 +7,4 @@ function isPalindrome(s: string): boolean { const splitS = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase() const revS = splitS.split('').reverse().join('') return splitS === revS -}; \ No newline at end of file +};