From 7f114c6e459e23986c815f62a074a3bb6b04a7cd Mon Sep 17 00:00:00 2001 From: krokerdile Date: Thu, 17 Apr 2025 19:17:08 +0900 Subject: [PATCH 1/6] valid-padindrome solution --- valid-palindrome/krokerdile.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 valid-palindrome/krokerdile.js diff --git a/valid-palindrome/krokerdile.js b/valid-palindrome/krokerdile.js new file mode 100644 index 000000000..809283bf7 --- /dev/null +++ b/valid-palindrome/krokerdile.js @@ -0,0 +1,9 @@ +function filterStr(inputString) { + return inputString.replace(/[^a-zA-Z0-9]/g, '').toLowerCase(); +} + +var isPalindrome = function(s) { + const filtered = filterStr(s); + const reversed = filtered.split('').reverse().join(''); + return filtered === reversed; +}; From 4ab5a0371296473a8824b7a46facf54f86445e25 Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 18 Apr 2025 18:36:49 +0900 Subject: [PATCH 2/6] number of 1 bits solution --- number-of-1-bits/krokerdile.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 number-of-1-bits/krokerdile.js diff --git a/number-of-1-bits/krokerdile.js b/number-of-1-bits/krokerdile.js new file mode 100644 index 000000000..4742faf6f --- /dev/null +++ b/number-of-1-bits/krokerdile.js @@ -0,0 +1,14 @@ +/** + * @param {number} n + * @return {number} + */ +var hammingWeight = function(n) { + let list = n.toString(2).split('').map(Number); + let sum = 0; + list.forEach((m)=>{ + if(m == 1){ + sum += 1;a + } + }) + return sum; +}; From 0e7bd7862f589e4fbcf7e06a9d84ad650fd76713 Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 18 Apr 2025 18:37:58 +0900 Subject: [PATCH 3/6] combination-sum solution --- combination-sum/krokerdile.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 combination-sum/krokerdile.js diff --git a/combination-sum/krokerdile.js b/combination-sum/krokerdile.js new file mode 100644 index 000000000..e4441b37d --- /dev/null +++ b/combination-sum/krokerdile.js @@ -0,0 +1,26 @@ +/** + * @param {number[]} candidates + * @param {number} target + * @return {number[][]} + */ +var combinationSum = function(candidates, target) { + const result = []; + + const dfs = (start, path, sum) => { + if (sum === target) { + result.push([...path]); // 정답 조합 발견 + return; + } + + if (sum > target) return; // target 초과 -> 백트랙 + + for (let i = start; i < candidates.length; i++) { + path.push(candidates[i]); // 숫자 선택 + dfs(i, path, sum + candidates[i]); // 같은 인덱스부터 다시 탐색 (중복 사용 허용) + path.pop(); // 백트래킹 + } + }; + + dfs(0, [], 0); + return result; +}; From 3864ab52aea0b3b7011c9b998ab2ece0dd4345cb Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 18 Apr 2025 18:39:49 +0900 Subject: [PATCH 4/6] decode ways solution --- decode-ways/krokerdile.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 decode-ways/krokerdile.js diff --git a/decode-ways/krokerdile.js b/decode-ways/krokerdile.js new file mode 100644 index 000000000..c21b3b8d0 --- /dev/null +++ b/decode-ways/krokerdile.js @@ -0,0 +1,27 @@ +/** + * @param {string} s + * @return {number} + */ +var numDecodings = function(s) { + const n = s.length; + const memo = {}; + + function dfs(index) { + if (index === n) return 1; + if (s[index] === '0') return 0; + if (memo.hasOwnProperty(index)) return memo[index]; + + let count = dfs(index + 1); + if (index + 1 < n) { + const twoDigit = parseInt(s.slice(index, index + 2)); + if (twoDigit >= 10 && twoDigit <= 26) { + count += dfs(index + 2); + } + } + + memo[index] = count; + return count; + } + + return dfs(0); +}; \ No newline at end of file From f1980721c3bd3f66ab6e9366ad015bf8452421ec Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 18 Apr 2025 18:40:52 +0900 Subject: [PATCH 5/6] maximum subarray solution --- maximum-subarray/krokerdile.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 maximum-subarray/krokerdile.js diff --git a/maximum-subarray/krokerdile.js b/maximum-subarray/krokerdile.js new file mode 100644 index 000000000..12853fb3c --- /dev/null +++ b/maximum-subarray/krokerdile.js @@ -0,0 +1,16 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var maxSubArray = function(nums) { + let maxSum = nums[0]; + let currentSum = 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; +}; From 11df4e1cb9cc1d01384df4c2cd9ffd06894898c7 Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 18 Apr 2025 18:44:17 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=EA=B0=9C=ED=96=89=EB=AC=B8=EC=9E=90=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- decode-ways/krokerdile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decode-ways/krokerdile.js b/decode-ways/krokerdile.js index c21b3b8d0..c604be77c 100644 --- a/decode-ways/krokerdile.js +++ b/decode-ways/krokerdile.js @@ -24,4 +24,4 @@ var numDecodings = function(s) { } return dfs(0); -}; \ No newline at end of file +};