Skip to content

[uraflower] Week 08 Solutions #1498

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions clone-graph/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Definition for a _Node.
function _Node(val, neighbors) {
this.val = val === undefined ? 0 : val;
this.neighbors = neighbors === undefined ? [] : neighbors;
};

/**
* 그래프를 깊은 복사하여 반환하는 함수
* @param {_Node} node
* @return {_Node}
*/
const cloneGraph = function (node) {
if (!node) return null;

function dfs(node, visited) {
const current = new _Node(node.val);
visited.set(node, current);

node.neighbors.forEach((neighbor) => {
const clonedNeighbor = visited.has(neighbor) ? visited.get(neighbor) : dfs(neighbor, visited);
current.neighbors.push(clonedNeighbor);
});

return current;
}

return dfs(node, new Map()); // visited: 원본 노드를 key, 클론한 노드를 value로 하는 맵
};

// 시간복잡도: O(V + E) (모든 노드와 간선을 한 번씩 순회)
// 공간복잡도: O(V) (visited 맵 + 재귀 호출 스택)
36 changes: 36 additions & 0 deletions longest-common-subsequence/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 가장 긴 공통 부분 수열의 길이를 반환하는 함수
* @param {string} text1
* @param {string} text2
* @return {number}
*/
const longestCommonSubsequence = function (text1, text2) {
const dp = Array.from({ length: text1.length }, () => Array.from({ length: text2.length }, () => -1));

// text1, 2를 순회하는 포인터 i, j를 두고, 두 문자끼리 비교하는 함수
function dfs(i, j) {
// 포인터가 범위를 넘어가면 백트래킹
if (i === text1.length || j === text2.length) {
return 0;
}

// 두 문자를 이미 비교한 적 있는 경우 해당 결과 반환
if (dp[i][j] !== -1) {
return dp[i][j];
}

// 두 문자를 비교
if (text1[i] === text2[j]) {
dp[i][j] = 1 + dfs(i + 1, j + 1);
} else {
dp[i][j] = Math.max(dfs(i + 1, j), dfs(i, j + 1));
}

return dp[i][j];
}

return dfs(0, 0);
};

// 시간복잡도: O(m * n) (m: text1.length, n: text2.length)
// 공간복잡도: O(m * n) (재귀 호출 깊이: m + n, dp 배열 크기: m * n)
32 changes: 32 additions & 0 deletions longest-repeating-character-replacement/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 주어진 문자열에서 최대 k개를 대체해 가장 긴 동일 문자 반복 부분 문자열을 만들 수 있을 때,
* 이 문자열의 길이를 반환하는 함수
* @param {string} s
* @param {number} k
* @return {number}
*/
const characterReplacement = function(s, k) {
let start = 0;
let end = 0;
let counter = {};
let maxFrequent = 0; // 현재 구간에 가장 많이 포함되어 있는 알파벳의 총 개수
let maxLength = 0;

while (start <= end && end < s.length) {
counter[s[end]] = (counter[s[end]] || 0) + 1;
maxFrequent = Math.max(maxFrequent, counter[s[end]]);

while (end - start + 1 - maxFrequent > k) {
counter[s[start]]--;
start++;
}

maxLength = Math.max(end - start + 1, maxLength);
end++;
}

return maxLength;
};

// 시간복잡도: O(n)
// 공간복잡도: O(1)
26 changes: 26 additions & 0 deletions palindromic-substrings/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 회문인 부분 문자열의 개수를 반환하는 함수
* @param {string} s
* @return {number}
*/
const countSubstrings = function(s) {
let count = 0;

for (let i = 0; i <s.length; i++) {
let substr = '';
let reversed = '';

for (let j = i; j < s.length; j++){
substr += s[j];
reversed = s[j] + reversed;
if (substr === reversed) {
count += 1;
}
}
}

return count;
};

// 시간복잡도: O(n^2)
// 공간복잡도: O(n)
15 changes: 15 additions & 0 deletions reverse-bits/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* 주어진 32비트 unsingned integer를 뒤집어 십진수로 반환하는 함수
* @param {number} n - a positive integer
* @return {number} - a positive integer
*/
const reverseBits = function(n) {
const binary = n.toString(2).padStart(32, '0');
const reversed = Array.from(binary).reverse().join('');
const decimal = parseInt(reversed, 2).toString(10);

return Number(decimal);
};

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