-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hyer0705] WEEK 08 solutions #1896
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
d3aec9c
reverse bits solution
hyer0705 d679ed8
longest repeating character replacement solution
hyer0705 ce0eb2e
clone graph solution
hyer0705 b7ff251
palindromic substrings solution
hyer0705 471cb16
longest common subsequence solution
hyer0705 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,46 @@ | ||
/** | ||
* 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 { | ||
if (!node) return null; | ||
|
||
const cloned = new Map<number, _Node>(); | ||
|
||
const queue: _Node[] = []; | ||
|
||
const copied = new _Node(node.val); | ||
cloned.set(node.val, copied); | ||
|
||
queue.push(node); | ||
|
||
let pointer = 0; | ||
|
||
while (pointer < queue.length) { | ||
const current = queue[pointer++]; | ||
|
||
const copiedNode = cloned.get(current.val)!; | ||
|
||
for (const neighbor of current.neighbors) { | ||
if (!cloned.has(neighbor.val)) { | ||
const copiedNeighbor = new _Node(neighbor.val); | ||
cloned.set(neighbor.val, copiedNeighbor); | ||
|
||
queue.push(neighbor); | ||
} | ||
copiedNode.neighbors.push(cloned.get(neighbor.val)!); | ||
} | ||
} | ||
|
||
return copied; | ||
} |
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,18 @@ | ||
function longestCommonSubsequence(text1: string, text2: string): number { | ||
const m = text1.length; | ||
const n = text2.length; | ||
|
||
const dp: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); | ||
|
||
for (let i = 1; i <= m; i++) { | ||
for (let j = 1; j <= n; j++) { | ||
if (text1[i - 1] === text2[j - 1]) { | ||
dp[i][j] = dp[i - 1][j - 1] + 1; | ||
} else { | ||
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); | ||
} | ||
} | ||
} | ||
|
||
return 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,22 @@ | ||
function characterReplacement(s: string, k: number): number { | ||
let longestLength = 0; | ||
let windowStart = 0; | ||
|
||
const countMap = new Map<string, number>(); | ||
let maxCount = 0; | ||
|
||
for (let windowEnd = 0; windowEnd < s.length; windowEnd++) { | ||
const currentCh = s[windowEnd]; | ||
countMap.set(currentCh, (countMap.get(currentCh) || 0) + 1); | ||
|
||
maxCount = Math.max(maxCount, countMap.get(currentCh) || 0); | ||
if (windowEnd - windowStart + 1 - maxCount > k) { | ||
countMap.set(s[windowStart], (countMap.get(s[windowStart]) || 0) - 1); | ||
|
||
windowStart++; | ||
} | ||
longestLength = Math.max(longestLength, windowEnd - windowStart + 1); | ||
} | ||
|
||
return longestLength; | ||
} |
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 @@ | ||
function countSubstrings(s: string): number { | ||
const sLen = s.length; | ||
|
||
const dp: boolean[][] = Array.from({ length: sLen }, () => Array(sLen).fill(false)); | ||
|
||
// 길이가 1 | ||
for (let i = 0; i < sLen; i++) { | ||
dp[i][i] = true; | ||
} | ||
|
||
// 길이가 2 | ||
for (let i = 0; i < sLen - 1; i++) { | ||
dp[i][i + 1] = s[i] === s[i + 1]; | ||
} | ||
|
||
// 길이가 3이상 | ||
for (let len = 3; len <= sLen; len++) { | ||
for (let i = 0; i < sLen - len + 1; i++) { | ||
const j = i + len - 1; | ||
if (s[i] === s[j] && dp[i + 1][j - 1]) { | ||
dp[i][j] = true; | ||
} | ||
} | ||
} | ||
|
||
let count = 0; | ||
for (let i = 0; i < dp.length; i++) { | ||
for (let j = 0; j < dp[i].length; j++) { | ||
if (dp[i][j]) count++; | ||
} | ||
} | ||
|
||
return count; | ||
} |
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,45 @@ | ||
// 8비트로 쪼갠 방법 | ||
function reverseBits(n: number): number { | ||
const TOTAL_BITS = 32; | ||
const BITS_IN_BYTE = 8; | ||
const BYTES_IN_INTEGER = TOTAL_BITS / BITS_IN_BYTE; | ||
const BYTE_MASK = 0xff; | ||
|
||
let result = 0; | ||
|
||
const reversedByteCache: number[] = Array.from({ length: 2 ** BITS_IN_BYTE }, (_, n) => { | ||
let reversedNum = 0; | ||
|
||
for (let i = 0; i < BITS_IN_BYTE; i++) { | ||
const bit = (n >>> i) & 1; | ||
reversedNum |= bit << (BITS_IN_BYTE - 1 - i); | ||
} | ||
|
||
return reversedNum; | ||
}); | ||
|
||
for (let i = 0; i < BYTES_IN_INTEGER; i++) { | ||
const byte = (n >>> (i * BITS_IN_BYTE)) & BYTE_MASK; | ||
|
||
const reversed = reversedByteCache[byte]; | ||
result |= reversed << (TOTAL_BITS - (i + 1) * BITS_IN_BYTE); | ||
} | ||
|
||
return result >>> 0; | ||
} | ||
|
||
// 32번 연산 방법 | ||
function reverseBits(n: number): number { | ||
const BITS_LEN = 32; | ||
|
||
let reversed = 0; | ||
|
||
for (let i = 0; i < BITS_LEN; i++) { | ||
const bit = (n >>> i) & 1; | ||
|
||
const reversedPosition = 31 - i; | ||
reversed |= bit << reversedPosition; | ||
hyer0705 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return reversed >>> 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.
Uh oh!
There was an error while loading. Please reload this page.