Skip to content

[ganu] WEEK 15 #1112

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

Merged
merged 4 commits into from
Mar 22, 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
51 changes: 51 additions & 0 deletions longest-palindromic-substring/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Time complexity: O(n^2)
// Space complexity: O(n^2)

/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function (s) {
const dp = Array.from({ length: s.length }, () =>
Array.from({ length: s.length }, () => false)
);

let answer = "";
let start = 0;
let end = 0;

const update = (i, j) => {
const newLen = Math.abs(i - j) + 1;

if (newLen > end - start + 1) {
start = i;
end = j;
}
};

for (let i = s.length - 1; i >= 0; i--) {
for (let j = i; j < s.length; j++) {
if (i === j) {
dp[i][j] = true;
update(i, j);
continue;
}

if (i + 1 === j) {
if (s[i] === s[j]) {
dp[i][j] = true;
update(i, j);
}

continue;
}

if (dp[i + 1][j - 1] && s[i] === s[j]) {
dp[i][j] = true;
update(i, j);
}
}
}

return s.slice(start, end + 1);
};
28 changes: 28 additions & 0 deletions rotate-image/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Time complexity: O(n^2)
// Space complexity: O(1)

/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var rotate = function (matrix) {
let top = 0;
let bottom = matrix.length - 1;

while (top < bottom) {
for (let i = 0; i < bottom - top; i++) {
const left = top;
const right = bottom;

const temp = matrix[top][left + i];

matrix[top][left + i] = matrix[bottom - i][left];
matrix[bottom - i][left] = matrix[bottom][right - i];
matrix[bottom][right - i] = matrix[top + i][right];
matrix[top + i][right] = temp;
}

top++;
bottom--;
}
};
92 changes: 92 additions & 0 deletions subtree-of-another-tree/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// n: number of nodes in root, m: number of nodes in subroot
// Time complexity: O(n*m)
// Space complexity: O(n)

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

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

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

shift() {
const rv = this.q[this.front];
delete this.q[this.front++];
return rv;
}
}
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} subRoot
* @return {boolean}
*/
var isSubtree = function (root, subRoot) {
const check = (root, subRoot) => {
const q = new _Queue();
q.push([root, subRoot]);

while (!q.isEmpty()) {
const [node, subNode] = q.shift();

if (node.val !== subNode.val) {
return false;
}

if ((node.left && !subNode.left) || (!node.left && subNode.left)) {
return false;
}

if ((node.right && !subNode.right) || (!node.right && subNode.right)) {
return false;
}

if (node.left && subNode.left) {
q.push([node.left, subNode.left]);
}

if (node.right && subNode.right) {
q.push([node.right, subNode.right]);
}
}

return true;
};

const q = new _Queue();
q.push(root);

while (!q.isEmpty()) {
const current = q.shift();

if (check(current, subRoot)) {
return true;
}

if (current.left) {
q.push(current.left);
}

if (current.right) {
q.push(current.right);
}
}

return false;
};
71 changes: 71 additions & 0 deletions validate-binary-search-tree/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Time complexity: O(n)
// Space complexity: O(n)

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isValidBST = function (root) {
const dfs = (root) => {
let minVal = root.val;
let maxVal = root.val;

if (root.left) {
if (root.left.val >= root.val) {
return false;
}

const result = dfs(root.left);

if (!result) {
return false;
}

const [min, max] = result;

if (max >= root.val) {
return false;
}

minVal = Math.min(minVal, min);
maxVal = Math.max(maxVal, max);
}

if (root.right) {
if (root.right.val <= root.val) {
return false;
}

const result = dfs(root.right);

if (!result) {
return false;
}

const [min, max] = result;

if (min <= root.val) {
return false;
}

minVal = Math.min(minVal, min);
maxVal = Math.max(maxVal, max);
}

return [minVal, maxVal];
};

if (dfs(root)) {
return true;
}

return false;
};