Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
44 changes: 44 additions & 0 deletions combination-sum/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 주어진 배열의 원소 조합(중복 허용)의 합이 target인 모든 경우를 반환하는 함수
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
const combinationSum = function(candidates, target) {
const sortedCandidates = candidates.filter((x) => x <= target).sort((a, b) => Number(a) - Number(b));
const answer = [];

if (sortedCandidates.length === 0) {
return answer;
}

function search(currentIdx, combination, total) {
if (total === target) {
answer.push([...combination]); // 배열 자체를 넣으면 참조 때문에 값이 변경되므로, 복사해서 넣어야 함
return;
}

if (total > target) {
return; // backtracking
}

combination.push(sortedCandidates[currentIdx]);
search(currentIdx, combination, total + sortedCandidates[currentIdx]);
combination.pop();

if (total + sortedCandidates[currentIdx] > target) {
return; // backtracking
}

if (currentIdx + 1 < sortedCandidates.length) {
search(currentIdx + 1, combination, total);
}
}

search(0, [], 0);
return answer;
};

// t: target
// 시간복잡도: O(2^t)
// 공간복잡도: O(t)
Comment on lines +42 to +44
Copy link
Contributor Author

Choose a reason for hiding this comment

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

복잡도 계산이 올바른지 잘 모르겠습니다..!

Copy link
Contributor

@byol-han byol-han Apr 16, 2025

Choose a reason for hiding this comment

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

제가 알고리즘 풀이가 처음이라 복잡도 계산을 잘 모릅니다... 같이 해드릴수가 없어서 죄송합니다ㅠㅠ

12 changes: 12 additions & 0 deletions number-of-1-bits/uraflower.js
Copy link
Contributor

Choose a reason for hiding this comment

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

문자열을 배열로 변환해서 filter하시는 아이디어가 참신해요!
uraflower님의 풀이 보고 저도 열심히 공부하고 있습니다.
3주차도 끝까지 화이팅입니다 :)

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 주어진 숫자를 2진수로 표현했을 때 비트가 1인 개수를 반환하는 함수
* @param {number} n
* @return {number}
*/
const hammingWeight = function(n) {
const binary = n.toString(2);
return Array.from(binary).filter((bit) => bit == 1).length;
};

// 시간복잡도: O(n)
// 공간복잡도: O(n)
26 changes: 26 additions & 0 deletions valid-palindrome/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 주어진 문자열이 조건을 만족하는 회문인지 여부를 반환하는 함수
* @param {string} s
* @return {boolean}
*/
const isPalindrome = function(s) {
const filtered = Array.from(s.toLowerCase()).reduce((str, char) => {
return isAlphanumeric(char) ? str + char : str;
}, '');

for (let left = 0, right = filtered.length - 1; left < right; left++, right--) {
if (filtered[left] !== filtered[right]) {
return false;
}
}

return true;
};


function isAlphanumeric(char) {
return char !== ' ' && (('a' <= char && char <= 'z') || !Number.isNaN(Number(char)));
}

// 시간복잡도: O(n)
// 공간복잡도: O(n)