Skip to content

[reach0908] WEEK 03 solutions #1790

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
Aug 10, 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
34 changes: 34 additions & 0 deletions combination-sum/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 시간복잡도 O(n^(T/m))
* 풀이방법: DFS 백트래킹
* 결과: 2ms
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
const combinationSum = function (candidates, target) {
const result = [];
const nums = [];

const sortedCandidates = candidates.sort((a, b) => a - b);

function dfs(start, sum) {
if (sum === target) {
result.push([...nums]);
}

for (let i = start; i < sortedCandidates.length; i += 1) {
const num = sortedCandidates[i];

if (sum + num > target) break;

nums.push(num);
dfs(i, sum + num);
nums.pop();
}
}

dfs(0, 0);

return result;
};
73 changes: 73 additions & 0 deletions decode-ways/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 시간복잡도: O(n)
* 실행속도: 65ms
* @param {string} s
* @return {number}
*/
const numDecodings = function (s) {
const n = s.length;

const memo = new Array(n + 1).fill(0);

function dfs(i) {
if (i === n) {
return 1;
}

if (s[i] === "0") {
return 0;
}

if (memo[i]) {
return memo[i];
}

let res = dfs(i + 1);

if (i + 1 < n && Number(s.slice(i, i + 2) <= 26)) {
res += dfs(i + 2);
}

memo[i] = res;

return res;
}

return dfs[0];
};

/**
* 시간복잡도: O(n)
* 실행속도: 0~1ms
* @param {string} s
* @return {number}
*/
/**
* 시간 복잡도: O(n)
* 공간 복잡도: O(n)
*/
var numDecodings2 = function (s) {
const memo = new Map();
memo.set(s.length, 1);

function dfs(start) {
if (memo.has(start)) {
return memo.get(start);
}

if (s[start] === "0") {
memo.set(start, 0);
} else if (
start + 1 < s.length &&
parseInt(s.slice(start, start + 2)) < 27
) {
memo.set(start, dfs(start + 1) + dfs(start + 2));
} else {
memo.set(start, dfs(start + 1));
}

return memo.get(start);
}

return dfs(0);
};
19 changes: 19 additions & 0 deletions maximum-subarray/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 시간복잡도: O(n)
* 시간: 4ms
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function (nums) {
let maxResult = nums[0];
let curResult = nums[0];

for (let i = 1; i < nums.length; i += 1) {
const num = nums[i];
// 지금까지 누적합 vs 현재 날짜
result = Math.max(curResult + num, num);
maxResult = Math.max(maxResult, curResult);
}

return maxResult;
};
23 changes: 23 additions & 0 deletions number-of-1-bits/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 시간복잡도: O(n)
* 공간복잡도: O(1)
* @param {number} n
* @return {number}
*/
var hammingWeight = function (n) {
let num = n;

let count = 0;

while (num > 0) {
const left = num % 2;

if (left === 1) {
count += 1;
}

num = Math.floor(num / 2);
}

return count;
};
27 changes: 27 additions & 0 deletions valid-palindrome/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 시간복잡도: O(N)
* 공간복잡도: O(1)
* @param {string} s
* @return {boolean}
*/
const isPalindrome = function (s) {
const parsedString = s
.trim()
.replace(" ", "")
.replace(/[^a-zA-Z0-9]/g, "")
.toLowerCase();

let left = 0;
let right = parsedString.length - 1;

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

left += 1;
right -= 1;
}

return true;
};