Skip to content

[hoyeongkwak] Week 8 Solutions #1513

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 1 commit into from
May 24, 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
35 changes: 35 additions & 0 deletions clone-graph/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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)
* }
* }
*
*/

/*
*/
function cloneGraph(node: _Node | null): _Node | null {
const clones = new Map<_Node, _Node>()
const dfs = (n: _Node) => {
if (n === null) return n

if (clones.has(n)) {
return clones.get(n)
}

const copy = new _Node(n.val)
clones.set(n, copy)

for (const neighbor of n.neighbors) {
copy.neighbors.push(dfs(neighbor))
}
return copy
}
return dfs(node)
};
17 changes: 17 additions & 0 deletions longest-common-subsequence/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function longestCommonSubsequence(text1: string, text2: string): number {
const m1 = text1.length
const m2 = text2.length

const dp: number[][] = Array(m1 + 1).fill(null).map(() => Array(m2 + 1).fill(0))

for (let r = 1; r <= m1; r++) {
for (let c = 1; c <= m2; c++) {
if (text1[r - 1] === text2[c - 1]) {
dp[r][c] = dp[r - 1][c - 1] + 1
} else {
dp[r][c] = Math.max(dp[r - 1][c], dp[r][c - 1])
}
}
}
return dp[m1][m2]
};
19 changes: 19 additions & 0 deletions longest-repeating-character-replacement/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function characterReplacement(s: string, k: number): number {
/*

*/
let maxLen = 0
let maxCnt = new Map<string, number>()
let start = 0
let maxFreq = 0
for (let e = 0; e < s.length; e++) {
maxCnt.set(s[e], (maxCnt.get(s[e]) || 0) + 1)
maxFreq = Math.max(maxFreq, maxCnt.get(s[e]))
while (e - start + 1 - maxFreq > k) {
maxCnt.set(s[start], maxCnt.get(s[start]) - 1)
start += 1
}
maxLen = Math.max(e - start + 1, maxLen)
}
return maxLen
};
20 changes: 20 additions & 0 deletions palindromic-substrings/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function countSubstrings(s: string): number {
const sLen = s.length;
const dp = Array(sLen).fill(null).map(() => Array(sLen).fill(false))
let cnt = 0

for (let i = sLen - 1; i >= 0; i--) {
for (let j = i; j < sLen; j++) {
if (i === j) {
dp[i][j] = true
} else if (j === i + 1) {
dp[i][j] = (s[i] === s[j])
} else {
dp[i][j] = (s[i] === s[j]) && dp[i + 1][j - 1]
}

if (dp[i][j]) cnt++
}
}
return cnt
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이것도 dp 방식으로 풀 수 있군요!!
공간 복잡도가 생기겠지만 2중 반복보다는 속도가 빠를 것 같네요!

5 changes: 5 additions & 0 deletions reverse-bits/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function reverseBits(n: number): number {
const binaryStr = n.toString(2).padStart(32, '0')
const reverseNum = binaryStr.split('').reverse().join('')
return parseInt(reverseNum, 2)
};