-
-
Notifications
You must be signed in to change notification settings - Fork 195
[soobing] WEEK08 Solutions #1497
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 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6cbb599
feat(soobing): week8 > reverse-bits
soobing 2ec1155
feat(soobing): week8 > longest-repeating-character-replacement
soobing b85d795
feat(soobing): week8 > clone-graph
soobing b8a8823
feat(soobing): week8 > palindromic-substrings
soobing 7409de1
feat(soobing): week8 > longest-common-subsequence
soobing 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,84 @@ | ||
/** | ||
* 문제 설명 | ||
* - 그래프를 탐색하면서 완전 복제하는 문제 | ||
* | ||
* 아이디어 | ||
* 1) 그래프 탐색 알고리즘 | ||
* - 그래프 복사는 네트워크 유형에 해당하므로 bfs 혹은 dfs로 풀이한다. | ||
*/ | ||
|
||
/** | ||
* Definition for _Node. | ||
* class _Node { | ||
* val: number | ||
* neighbors: _Node[] | ||
* | ||
* constructor(val?: number, neighbors?: _Node[]) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.neighbors = (neighbors===undefined ? [] : neighbors) | ||
* } | ||
* } | ||
* | ||
*/ | ||
|
||
class _Node { | ||
val: number; | ||
neighbors: _Node[]; | ||
|
||
constructor(val?: number, neighbors?: _Node[]) { | ||
this.val = val === undefined ? 0 : val; | ||
this.neighbors = neighbors === undefined ? [] : neighbors; | ||
} | ||
} | ||
|
||
function cloneGraph(node: _Node | null): _Node | null { | ||
if (node === null) return null; | ||
|
||
const visited = new Map<_Node, _Node>(); | ||
|
||
function dfs(cur: _Node): _Node { | ||
if (visited.has(cur)) { | ||
return visited.get(cur)!; | ||
} | ||
|
||
const cloned = new _Node(cur.val); | ||
visited.set(cur, cloned); | ||
|
||
for (const neighbor of cur.neighbors) { | ||
cloned.neighbors.push(dfs(neighbor)); | ||
} | ||
|
||
return cloned; | ||
} | ||
|
||
return dfs(node); | ||
} | ||
|
||
/** | ||
* BFS 풀이 | ||
function cloneGraph(node: _Node | null): _Node | null { | ||
if(!node) return null; | ||
|
||
const visited = new Map<_Node, _Node>(); | ||
|
||
const cloneStart = new _Node(node.val); | ||
visited.set(node, cloneStart); | ||
|
||
const queue: _Node[] = [node]; | ||
|
||
while(queue.length > 0) { | ||
const cur = queue.shift(); | ||
const curClone = visited.get(cur); | ||
|
||
for(const neighbor of cur.neighbors) { | ||
if(!visited.get(neighbor)) { | ||
visited.set(neighbor, new _Node(neighbor.val)); | ||
queue.push(neighbor); | ||
} | ||
|
||
curClone.neighbors.push(visited.get(neighbor)); | ||
} | ||
} | ||
return cloneStart; | ||
}; | ||
*/ |
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,34 @@ | ||
/** | ||
* 문제 설명 | ||
* - k번 문자 대체를 통해 가장 길게 반복되는 문자열 찾기 | ||
* | ||
* 아이디어 | ||
* 1) Sliding Window 활용 | ||
* - 투포인터 알고리즘을 활용하여 현재 윈도우 내에서 k번 문자 대체가 가능한지 체크하고, | ||
* 윈도우 크기를 조절하면서 최대 길이를 찾는다. | ||
* | ||
* 시간 복잡도 | ||
* - O(n) | ||
* - 문자열을 한번 순회하면서 윈도우 크기를 조절하기 때문에 O(n)의 시간 복잡도를 가진다. | ||
* | ||
* 공간 복잡도 | ||
* - O(1) | ||
*/ | ||
|
||
function characterReplacement(s: string, k: number): number { | ||
let start = 0; | ||
const map: Record<string, number> = {}; | ||
let maxLength = 0; | ||
let maxCount = 0; | ||
for (let end = 0; end < s.length; end++) { | ||
map[s[end]] = (map[s[end]] || 0) + 1; | ||
maxCount = Math.max(maxCount, map[s[end]]); | ||
while (end - start + 1 - maxCount > k) { | ||
map[s[start]]--; | ||
start++; | ||
} | ||
|
||
maxLength = Math.max(end - start + 1, maxLength); | ||
} | ||
return maxLength; | ||
} |
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,22 @@ | ||
/** | ||
* | ||
* 문제 설명 | ||
* - 32비트 unsigned 정수의 비트 순서를 뒤집어서 다시 정수로 반환하는 문제 | ||
* | ||
* 아이디어 | ||
* - 비트 연산을 이해하고, O(1)알아가는데 의의를 두면 좋을 것 같음. | ||
* | ||
* 비트 연산 | ||
* >>>, >>, <<, &, | | ||
* - signed, unsigned 연산 | ||
*/ | ||
function reverseBits(n: number): number { | ||
let result = 0; | ||
for (let i = 0; i < 32; i++) { | ||
result <<= 1; | ||
result |= n & 1; | ||
n >>>= 1; | ||
} | ||
|
||
return result >>> 0; | ||
} |
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.
저는
toString()
,parseInt()
를 사용해서 진법 변환을 했는데, 이렇게 비트 연산을 활용한 정석 풀이를 보니까 공부가 되는 것 같습니다 👍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.
저도 처음에 그렇게 풀었다가 문제 의도가 궁금해서 알아보니 비트연산을 할 줄 아는지 였더라구요 😊 덕분에 다시한번 찾아보는 계기가 되었습니다~