Skip to content

[uraflower] WEEK 03 solutions #1285

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 5 commits into from
Apr 18, 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/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.

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

36 changes: 36 additions & 0 deletions decode-ways/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 주어진 문자열을 복호화하는 경우의 수를 반환하는 함수
* @param {string} s
* @return {number}
*/
const numDecodings = function(s) {
const dp = {};

function decode(idx) {
if (s[idx] === '0') {
return 0;
}

if (idx === s.length) {
return 1;
}

if (dp[idx]) {
return dp[idx];
}

let result = 0;
result += decode(idx + 1); // 현재 문자만 쓰는 경우: 다음 문자부터 탐색
if (s[idx + 1] && Number(s[idx] + s[idx+1]) <= 26) {
result += decode(idx + 2); // 현재 문자와 다음 문자 붙여서 쓰는 경우: 다다음 문자부터 탐색
}

dp[idx] = result;
return result;
}

return decode(0);
};

// 시간복잡도: O(n) (메모이제이션 안하면 매 인덱스마다 최대 2개의 하위 호출이 발생하여 O(2^n))
// 공간복잡도: O(n)
22 changes: 22 additions & 0 deletions maximum-subarray/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 주어진 배열에서 원소의 합이 가장 큰 부분 배열의 합을 반환하는 함수
* @param {number[]} nums
* @return {number}
*/
const maxSubArray = function(nums) {
let max = nums[0];
let subSum = 0;

nums.forEach((num) => {
subSum = Math.max(subSum + num, num);

if (max < subSum) {
max = subSum;
}
});

return max;
};

// 시간복잡도: O(n)
// 공간복잡도: O(1)
12 changes: 12 additions & 0 deletions number-of-1-bits/uraflower.js
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)