-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Jeehay28] Week 07 Solutions #1464
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
+232
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1a4b697
Add reverse-linked-list solution in TypeScript
bf225cf
Add longest-substring-without-repeating-characters solution in TS
bd1540a
Add number-of-islands solution in TS
ab1e868
Add unique-paths solution in TypeScript
3a1c92d
Add set-matrix-zeros solution in TS
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
21 changes: 21 additions & 0 deletions
21
longest-substring-without-repeating-characters/Jeehay28.ts
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,21 @@ | ||
// TC: O(n) | ||
// SC: O(n) | ||
|
||
function lengthOfLongestSubstring(s: string): number { | ||
let seen = new Map<string, number>(); | ||
let maxLength = 0; | ||
let start = 0; | ||
|
||
for (let end = 0; end < s.length; end++) { | ||
const ch = s[end]; | ||
|
||
if (seen.has(ch) && seen.get(ch)! >= start) { | ||
start = seen.get(ch)! + 1; | ||
} | ||
|
||
seen.set(ch, end); | ||
maxLength = Math.max(maxLength, end - start + 1); | ||
} | ||
|
||
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,41 @@ | ||
// TC: O(n), where N = total number of cells = m * n | ||
// SC: O(n) | ||
|
||
function numIslands(grid: string[][]): number { | ||
const sink = (row: number, col: number) => { | ||
visited[row][col] = true; | ||
|
||
const dirs = [ | ||
[row - 1, col], | ||
[row + 1, col], | ||
[row, col - 1], | ||
[row, col + 1], | ||
]; | ||
|
||
for (const dir of dirs) { | ||
const [r, c] = dir; | ||
|
||
if (r >= 0 && r < grid.length && c >= 0 && c < grid[0].length) { | ||
if (!visited[r][c] && grid[r][c] === "1") { | ||
sink(r, c); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
let count = 0; | ||
const visited: boolean[][] = Array.from({ length: grid.length }, () => | ||
Array(grid[0].length).fill(false) | ||
); | ||
|
||
for (let i = 0; i < grid.length; i++) { | ||
for (let j = 0; j < grid[0].length; j++) { | ||
if (!visited[i][j] && grid[i][j] === "1") { | ||
count++; | ||
sink(i, j); // // Sink all connected neighboring land cells | ||
} | ||
} | ||
} | ||
|
||
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,34 @@ | ||
class ListNode { | ||
val: number; | ||
next: ListNode | null; | ||
constructor(val?: number, next?: ListNode | null) { | ||
this.val = val === undefined ? 0 : val; | ||
this.next = next === undefined ? null : next; | ||
} | ||
} | ||
|
||
// TC: O(n) | ||
// SC: O(1) | ||
function reverseList(head: ListNode | null): ListNode | null { | ||
if (!head) return head; | ||
|
||
// 1 -> 2 -> 3 -> 4 -> 5 -> null | ||
|
||
// null <- 1 | ||
// prev curr | ||
|
||
// null <- 1 <- 2 | ||
// prev cur | ||
|
||
let prev: ListNode | null = null; | ||
let curr: ListNode | null = head; | ||
|
||
while (curr) { | ||
const tempNext: ListNode | null = curr.next; // 2 -> 3 -> 4 -> 5 -> null | ||
curr.next = prev; // curr: 1 -> null | ||
prev = curr; // curr: 1 -> null, 2 -> 1 -> null | ||
curr = tempNext; | ||
} | ||
|
||
return prev; | ||
} |
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,95 @@ | ||
/** | ||
Do not return anything, modify matrix in-place instead. | ||
*/ | ||
|
||
// TC: O(m * n) | ||
// ✅ SC: O(1) | ||
function setZeroes(matrix: number[][]): void { | ||
let isFirstRowZero: boolean = false; | ||
let isFirstColZero: boolean = false; | ||
|
||
// check if the first row has any zeros | ||
for (let col = 0; col < matrix[0].length; col++) { | ||
if (matrix[0][col] === 0) { | ||
isFirstRowZero = true; | ||
break; | ||
} | ||
} | ||
|
||
// check if the first column has any zeros | ||
for (let row = 0; row < matrix.length; row++) { | ||
if (matrix[row][0] === 0) { | ||
isFirstColZero = true; | ||
break; | ||
} | ||
} | ||
|
||
// Use the first row and column to mark rows and columns that need to be zeroed | ||
for (let row = 1; row < matrix.length; row++) { | ||
for (let col = 1; col < matrix[0].length; col++) { | ||
if (matrix[row][col] === 0) { | ||
matrix[row][0] = 0; | ||
matrix[0][col] = 0; | ||
} | ||
} | ||
} | ||
|
||
// Set matrix cells to zero based on markers in the first row and column | ||
for (let row = 1; row < matrix.length; row++) { | ||
for (let col = 1; col < matrix[0].length; col++) { | ||
if (matrix[row][0] === 0 || matrix[0][col] === 0) { | ||
matrix[row][col] = 0; | ||
} | ||
} | ||
} | ||
|
||
// Zero out the first row if needed | ||
if (isFirstRowZero) { | ||
for (let col = 0; col < matrix[0].length; col++) { | ||
matrix[0][col] = 0; | ||
} | ||
} | ||
|
||
// Zero out the first column if needed | ||
if (isFirstColZero) { | ||
for (let row = 0; row < matrix.length; row++) { | ||
matrix[row][0] = 0; | ||
} | ||
} | ||
} | ||
|
||
|
||
// TC: O(m * n) | ||
// SC: O(m + n) | ||
/* | ||
function setZeroes(matrix: number[][]): void { | ||
|
||
const rows = new Set<number>(); | ||
const cols = new Set<number>(); | ||
|
||
// Identify all rows and columns that contain at least one zero | ||
for (let row = 0; row < matrix.length; row++) { | ||
for (let col = 0; col < matrix[0].length; col++) { | ||
if (matrix[row][col] === 0) { | ||
rows.add(row); | ||
cols.add(col); | ||
} | ||
} | ||
} | ||
|
||
// Set all elements in the identified rows to zero | ||
for (const row of rows) { | ||
for (let col = 0; col < matrix[0].length; col++) { | ||
matrix[row][col] = 0; | ||
} | ||
} | ||
|
||
// Set all elements in the identified columns to zero | ||
for (const col of cols) { | ||
for (let row = 0; row < matrix.length; row++) { | ||
matrix[row][col] = 0; | ||
} | ||
} | ||
}; | ||
*/ | ||
|
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,41 @@ | ||
// Memoized DFS | ||
// TC: O(m * n) | ||
// SC: O(m * n) | ||
function uniquePaths(m: number, n: number): number { | ||
const memo = new Map<string, number>(); | ||
|
||
const traverse = (row: number, col: number) => { | ||
if (row >= m || col >= n) return 0; | ||
if (row === m - 1 && col === n - 1) return 1; | ||
const key = `${row}-${col}`; | ||
if (memo.has(key)) return memo.get(key); | ||
|
||
const result = traverse(row + 1, col) + traverse(row, col + 1); | ||
memo.set(key, result); | ||
return result; | ||
}; | ||
|
||
return traverse(0, 0); | ||
} | ||
|
||
// DP | ||
// TC: O(m * n) | ||
// SC: O(m * n) | ||
/* | ||
function uniquePaths(m: number, n: number): number { | ||
|
||
// 1, 1, 1 | ||
// 1, 1+1=2, 1+(1+1)=3 | ||
|
||
const dp = Array.from({length: m}, () => Array(n).fill(1)); | ||
|
||
for(let i=1; i<m; i++) { | ||
for(let j=1; j<n; j++) { | ||
dp[i][j] = dp[i-1][j] + dp[i][j-1] | ||
} | ||
} | ||
|
||
return dp[m-1][n-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.
오 이부분에서 막혔었는데 이렇게 풀수있군요! 배워갑니다.