Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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;
}
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];
};
*/