Skip to content

[youngduck] WEEK 03 solutions #1781

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 4 commits into from
Aug 11, 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
44 changes: 44 additions & 0 deletions combination-sum/youngduck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum = function (candidates, target) {
// 작은숫자 부터 더해서, 더 큰 숫자를 더해볼필요없이 미리 리턴시키기위한용도, 중복값도 방지가능
candidates.sort();
const result = [];

const sumArr = (arr) => {
if (arr.length === 0) return 0;
else {
const sum = arr.reduce((acc, cur) => acc + cur);

return sum;
}
};

const backtrack = (targetArr, startIndex) => {
const sumData = sumArr(targetArr);

if (sumData === target) {
result.push([...targetArr]);
return;
}

for (let i = startIndex; i < candidates.length; i++) {
if (sumData < target) {
backtrack([...targetArr, candidates[i]], i);
} else if (sumData > target) {
continue;
}
}
};

// 백트랙킹 방식으로 풀예정 (재귀)
// target보다 작을경우 계속 더함. 클경우 재귀멈춤 backtrack인수에
// targetArr만줬더니 result에 중복값이 생김. ex) 7을만들때[2,2,3],[2,3,2]같은 중복생김
// 자기자신 이상의 index를 타게하기위해서 startIndex
backtrack([], 0);

return result;
};
15 changes: 15 additions & 0 deletions number-of-1-bits/youngduck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @param {number} n
* @return {number}
*/
var hammingWeight = function (n) {
// 이진수 변환함수 시간복잡도: O(1)
const bin = n.toString(2);

// replace, replaceAll 시간복잡도: O(n)
const result = bin.replaceAll('0', '').length;

// 시간복잡도: O(n), 공간복잡도: O(1)
Copy link
Contributor

@sonjh1217 sonjh1217 Aug 6, 2025

Choose a reason for hiding this comment

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

오 replace 생각하지 못했는데 좋네용! 그리구 이진이라서 O(logn)이나.. 심지어 O(1) 으로도 풀이가 가능하더라구요. 다른 풀이들도 도전해보시면 좋을 것 같습니다.

Copy link
Contributor

Choose a reason for hiding this comment

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

내장 함수를 잘 사용해 주셨네요! 비트 연산으로도 풀어보시면 어떤 언어에서도 풀이 가능하니 도전해보시면 좋을것 같습니다!


return result;
};
20 changes: 20 additions & 0 deletions valid-palindrome/youngduck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function (s) {
// 1. toLowerCase() - O(n)
// 2. replace(/[^a-z0-9]/g, '') - O(n)
const clean = s.toLowerCase().replace(/[^a-z0-9]/g, '');

// 3. [...clean] 스프레드 연산자 - O(n)
// 4. reverse() - O(n)
// 5. join('') - O(n)
const reverse = [...clean].reverse().join('');

// 6. 문자열 비교 - O(n)
return clean === reverse;
};

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