Skip to content

Commit 21c512f

Browse files
authored
Merge pull request #929 from gwbaik9717/main
2 parents 5d393ea + 12c3b94 commit 21c512f

File tree

5 files changed

+227
-0
lines changed

5 files changed

+227
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// n: len(s)
2+
// Time complexity: O(n^2)
3+
// Space complexity: O(n)
4+
5+
/**
6+
* @param {string} s
7+
* @return {number}
8+
*/
9+
var lengthOfLongestSubstring = function (s) {
10+
let answer = 0;
11+
const map = new Map();
12+
13+
for (let i = 0; i < s.length; i++) {
14+
const chr = s[i];
15+
16+
if (map.has(chr)) {
17+
const temp = map.get(chr);
18+
for (const [key, value] of map) {
19+
if (value <= temp) {
20+
map.delete(key);
21+
}
22+
}
23+
}
24+
25+
map.set(chr, i);
26+
answer = Math.max(answer, map.size);
27+
}
28+
29+
return answer;
30+
};

number-of-islands/gwbaik9717.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// w: width of grid, h: height of grid
2+
// Time complexity: O(h*w)
3+
// Space complexity: O(h*w)
4+
5+
class MyQueue {
6+
constructor() {
7+
this.q = [];
8+
this.front = 0;
9+
this.rear = 0;
10+
}
11+
12+
get isEmpty() {
13+
return this.front === this.rear;
14+
}
15+
16+
push(value) {
17+
this.q.push(value);
18+
this.rear++;
19+
}
20+
21+
pop() {
22+
const value = this.q[this.front];
23+
delete this.q[this.front++];
24+
return value;
25+
}
26+
}
27+
28+
/**
29+
* @param {character[][]} grid
30+
* @return {number}
31+
*/
32+
var numIslands = function (grid) {
33+
const h = grid.length;
34+
const w = grid[0].length;
35+
36+
const dy = [1, 0, -1, 0];
37+
const dx = [0, 1, 0, -1];
38+
39+
const bfs = (start) => {
40+
const q = new MyQueue();
41+
q.push(start);
42+
const [sy, sx] = start;
43+
grid[sy][sx] = "0";
44+
45+
while (!q.isEmpty) {
46+
const [cy, cx] = q.pop();
47+
48+
for (let i = 0; i < dy.length; i++) {
49+
const ny = cy + dy[i];
50+
const nx = cx + dx[i];
51+
52+
if (ny >= 0 && ny < h && nx >= 0 && nx < w && grid[ny][nx] === "1") {
53+
q.push([ny, nx]);
54+
grid[ny][nx] = "0";
55+
}
56+
}
57+
}
58+
};
59+
60+
let answer = 0;
61+
for (let i = 0; i < h; i++) {
62+
for (let j = 0; j < w; j++) {
63+
if (grid[i][j] === "1") {
64+
answer++;
65+
bfs([i, j]);
66+
}
67+
}
68+
}
69+
70+
return answer;
71+
};

reverse-linked-list/gwbaik9717.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(1)
3+
4+
/**
5+
* Definition for singly-linked list.
6+
* function ListNode(val, next) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
*/
11+
/**
12+
* @param {ListNode} head
13+
* @return {ListNode}
14+
*/
15+
var reverseList = function (head) {
16+
let current = head;
17+
let prev = null;
18+
19+
while (current) {
20+
const node = new ListNode(current.val);
21+
node.next = prev;
22+
prev = node;
23+
current = current.next;
24+
}
25+
26+
return prev;
27+
};

set-matrix-zeroes/gwbaik9717.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// m: height of matrix, n: width of matrix
2+
// Time complexity: O(m*n)
3+
// Space complexity: O(1)
4+
5+
/**
6+
* @param {number[][]} matrix
7+
* @return {void} Do not return anything, modify matrix in-place instead.
8+
*/
9+
var setZeroes = function (matrix) {
10+
const h = matrix.length;
11+
const w = matrix[0].length;
12+
13+
// a flag for iterating rows of the first column
14+
let temp1 = false;
15+
16+
// a flag for iterating cols of the first row
17+
let temp2 = false;
18+
19+
for (let i = 0; i < h; i++) {
20+
if (matrix[i][0] === 0) {
21+
temp1 = true;
22+
break;
23+
}
24+
}
25+
26+
for (let i = 0; i < w; i++) {
27+
if (matrix[0][i] === 0) {
28+
temp2 = true;
29+
break;
30+
}
31+
}
32+
33+
for (let i = 1; i < h; i++) {
34+
for (let j = 1; j < w; j++) {
35+
if (matrix[i][j] === 0) {
36+
matrix[i][0] = 0;
37+
matrix[0][j] = 0;
38+
}
39+
}
40+
}
41+
42+
for (let i = 1; i < h; i++) {
43+
if (matrix[i][0] === 0) {
44+
for (let j = 1; j < w; j++) {
45+
matrix[i][j] = 0;
46+
}
47+
}
48+
}
49+
50+
for (let i = 1; i < w; i++) {
51+
if (matrix[0][i] === 0) {
52+
for (let j = 1; j < h; j++) {
53+
matrix[j][i] = 0;
54+
}
55+
}
56+
}
57+
58+
if (temp1) {
59+
for (let i = 0; i < h; i++) {
60+
matrix[i][0] = 0;
61+
}
62+
}
63+
64+
if (temp2) {
65+
for (let j = 0; j < w; j++) {
66+
matrix[0][j] = 0;
67+
}
68+
}
69+
};

unique-paths/gwbaik9717.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// m: height of grid, n: width of grid
2+
// Time complexity: O(m*n)
3+
// Space complexity: O(m*n)
4+
5+
/**
6+
* @param {number} m
7+
* @param {number} n
8+
* @return {number}
9+
*/
10+
var uniquePaths = function (m, n) {
11+
const dp = Array.from({ length: m }, () =>
12+
Array.from({ length: n }, () => 0)
13+
);
14+
15+
dp[0][0] = 1;
16+
17+
for (let i = 0; i < m; i++) {
18+
for (let j = 0; j < n; j++) {
19+
if (i >= 1) {
20+
dp[i][j] += dp[i - 1][j];
21+
}
22+
23+
if (j >= 1) {
24+
dp[i][j] += dp[i][j - 1];
25+
}
26+
}
27+
}
28+
29+
return dp.at(-1).at(-1);
30+
};

0 commit comments

Comments
 (0)