Skip to content

[hyer0705] WEEK 03 solutions #1788

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions combination-sum/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function combinationSum(candidates: number[], target: number): number[][] {
const results: number[][] = [];

function backtrack(currentIndex: number, sum: number, selected: number[]) {
if (sum > target) return;
if (sum === target) {
results.push([...selected]);
return;
}

for (let i = currentIndex; i < candidates.length; i++) {
selected.push(candidates[i]);
backtrack(i, sum + candidates[i], selected);
selected.pop();
}
}

backtrack(0, 0, []);

return results;
}
25 changes: 25 additions & 0 deletions decode-ways/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function numDecodings(s: string): number {
const sLen = s.length;
const isValid = (s: string): boolean => {
if (s[0] === "0") return false;

return Number(s) > 0 && Number(s) <= 26;
};

if (sLen === 0) return 0;
if (s.length === 1) return isValid(s[0]) ? 1 : 0;

// dp[i]: i번째 위치까지 디코딩할 수 있는 방법의 수
const dp: number[] = Array(sLen).fill(0);
dp[0] = isValid(s[0]) ? 1 : 0;
dp[1] = (isValid(s[1]) ? dp[0] : 0) + (isValid(s.substring(0, 2)) ? 1 : 0);

for (let i = 2; i < sLen; i++) {
const singleDigitWays = isValid(s[i]) ? dp[i - 1] : 0;
const doubleDigitWays = isValid(s[i - 1] + s[i]) ? dp[i - 2] : 0;

dp[i] = singleDigitWays + doubleDigitWays;
}

return dp[sLen - 1];
}
12 changes: 12 additions & 0 deletions maximum-subarray/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function maxSubArray(nums: number[]): number {
const numsLen = nums.length;
let currentSum = nums[0];
let maxSum = nums[0];

for (let i = 1; i < numsLen; i++) {
currentSum = Math.max(currentSum + nums[i], nums[i]);
maxSum = Math.max(maxSum, currentSum);
}

return maxSum;
}
5 changes: 5 additions & 0 deletions number-of-1-bits/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function hammingWeight(n: number): number {
const RADIX = 2;

return n.toString(RADIX).match(/1/g)?.length || 0;
}
14 changes: 14 additions & 0 deletions valid-palindrome/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function isPalindrome(s: string): boolean {
const converted = s.toLowerCase().replace(/[^a-z\d]/g, "");

let l = 0;
let r = converted.length - 1;

while (l < r) {
if (converted[l] !== converted[r]) return false;
l++;
r--;
}

return true;
}