Skip to content

Commit 26dc9bf

Browse files
committed
week3 solution
1 parent 5a77dce commit 26dc9bf

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

combination-sum/hoyeongkwak.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function combinationSum(candidates: number[], target: number): number[][] {
2+
const dp: number[][][] = Array(target + 1).fill(null).map(() => [])
3+
dp[0] = [[]]
4+
5+
for (let i = 1; i <= target; i++){
6+
for (const num of candidates) {
7+
if (i - num >= 0 && dp[i - num].length > 0) {
8+
for (const combo of dp[i - num]) {
9+
if (combo.length === 0 || num >= combo[combo.length - 1]) {
10+
dp[i].push([...combo, num])
11+
}
12+
}
13+
}
14+
}
15+
}
16+
return dp[target]
17+
};

decode-ways/hoyeongkwak.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
time complexity : O(n)
3+
space complexity : O(n)
4+
*/
5+
function numDecodings(s: string): number {
6+
const memo = new Map<number, number>()
7+
const decode = (index: number): number => {
8+
if (index === s.length) return 1
9+
if (s[index] === '0') return 0
10+
if (memo.has(index)) return memo.get(index)
11+
12+
let ways = decode(index + 1)
13+
if (index + 1 < s.length && (s[index] === '1' || (s[index] === '2' && parseInt(s[index + 1]) <= 6))) {
14+
ways += decode(index + 2)
15+
}
16+
memo.set(index, ways)
17+
return ways
18+
}
19+
return decode(0)
20+
}

maximum-subarray/hoyeongkwak.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
time complexity : O(n)
3+
space complexity : O(1)
4+
*/
5+
function maxSubArray(nums: number[]): number {
6+
let maxSum = -Infinity
7+
let currSum = 0
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
currSum += nums[i]
11+
12+
if (currSum > maxSum) {
13+
maxSum = currSum
14+
}
15+
16+
if (currSum < 0) {
17+
currSum = 0
18+
}
19+
}
20+
return maxSum
21+
};

number-of-1-bits/hoyeongkwak.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
time complexity : O(log n)
3+
space complexity : O(1)
4+
*/
5+
function hammingWeight(n: number): number {
6+
let count = 0
7+
while ( n != 0) {
8+
count += n & 1
9+
n >>>= 1
10+
}
11+
return count
12+
13+
/*
14+
time complexity : O(log n)
15+
space complexity : O(log n)
16+
*/
17+
// const twoBits = n.toString(2)
18+
// const bitCount = twoBits.split('').filter((s) => s === '1').length
19+
// return bitCount
20+
};

valid-palindrome/hoyeongkwak.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
time complexity : O(n)
3+
space complexity : O(n)
4+
*/
5+
function isPalindrome(s: string): boolean {
6+
if (s.length === 1) return true
7+
const splitS = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase()
8+
const revS = splitS.split('').reverse().join('')
9+
return splitS === revS
10+
};

0 commit comments

Comments
 (0)