Skip to content

Commit e14024d

Browse files
authored
Merge pull request #1112 from gwbaik9717/main
[ganu] WEEK 15
2 parents 1d7e79d + c16a29d commit e14024d

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Time complexity: O(n^2)
2+
// Space complexity: O(n^2)
3+
4+
/**
5+
* @param {string} s
6+
* @return {string}
7+
*/
8+
var longestPalindrome = function (s) {
9+
const dp = Array.from({ length: s.length }, () =>
10+
Array.from({ length: s.length }, () => false)
11+
);
12+
13+
let answer = "";
14+
let start = 0;
15+
let end = 0;
16+
17+
const update = (i, j) => {
18+
const newLen = Math.abs(i - j) + 1;
19+
20+
if (newLen > end - start + 1) {
21+
start = i;
22+
end = j;
23+
}
24+
};
25+
26+
for (let i = s.length - 1; i >= 0; i--) {
27+
for (let j = i; j < s.length; j++) {
28+
if (i === j) {
29+
dp[i][j] = true;
30+
update(i, j);
31+
continue;
32+
}
33+
34+
if (i + 1 === j) {
35+
if (s[i] === s[j]) {
36+
dp[i][j] = true;
37+
update(i, j);
38+
}
39+
40+
continue;
41+
}
42+
43+
if (dp[i + 1][j - 1] && s[i] === s[j]) {
44+
dp[i][j] = true;
45+
update(i, j);
46+
}
47+
}
48+
}
49+
50+
return s.slice(start, end + 1);
51+
};

rotate-image/gwbaik9717.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time complexity: O(n^2)
2+
// Space complexity: O(1)
3+
4+
/**
5+
* @param {number[][]} matrix
6+
* @return {void} Do not return anything, modify matrix in-place instead.
7+
*/
8+
var rotate = function (matrix) {
9+
let top = 0;
10+
let bottom = matrix.length - 1;
11+
12+
while (top < bottom) {
13+
for (let i = 0; i < bottom - top; i++) {
14+
const left = top;
15+
const right = bottom;
16+
17+
const temp = matrix[top][left + i];
18+
19+
matrix[top][left + i] = matrix[bottom - i][left];
20+
matrix[bottom - i][left] = matrix[bottom][right - i];
21+
matrix[bottom][right - i] = matrix[top + i][right];
22+
matrix[top + i][right] = temp;
23+
}
24+
25+
top++;
26+
bottom--;
27+
}
28+
};
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// n: number of nodes in root, m: number of nodes in subroot
2+
// Time complexity: O(n*m)
3+
// Space complexity: O(n)
4+
5+
class _Queue {
6+
constructor() {
7+
this.q = [];
8+
this.front = 0;
9+
this.rear = 0;
10+
}
11+
12+
isEmpty() {
13+
return this.front === this.rear;
14+
}
15+
16+
push(value) {
17+
this.q.push(value);
18+
this.rear++;
19+
}
20+
21+
shift() {
22+
const rv = this.q[this.front];
23+
delete this.q[this.front++];
24+
return rv;
25+
}
26+
}
27+
/**
28+
* Definition for a binary tree node.
29+
* function TreeNode(val, left, right) {
30+
* this.val = (val===undefined ? 0 : val)
31+
* this.left = (left===undefined ? null : left)
32+
* this.right = (right===undefined ? null : right)
33+
* }
34+
*/
35+
/**
36+
* @param {TreeNode} root
37+
* @param {TreeNode} subRoot
38+
* @return {boolean}
39+
*/
40+
var isSubtree = function (root, subRoot) {
41+
const check = (root, subRoot) => {
42+
const q = new _Queue();
43+
q.push([root, subRoot]);
44+
45+
while (!q.isEmpty()) {
46+
const [node, subNode] = q.shift();
47+
48+
if (node.val !== subNode.val) {
49+
return false;
50+
}
51+
52+
if ((node.left && !subNode.left) || (!node.left && subNode.left)) {
53+
return false;
54+
}
55+
56+
if ((node.right && !subNode.right) || (!node.right && subNode.right)) {
57+
return false;
58+
}
59+
60+
if (node.left && subNode.left) {
61+
q.push([node.left, subNode.left]);
62+
}
63+
64+
if (node.right && subNode.right) {
65+
q.push([node.right, subNode.right]);
66+
}
67+
}
68+
69+
return true;
70+
};
71+
72+
const q = new _Queue();
73+
q.push(root);
74+
75+
while (!q.isEmpty()) {
76+
const current = q.shift();
77+
78+
if (check(current, subRoot)) {
79+
return true;
80+
}
81+
82+
if (current.left) {
83+
q.push(current.left);
84+
}
85+
86+
if (current.right) {
87+
q.push(current.right);
88+
}
89+
}
90+
91+
return false;
92+
};
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* function TreeNode(val, left, right) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.left = (left===undefined ? null : left)
9+
* this.right = (right===undefined ? null : right)
10+
* }
11+
*/
12+
/**
13+
* @param {TreeNode} root
14+
* @return {boolean}
15+
*/
16+
var isValidBST = function (root) {
17+
const dfs = (root) => {
18+
let minVal = root.val;
19+
let maxVal = root.val;
20+
21+
if (root.left) {
22+
if (root.left.val >= root.val) {
23+
return false;
24+
}
25+
26+
const result = dfs(root.left);
27+
28+
if (!result) {
29+
return false;
30+
}
31+
32+
const [min, max] = result;
33+
34+
if (max >= root.val) {
35+
return false;
36+
}
37+
38+
minVal = Math.min(minVal, min);
39+
maxVal = Math.max(maxVal, max);
40+
}
41+
42+
if (root.right) {
43+
if (root.right.val <= root.val) {
44+
return false;
45+
}
46+
47+
const result = dfs(root.right);
48+
49+
if (!result) {
50+
return false;
51+
}
52+
53+
const [min, max] = result;
54+
55+
if (min <= root.val) {
56+
return false;
57+
}
58+
59+
minVal = Math.min(minVal, min);
60+
maxVal = Math.max(maxVal, max);
61+
}
62+
63+
return [minVal, maxVal];
64+
};
65+
66+
if (dfs(root)) {
67+
return true;
68+
}
69+
70+
return false;
71+
};

0 commit comments

Comments
 (0)