-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aceb3ca
[ PS ] : Reverse Bits
uraflower dbda718
[ PS ] : Longest Repeating Character Replacement
uraflower 4c42c3e
[ PS ] : Clone Graph
uraflower 558ed4e
[ PS ] : Palindromic Substrings
uraflower c523446
[ PS ] : Longest Common Subsequence
uraflower File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 맵 + 재귀 호출 스택) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JS의 내장함수를 사용하지 않고 풀려면 어떻게 하면 좋을지 도전해보셔도 좋을것 같아요 :)