Skip to content

Commit b2b9712

Browse files
authored
Merge pull request #1309 from Wonjuny0804/main
[Wonjuny0804] WEEK 03 solutions
2 parents 632dea3 + 14fb531 commit b2b9712

File tree

6 files changed

+109
-0
lines changed

6 files changed

+109
-0
lines changed

combination-sum/Wonjuny0804.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function combinationSum(candidates: number[], target: number): number[][] {
2+
const result: number[][] = [];
3+
4+
const dfs = (start: number, target: number, path: number[]) => {
5+
if (target === 0) {
6+
result.push([...path]);
7+
return;
8+
}
9+
10+
for (let i = start; i < candidates.length; i++) {
11+
if (candidates[i] > target) continue;
12+
13+
path.push(candidates[i]);
14+
dfs(i, target - candidates[i], path);
15+
path.pop();
16+
}
17+
}
18+
19+
candidates.sort((a, b) => a - b);
20+
dfs(0, target, []);
21+
return result;
22+
};

decode-ways/Wonjuny0804.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function numDecodings(s: string): number {
2+
const memo: Record<number, number> = {};
3+
4+
const dfs = (index: number): number => {
5+
if (index === s.length) return 1;
6+
if (s[index] === '0') return 0;
7+
8+
if (memo[index] !== undefined) return memo[index];
9+
10+
let res = dfs(index + 1);
11+
12+
if (index + 1 < s.length && Number(s.slice(index, index + 2)) <= 26) {
13+
res += dfs(index + 2);
14+
}
15+
16+
memo[index] = res;
17+
return res;
18+
}
19+
20+
return dfs(0);
21+
}

maximum-subarray/Wonjuny0804.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function maxSubArray(nums: number[]): number {
2+
const n = nums.length;
3+
const dp = new Array(n).fill(0);
4+
dp[0] = nums[0];
5+
let result = dp[0];
6+
7+
for (let i = 1; i < n; i++) {
8+
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
9+
result = Math.max(result, dp[i]);
10+
}
11+
12+
return result;
13+
}

number-of-1-bits/Wonjuny0804.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function hammingWeight(n: number): number {
2+
return n
3+
.toString(2)
4+
.split("")
5+
.reduce((acc, i) => acc + +i, 0);
6+
}

two-sum/Wonjuny0804.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
3+
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
4+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
5+
You can return the answer in any order.
6+
7+
Example 1:
8+
9+
Input: nums = [2,7,11,15], target = 9
10+
Output: [0,1]
11+
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
12+
13+
Example 2:
14+
15+
Input: nums = [3,2,4], target = 6
16+
Output: [1,2]
17+
18+
Example 3:
19+
20+
Input: nums = [3,3], target = 6
21+
Output: [0,1]
22+
23+
*/
24+
25+
function twoSum(nums: number[], target: number): number[] {
26+
const map = new Map();
27+
28+
for (let i = 0; i < nums.length; i++) {
29+
const diff = target - nums[i];
30+
if (map.has(nums[i])) return [map.get(nums[i]), i];
31+
else map.set(diff, i);
32+
}
33+
34+
return [];
35+
}

valid-palindrome/Wonjuny0804.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function isPalindrome(s: string): boolean {
2+
const lowered = s.toLowerCase();
3+
const removed = lowered.replaceAll(
4+
/[!|\s|.\\|\|()@#$%^&*_\-+,:'"\[\]{}\?;`]/g,
5+
""
6+
);
7+
8+
for (let i = 0; i < removed.length; i++) {
9+
if (removed[i] !== removed[removed.length - i - 1]) return false;
10+
}
11+
return true;
12+
}

0 commit comments

Comments
 (0)