Skip to content

[Wonjuny0804] WEEK 03 solutions #1309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2025
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
22 changes: 22 additions & 0 deletions combination-sum/Wonjuny0804.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function combinationSum(candidates: number[], target: number): number[][] {
const result: number[][] = [];

const dfs = (start: number, target: number, path: number[]) => {
if (target === 0) {
result.push([...path]);
return;
}

for (let i = start; i < candidates.length; i++) {
if (candidates[i] > target) continue;

path.push(candidates[i]);
dfs(i, target - candidates[i], path);
path.pop();
}
}

candidates.sort((a, b) => a - b);
dfs(0, target, []);
return result;
};
21 changes: 21 additions & 0 deletions decode-ways/Wonjuny0804.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

마지막 줄에 개행 추가 부탁드립니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function numDecodings(s: string): number {
const memo: Record<number, number> = {};

const dfs = (index: number): number => {
if (index === s.length) return 1;
if (s[index] === '0') return 0;

if (memo[index] !== undefined) return memo[index];

let res = dfs(index + 1);

if (index + 1 < s.length && Number(s.slice(index, index + 2)) <= 26) {
res += dfs(index + 2);
}

memo[index] = res;
return res;
}

return dfs(0);
}
13 changes: 13 additions & 0 deletions maximum-subarray/Wonjuny0804.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function maxSubArray(nums: number[]): number {
const n = nums.length;
const dp = new Array(n).fill(0);
dp[0] = nums[0];
let result = dp[0];

for (let i = 1; i < n; i++) {
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
result = Math.max(result, dp[i]);
}

return result;
}
6 changes: 6 additions & 0 deletions number-of-1-bits/Wonjuny0804.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function hammingWeight(n: number): number {
return n
.toString(2)
.split("")
.reduce((acc, i) => acc + +i, 0);
}
35 changes: 35 additions & 0 deletions two-sum/Wonjuny0804.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

*/

function twoSum(nums: number[], target: number): number[] {
const map = new Map();

for (let i = 0; i < nums.length; i++) {
const diff = target - nums[i];
if (map.has(nums[i])) return [map.get(nums[i]), i];
else map.set(diff, i);
}

return [];
}
12 changes: 12 additions & 0 deletions valid-palindrome/Wonjuny0804.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function isPalindrome(s: string): boolean {
const lowered = s.toLowerCase();
const removed = lowered.replaceAll(
/[!|\s|.\\|\|()@#$%^&*_\-+,:'"\[\]{}\?;`]/g,
""
);

for (let i = 0; i < removed.length; i++) {
if (removed[i] !== removed[removed.length - i - 1]) return false;
}
return true;
}