Skip to content
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

[ganu] Week 07 #929

Merged
merged 7 commits into from
Jan 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
30 changes: 30 additions & 0 deletions longest-substring-without-repeating-characters/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// n: len(s)
// Time complexity: O(n^2)
// Space complexity: O(n)

/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function (s) {
let answer = 0;
const map = new Map();

for (let i = 0; i < s.length; i++) {
const chr = s[i];

if (map.has(chr)) {
const temp = map.get(chr);
for (const [key, value] of map) {
if (value <= temp) {
map.delete(key);
}
}
}

map.set(chr, i);
answer = Math.max(answer, map.size);
}

return answer;
};
71 changes: 71 additions & 0 deletions number-of-islands/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// w: width of grid, h: height of grid
// Time complexity: O(h*w)
// Space complexity: O(h*w)

class MyQueue {
constructor() {
this.q = [];
this.front = 0;
this.rear = 0;
}

get isEmpty() {
return this.front === this.rear;
}

push(value) {
this.q.push(value);
this.rear++;
}

pop() {
const value = this.q[this.front];
delete this.q[this.front++];
return value;
}
}

/**
* @param {character[][]} grid
* @return {number}
*/
var numIslands = function (grid) {
const h = grid.length;
const w = grid[0].length;

const dy = [1, 0, -1, 0];
const dx = [0, 1, 0, -1];

const bfs = (start) => {
const q = new MyQueue();
q.push(start);
const [sy, sx] = start;
grid[sy][sx] = "0";

while (!q.isEmpty) {
const [cy, cx] = q.pop();

for (let i = 0; i < dy.length; i++) {
const ny = cy + dy[i];
const nx = cx + dx[i];

if (ny >= 0 && ny < h && nx >= 0 && nx < w && grid[ny][nx] === "1") {
q.push([ny, nx]);
grid[ny][nx] = "0";
}
}
}
};

let answer = 0;
for (let i = 0; i < h; i++) {
for (let j = 0; j < w; j++) {
if (grid[i][j] === "1") {
answer++;
bfs([i, j]);
}
}
}

return answer;
};
27 changes: 27 additions & 0 deletions reverse-linked-list/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Time complexity: O(n)
// Space complexity: O(1)

/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function (head) {
let current = head;
let prev = null;

while (current) {
const node = new ListNode(current.val);
node.next = prev;
prev = node;
current = current.next;
}

return prev;
};
69 changes: 69 additions & 0 deletions set-matrix-zeroes/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// m: height of matrix, n: width of matrix
// Time complexity: O(m*n)
// Space complexity: O(1)

/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = function (matrix) {
const h = matrix.length;
const w = matrix[0].length;

// a flag for iterating rows of the first column
let temp1 = false;

// a flag for iterating cols of the first row
let temp2 = false;

for (let i = 0; i < h; i++) {
if (matrix[i][0] === 0) {
temp1 = true;
break;
}
}

for (let i = 0; i < w; i++) {
if (matrix[0][i] === 0) {
temp2 = true;
break;
}
}

for (let i = 1; i < h; i++) {
for (let j = 1; j < w; j++) {
if (matrix[i][j] === 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}

for (let i = 1; i < h; i++) {
if (matrix[i][0] === 0) {
for (let j = 1; j < w; j++) {
matrix[i][j] = 0;
}
}
}

for (let i = 1; i < w; i++) {
if (matrix[0][i] === 0) {
for (let j = 1; j < h; j++) {
matrix[j][i] = 0;
}
}
}

if (temp1) {
for (let i = 0; i < h; i++) {
matrix[i][0] = 0;
}
}

if (temp2) {
for (let j = 0; j < w; j++) {
matrix[0][j] = 0;
}
}
};
30 changes: 30 additions & 0 deletions unique-paths/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// m: height of grid, n: width of grid
// Time complexity: O(m*n)
// Space complexity: O(m*n)

/**
* @param {number} m
* @param {number} n
* @return {number}
*/
var uniquePaths = function (m, n) {
const dp = Array.from({ length: m }, () =>
Array.from({ length: n }, () => 0)
);

dp[0][0] = 1;

for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (i >= 1) {
dp[i][j] += dp[i - 1][j];
}

if (j >= 1) {
dp[i][j] += dp[i][j - 1];
}
}
}

return dp.at(-1).at(-1);
};
Loading