Skip to content

[byol-han] WEEK 03 solutions #1286

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 5 commits into from
Apr 21, 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
25 changes: 25 additions & 0 deletions combination-sum/byol-han.js
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Backtracking 이 뭔지... 코드가 어떻게 돌아가는건지... 이해못함. 더 공부해야함.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum = function (candidates, target) {
const result = [];

function backtrack(remaining, combination, start) {
if (remaining === 0) {
result.push([...combination]);
return;
}
if (remaining < 0) return;

for (let i = start; i < candidates.length; i++) {
combination.push(candidates[i]);
backtrack(remaining - candidates[i], combination, i); // 같은 숫자 다시 사용 가능
combination.pop(); // backtrack
}
}

backtrack(target, [], 0);
return result;
};
27 changes: 27 additions & 0 deletions decode-ways/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @param {string} s
* @return {number}
*/
var numDecodings = function (s) {
if (!s || s[0] === "0") return 0;

const n = s.length;
const dp = Array(n + 1).fill(0);

dp[0] = 1; // 빈 문자열은 1가지 방법
dp[1] = 1; // 첫 글자가 0이 아니면 1가지 방법

for (let i = 2; i <= n; i++) {
const oneDigit = parseInt(s.slice(i - 1, i));
const twoDigits = parseInt(s.slice(i - 2, i));

if (oneDigit >= 1 && oneDigit <= 9) {
dp[i] += dp[i - 1];
}
if (twoDigits >= 10 && twoDigits <= 26) {
dp[i] += dp[i - 2];
}
}

return dp[n];
};
19 changes: 19 additions & 0 deletions maximum-subarray/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function (nums) {
// 초기값 설정: 현재까지의 최대합과 전체 최대합을 배열의 첫 번째 값으로 초기화
let currentSum = nums[0];
let maxSum = nums[0];

// 두 번째 원소부터 순회
for (let i = 1; i < nums.length; i++) {
// 이전까지의 합에 현재 원소를 더할지, 아니면 현재 원소부터 새로 시작할지 결정
currentSum = Math.max(nums[i], currentSum + nums[i]);
// 전체 최대값 갱신
maxSum = Math.max(maxSum, currentSum);
}

return maxSum;
};
30 changes: 30 additions & 0 deletions number-of-1-bits/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @param {number} n
* @return {number}
*/
//1. divide the number by 2 and count the remainder
var hammingWeight = function (n) {
let count = 0;
while (n > 0) {
if (n % 2 === 1) {
count++;
}
n = Math.floor(n / 2);
}
return count;
};

//2. Count the number of set bits (1s) in the binary representation of n
var hammingWeight = function (n) {
return n.toString(2).split("1").length - 1;
};

//3. bit manipulation
var hammingWeight = function (n) {
let count = 0;
while (n > 0) {
count += n & 1; // 마지막 비트가 1이면 count++
n = n >>> 1; // 오른쪽으로 한 비트 이동 (2로 나눔)
}
return count;
};
32 changes: 32 additions & 0 deletions valid-palindrome/byol-han.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @param {string} s
* @return {boolean}
*/

// 1. Two Pointers
// time complexity: O(n)
// space complexity: O(1)
var isPalindrome = function (s) {
let sRefine = s.toLowerCase().replace(/[^a-z0-9]/g, "");
let left = 0;
let right = sRefine.length - 1;

while (left < right) {
if (sRefine[left] !== sRefine[right]) {
return false;
}
left++;
right--;
}

return true;
};

// 2. String Manipulation
// time complexity: O(n)
// space complexity: O(n)
var isPalindrome = function (s) {
let refined = s.toLowerCase().replace(/[^a-z0-9]/g, "");
let reversed = refined.split("").reverse().join("");
return refined === reversed;
};