Skip to content
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
17 changes: 17 additions & 0 deletions combination-sum/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -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]
};
20 changes: 20 additions & 0 deletions decode-ways/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
time complexity : O(n)
space complexity : O(n)
*/
function numDecodings(s: string): number {
const memo = new Map<number, number>()
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)
}
21 changes: 21 additions & 0 deletions maximum-subarray/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -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
};
20 changes: 20 additions & 0 deletions number-of-1-bits/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -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
};
10 changes: 10 additions & 0 deletions valid-palindrome/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -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
};