Skip to content
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
23 changes: 23 additions & 0 deletions combination-sum/JangAyeon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum = function (arr, target) {
const N = arr.length;
Copy link
Contributor

Choose a reason for hiding this comment

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

arr를 정렬한다면 백트래킹 시 pruning이 가능합니다!

const answer = [];
function dfs(total, idx, route) {
if (total >= target) {
if (total == target) {
Copy link
Contributor

Choose a reason for hiding this comment

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

현재는 total >= target 내부에서 total == target 조건을 한 번 더 검사하는데, total > target 조건과 total == target 조건으로 아예 분리하는 것이 가독성 측면에서 더 좋을 것 같아요!

answer.push(route);
}
return;
}
for (let i = idx; i < N; i++) {
dfs(total + arr[i], i, [...route, arr[i]]);
Copy link
Contributor

Choose a reason for hiding this comment

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

매 dfs 호출마다 배열을 복사하기 때문에 비용이 발생하는데요, route 배열을 dfs() 호출 앞뒤로 push() / pop() 함으로써 재사용한다면 메모리 및 성능 측면에서 더 효율적일 것 같습니다!

}
}

dfs(0, 0, []);
return answer;
};
23 changes: 23 additions & 0 deletions decode-ways/JangAyeon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var numDecodings = function (s) {
const N = s.length;
const memo = {};

function dfs(i) {
if (i === N) return 1;
if (s[i] === "0") return 0; // 0은 단독으로 decode 불가능
if (memo[i] !== undefined) return memo[i];

// 1자리
let count = dfs(i + 1);

// 2자리
if (i + 1 < N && Number(s.slice(i, i + 2)) <= 26) {
count += dfs(i + 2);
Copy link
Contributor

Choose a reason for hiding this comment

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

코멘트와 코드만으로도 명확하게 이해되는 깔끔한 풀이인 것 같습니다!!

추가로 현재 코드는 memoization + dfs를 이용한 top-down DP로 dfs(i)를 계산하기 위해 dfs(i + 1), dfs(i + 2)가 필요한데요,

저는 이를 bottom-up DP로 풀이하고, dp[i]를 계산할 때 dp[i - 1], dp[i - 2]만 확인하므로 DP 테이블을 O(1) space 변수 두 개로 대체하여 공간 복잡도를 최적화했습니다!

이렇게도 풀 수 있어서 참고차 공유드려요~!

}

memo[i] = count;
return count;
}

return dfs(0);
};
19 changes: 19 additions & 0 deletions maximum-subarray/JangAyeon.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 pointer = nums[0];
let answer = pointer;
const N = nums.length;
for (let idx = 1; idx < N; idx++) {
if (nums[idx] > pointer + nums[idx]) {
pointer = nums[idx];
} else {
pointer += nums[idx];
}
// console.log(idx, pointer)
answer = Math.max(pointer, answer);
}
return answer;
};
21 changes: 21 additions & 0 deletions number-of-1-bits/JangAyeon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {number} n
* @return {number}
*/
var hammingWeight = function (n) {
function calDivision(num) {
return { v: Math.floor(num / 2), rest: num % 2 };
}
let num = n;
let result = 0;
while (true) {
const { v, rest } = calDivision(num);
Copy link
Contributor

Choose a reason for hiding this comment

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

n & n-1로 오른쪽에서부터 1인 비트를 하나씩 지워가며 개수를 세는 방식인 Brian Kernighan's 알고리즘이라는 방법도 있어 공유드립니다~!

https://leetcode.com/problems/number-of-1-bits/solutions/4341511/faster-lesser-3-methods-simple-count-brian-kernighan-s-algorithm-bit-manipulation-explained

result += rest;
if (v == 0) {
break;
}
num = v;
}

return result;
};
20 changes: 20 additions & 0 deletions valid-palindrome/JangAyeon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function (s) {
const sLowered = [...s.toLowerCase().replace(" ", "")];
const isAlpha = (item) => item.charCodeAt() >= 97 && item.charCodeAt() <= 122;
const isNumber = (item) => item.charCodeAt() >= 48 && item.charCodeAt() <= 57;
const str = sLowered.filter((c) => isAlpha(c) || isNumber(c));
const N = str.length;
let [start, end] = [0, N - 1];
while (start <= end) {
if (str[start] != str[end]) {
return false;
}
start++;
end--;
}
return true;
};