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
24 changes: 24 additions & 0 deletions combination-sum/Cyjin-jani.js
Copy link
Contributor

Choose a reason for hiding this comment

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

백트래킹 쉽지 않네요. 저도..AI 도움을 많이 받았습니다. 🥲

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ! 나중에 다시 풀어야 할 문제
//! 30분 내에 풀지 못해서 AI의 도움을 받아 구현했습니다..
// recursion의 사용, 종료 조건 등 어느정도 근접한 아이디어를 구현했지만,
// idx를 넘기지 않고 number를 직접 넘기려고 했던 부분이나, results pop을 놓쳤던 부분 등이 있습니다.
const combinationSum = function (candidates, target) {
const answer = [];

function recursion(idx, target, results = []) {
if (target === 0) {
answer.push([...results]);
return;
}
if (target < 0) return;

for (let i = idx; i < candidates.length; i++) {
results.push(candidates[i]);
recursion(i, target - candidates[i], results);
results.pop();
}
}
recursion(0, target, []);

return answer;
};
57 changes: 57 additions & 0 deletions decode-ways/Cyjin-jani.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! 다시 풀어야 하는 문제

// Time limit 초과
const numDecodings_timeLimit = function (s) {
let answer = 0;

function recursion(idx, remains) {
if (idx === remains.length) {
answer++;
return;
}

const one = remains.slice(idx, idx + 1);
if (+one >= 1 && +one <= 9) {
recursion(idx + 1, remains);
}

if (idx + 1 < remains.length) {
const two = remains.slice(idx, idx + 2);
if (+two >= 10 && +two <= 26) {
recursion(idx + 2, remains);
}
}
}

recursion(0, s);

return answer;
};

// 메모이제이션 적용
const numDecodings = function (s) {
function recursion(idx, remains, memo = {}) {
if (idx === remains.length) return 1;

if (idx in memo) return memo[idx];

let answer = 0;

const one = remains.slice(idx, idx + 1);
if (+one >= 1 && +one <= 9) {
answer += recursion(idx + 1, remains, memo);
}

if (idx + 1 < remains.length) {
const two = remains.slice(idx, idx + 2);
if (+two >= 10 && +two <= 26) {
answer += recursion(idx + 2, remains, memo);
}
}

memo[idx] = answer;
return answer;
}

return recursion(0, s, {});
};
13 changes: 13 additions & 0 deletions maximum-subarray/Cyjin-jani.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! 다시 풀어보기 (divide and conquer)
const maxSubArray = function (nums) {
let currentSum = 0;
let maxSum = -Infinity;

for (let right = 0; right < nums.length; right++) {
currentSum += nums[right];
maxSum = Math.max(maxSum, currentSum);
if (currentSum < 0) currentSum = 0;
}

return maxSum;
};
29 changes: 29 additions & 0 deletions number-of-1-bits/Cyjin-jani.js
Copy link
Contributor

Choose a reason for hiding this comment

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

string으로 처음에 풀이하신 후,
10진수를 2진수로 변환하는 방식으로 최적화 하신 점 좋습니다.

이 문제를 비트를 조작해서 풀어보시는 것도 추천드립니다!
풀이 방식이 다양하고, 재미있습니다.

시간복잡도나 공간복잡도 면에서는 비트로 풀어도 동일합니다.
다만, 실행 측면에서는 나눗셈보다 비트가 더 유리합니다.

시간이 부족하시면, 달레님 블로그 풀이를 참고하셔도 좋을 듯 합니다. 😄

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,29 @@
// tc: O(logn)
// sc: O(logn)
const hammingWeight_simple = function (n) {
const binaryStr = n.toString(2);
let answer = 0;

for (char of binaryStr) {
if (+char === 1) {
answer++;
}
}

return answer;
};

// toString(2)를 쓰지 않고 처리
// tc: O(logn)
// sc: O(1)
const hammingWeight = function (n) {
let answer = 0;

while (n >= 0) {
const remains = n % 2;
if (remains === 1) answer++;
n = Math.floor(n / 2);
}

return answer;
};
41 changes: 41 additions & 0 deletions valid-palindrome/Cyjin-jani.js
Copy link
Contributor

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,41 @@
// naive한 풀이
// tc: O(n)
// sc: O(n)
const isPalindromeNaive = function (s) {
const str = s.toLowerCase().replace(/[^a-z0-9]/g, ''); // 이 부분에서 공간복잡도가 O(n)
let leftIdx = 0;
let rightIdx = str.length - 1;

while (leftIdx <= rightIdx) {
if (str[leftIdx] !== str[rightIdx]) {
return false;
} else {
leftIdx++;
rightIdx--;
}
}
return true;
};

// 좀 더 최적화 된 풀이
// tc: O(n)
// cs: O(1)
const isPalindrome = function (s) {
let leftIdx = 0;
let rightIdx = s.length - 1;

while (leftIdx < rightIdx) {
while (leftIdx < rightIdx && !isAlphaNumeric(s[leftIdx])) leftIdx++;
while (leftIdx < rightIdx && !isAlphaNumeric(s[rightIdx])) rightIdx--;

if (s[leftIdx].toLowerCase() !== s[rightIdx].toLowerCase()) return false;

leftIdx++;
rightIdx--;
}
return true;
};

function isAlphaNumeric(char) {
return /[a-zA-Z0-9]/.test(char);
}
Loading