diff --git a/combination-sum/byol-han.js b/combination-sum/byol-han.js new file mode 100644 index 000000000..bf80724b1 --- /dev/null +++ b/combination-sum/byol-han.js @@ -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; +}; diff --git a/decode-ways/byol-han.js b/decode-ways/byol-han.js new file mode 100644 index 000000000..f80602bf4 --- /dev/null +++ b/decode-ways/byol-han.js @@ -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]; +}; diff --git a/maximum-subarray/byol-han.js b/maximum-subarray/byol-han.js new file mode 100644 index 000000000..cb5a5ec33 --- /dev/null +++ b/maximum-subarray/byol-han.js @@ -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; +}; diff --git a/number-of-1-bits/byol-han.js b/number-of-1-bits/byol-han.js new file mode 100644 index 000000000..c221e7cd5 --- /dev/null +++ b/number-of-1-bits/byol-han.js @@ -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; +}; diff --git a/valid-palindrome/byol-han.js b/valid-palindrome/byol-han.js new file mode 100644 index 000000000..7e26307c1 --- /dev/null +++ b/valid-palindrome/byol-han.js @@ -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; +};