diff --git a/longest-substring-without-repeating-characters/gwbaik9717.js b/longest-substring-without-repeating-characters/gwbaik9717.js new file mode 100644 index 000000000..02c599e33 --- /dev/null +++ b/longest-substring-without-repeating-characters/gwbaik9717.js @@ -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; +}; diff --git a/number-of-islands/gwbaik9717.js b/number-of-islands/gwbaik9717.js new file mode 100644 index 000000000..c4f3b14f8 --- /dev/null +++ b/number-of-islands/gwbaik9717.js @@ -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; +}; diff --git a/reverse-linked-list/gwbaik9717.js b/reverse-linked-list/gwbaik9717.js new file mode 100644 index 000000000..68dc8f9f1 --- /dev/null +++ b/reverse-linked-list/gwbaik9717.js @@ -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; +}; diff --git a/set-matrix-zeroes/gwbaik9717.js b/set-matrix-zeroes/gwbaik9717.js new file mode 100644 index 000000000..7643a4a10 --- /dev/null +++ b/set-matrix-zeroes/gwbaik9717.js @@ -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; + } + } +}; diff --git a/unique-paths/gwbaik9717.js b/unique-paths/gwbaik9717.js new file mode 100644 index 000000000..8fdeb464c --- /dev/null +++ b/unique-paths/gwbaik9717.js @@ -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); +};