Skip to content

[krokerdile] WEEK 03 solutions #1312

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 6 commits into from
Apr 19, 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
26 changes: 26 additions & 0 deletions combination-sum/krokerdile.js
Original file line number Diff line number Diff line change
@@ -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;
};
27 changes: 27 additions & 0 deletions decode-ways/krokerdile.js
Copy link
Contributor

@JEONGBEOMKO JEONGBEOMKO Apr 18, 2025

Choose a reason for hiding this comment

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

dfs와 dp를 사용하여 시간복잡도 O(n) 공간복잡도 O(n) 풀이를 보고 저도 많은 도움이 되었습니다. 이번 주차도 고생하셨습니다~!

Copy link
Contributor Author

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,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);
};
16 changes: 16 additions & 0 deletions maximum-subarray/krokerdile.js
Original file line number Diff line number Diff line change
@@ -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;
};
14 changes: 14 additions & 0 deletions number-of-1-bits/krokerdile.js
Original file line number Diff line number Diff line change
@@ -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;
};
9 changes: 9 additions & 0 deletions valid-palindrome/krokerdile.js
Original file line number Diff line number Diff line change
@@ -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;
};