diff --git a/combination-sum/hoyeongkwak.ts b/combination-sum/hoyeongkwak.ts new file mode 100644 index 000000000..0c26d64ea --- /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] +}; diff --git a/decode-ways/hoyeongkwak.ts b/decode-ways/hoyeongkwak.ts new file mode 100644 index 000000000..511ceca1a --- /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) +} diff --git a/maximum-subarray/hoyeongkwak.ts b/maximum-subarray/hoyeongkwak.ts new file mode 100644 index 000000000..751dc04b7 --- /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 +}; diff --git a/number-of-1-bits/hoyeongkwak.ts b/number-of-1-bits/hoyeongkwak.ts new file mode 100644 index 000000000..e80020c56 --- /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 +}; diff --git a/valid-palindrome/hoyeongkwak.ts b/valid-palindrome/hoyeongkwak.ts new file mode 100644 index 000000000..5660a678e --- /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 +};