Skip to content

[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
merged 5 commits into from
May 16, 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
21 changes: 21 additions & 0 deletions longest-substring-without-repeating-characters/Jeehay28.ts
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;
}
41 changes: 41 additions & 0 deletions number-of-islands/Jeehay28.ts
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;
}
34 changes: 34 additions & 0 deletions reverse-linked-list/Jeehay28.ts
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;
}
95 changes: 95 additions & 0 deletions set-matrix-zeroes/Jeehay28.ts
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;
}
}
};
*/

41 changes: 41 additions & 0 deletions unique-paths/Jeehay28.ts
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);
}
Comment on lines +4 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

오 이부분에서 막혔었는데 이렇게 풀수있군요! 배워갑니다.


// 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];
};
*/