Skip to content

[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

Open
wants to merge 3 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
84 changes: 84 additions & 0 deletions clone-graph/soobing.ts
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;
};
*/
34 changes: 34 additions & 0 deletions longest-repeating-character-replacement/soobing.ts
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;
}
22 changes: 22 additions & 0 deletions reverse-bits/soobing.ts
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;
}