Skip to content

Commit d10783f

Browse files
committed
save notes
1 parent 12581d4 commit d10783f

14 files changed

+332
-34
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { TreeNode } from '../utils/nodes';
2+
3+
function diameterOfBinaryTree(root: TreeNode | null): number {
4+
let max = 0;
5+
function dfs(root: TreeNode | null): number {
6+
if (!root) return 0;
7+
const l = dfs(root.left),
8+
r = dfs(root.right);
9+
max = Math.max(max, l + r);
10+
return Math.max(l, r) + 1;
11+
}
12+
dfs(root);
13+
return max;
14+
}
15+
16+
// function isValidBST(root: TreeNode | null): boolean {
17+
// if (!root) return true;
18+
// if (root.left && root.left.val > root.val) return false;
19+
// if (root.right && root.right.val < root.val) return false;
20+
// const l = isValidBST(root.left), r = isValidBST(root.right);
21+
// return l && r;
22+
// }
23+
24+
// function isValidBST(root: TreeNode | null, parent = root.val): boolean {
25+
// if (!root) return false;
26+
// if (root.left && (root.left.val >= root.val || root.left.val >= parent)) return false;
27+
// if (root.right && (root.right.val <= root.val || root.right.val <= parent)) return false;
28+
// return isValidBST(root.left, root.val) && isValidBST(root.right, root.val);
29+
// }
30+
31+
function isValidBST(
32+
root: TreeNode | null,
33+
min: number | null = null,
34+
max: number | null = null
35+
): boolean {
36+
if (!root) return true;
37+
if ((min !== null && root.val <= min) || (max !== null && root.val >= max))
38+
return false;
39+
return ( isValidBST(root.left, min, root.val) && isValidBST(root.right, root.val, max));
40+
}
41+
42+
const a = new TreeNode(7);
43+
const c = new TreeNode(3);
44+
const d = new TreeNode(6, c, a);
45+
const b = new TreeNode(4);
46+
const e = new TreeNode(5, b, d);
47+
48+
console.log(isValidBST(e));
49+
50+
const f = new TreeNode(1);
51+
const g = new TreeNode(3);
52+
const h = new TreeNode(2, f, g);
53+
54+
// console.log(isValidBST(h));
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const maxProfit = (prices: number[]): number => {
2-
let curr = 0, max = 0, l = 0;
3-
for (let i = 1; i < prices.length; i++) {
4-
if (prices[l] < prices[i])
5-
(curr = prices[i] - prices[l]), (max = Math.max(curr, max));
6-
else l = i;
7-
}
8-
return max;
9-
};
1+
// const maxProfit = (prices: number[]): number => {
2+
// let curr = 0, max = 0, l = 0;
3+
// for (let i = 1; i < prices.length; i++) {
4+
// if (prices[l] < prices[i])
5+
// (curr = prices[i] - prices[l]), (max = Math.max(curr, max));
6+
// else l = i;
7+
// }
8+
// return max;
9+
// };

ts/src/neetcode-150-practice/sliding-window/02_longest-substring-without-repeating-characters.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { ITestCase, runTests } from '../../utils/tests';
22

33
const lengthOfLongestSubstring = (s: string): number => {
4-
let max = 0, i = 0, charsSet = new Set<string>();
5-
for (let j = 0; j < s.length; j++) {
6-
const char = s.charAt(j);
7-
if (charsSet.has(char)) {
8-
max = Math.max(max, charsSet.size);
9-
while (charsSet.has(char) && i < j) charsSet.delete(s.charAt(i++));
10-
charsSet.add(char);
11-
} else charsSet.add(char);
4+
let l = 0,
5+
wCount = new Set<string>(),
6+
maxLen = 0;
7+
for (const c of s) {
8+
if (wCount.has(c)) {
9+
maxLen = Math.max(maxLen, wCount.size);
10+
while (wCount.has(c)) wCount.delete(s.charAt(l++));
11+
}
12+
wCount.add(c);
1213
}
13-
return Math.max(charsSet.size, max);
14+
return Math.max(wCount.size, maxLen);
1415
};
1516

1617
// TEST CASES
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// function maxProfit(prices: number[]): number {
2+
// let profit = 0;
3+
// for (let i = 1; i < prices.length; i++)
4+
// if (prices[i] > prices[i - 1]) profit += prices[i] - prices[i - 1];
5+
// return profit;
6+
// }
7+
8+
// console.log(maxProfit([7, 1, 5, 3, 6, 4]));
9+
// console.log(maxProfit([1, 2, 3, 4, 5]));
10+
// console.log(maxProfit([7, 6, 4, 3, 1]));
11+
// console.log(maxProfit([2, 1, 2, 0, 1]));
12+
13+
function searchInsert(nums: number[], target: number): number {
14+
let [l, r, m] = [0, nums.length - 1, 0];
15+
while (l <= r) {
16+
m = Math.floor((l + r) / 2);
17+
if (nums[m] < target) l = m + 1;
18+
else if (nums[m] > target) r = m - 1;
19+
else if (nums[m] === target) return m;
20+
}
21+
return l;
22+
}
23+
24+
console.log(searchInsert([1, 3, 5, 6], 5));
25+
console.log(searchInsert([1, 3, 5, 6], 2));
26+
console.log(searchInsert([1, 3, 5, 6], 7));
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function getMaxPalindrome(l: number, r: number, s: string) {
2+
while (l >= 0 && r < s.length && s[l] == s[r]) l--, r++;
3+
return [++l, --r, r - l + 1];
4+
}
5+
6+
function longestPalindrome(s: string): string {
7+
let [maxPalStart, maxPalEnd, maxPalLen, len] = [0, 0, 0, s.length];
8+
for (let i = 0; i < len; i++) {
9+
const oddPal = getMaxPalindrome(i, i, s);
10+
const evenPal = getMaxPalindrome(i, i + 1, s);
11+
if (oddPal[2] > maxPalLen)
12+
(maxPalLen = oddPal[2]),
13+
(maxPalStart = oddPal[0]),
14+
(maxPalEnd = oddPal[1]);
15+
if (evenPal[2] > maxPalLen)
16+
(maxPalLen = evenPal[2]),
17+
(maxPalStart = evenPal[0]),
18+
(maxPalEnd = evenPal[1]);
19+
}
20+
return s.slice(maxPalStart, maxPalEnd + 1);
21+
}
22+
23+
console.log(longestPalindrome('babad'), ' === bab/aba');
24+
console.log(longestPalindrome('cbbd'), ' === bb');
25+
26+
// let [l, r, currLen] = [i, i, 0];
27+
// while (l >= 0 && r < len && s[l] == s[r]) {
28+
// currLen = r - l + 1;
29+
// if (currLen > maxPalLen)
30+
// (maxPalLen = currLen), (maxPalStart = l), (maxPalEnd = r);
31+
// l--, r++;
32+
// }
33+
// (l = i), (r = i + 1);
34+
// while (l >= 0 && r < len && s[l] == s[r]) {
35+
// currLen = r - l + 1;
36+
// if (currLen > maxPalLen)
37+
// (maxPalLen = currLen), (maxPalStart = l), (maxPalEnd = r);
38+
// l--, r++;
39+
// }

ts/src/random/palindrome-number.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// function isPalindrome(x: number): boolean {
2+
// if (x < 0) return false;
3+
// while (x > 9) {
4+
// const m = Math.pow(10, Math.floor(Math.log10(x)));
5+
// if (Math.floor(x / m) !== x % 10) return false;
6+
// x = x % m;
7+
// x = Math.floor(x / 10);
8+
// }
9+
// return true;
10+
// }
11+
12+
function isPalindrome(x: number): boolean {
13+
if (x < 0) return false;
14+
let [temp, reversed] = [x, 0];
15+
while (temp > 0)
16+
(reversed += temp % 10), (reversed *= 10), (temp = Math.floor(temp / 10));
17+
return reversed === x * 10;
18+
}
19+
20+
console.log(isPalindrome(121), ' == true');
21+
console.log(isPalindrome(-121), ' == false');
22+
console.log(isPalindrome(10), ' == false');
23+
console.log(isPalindrome(1221), ' == true');
24+
console.log(isPalindrome(1000021), ' == false'); // edge case
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function reversePrefix(word: string, ch: string): string {
2+
let i = word.indexOf(ch);
3+
if (i == -1) return word;
4+
const res = word.split('');
5+
let l = 0;
6+
while (l < i) ([res[l], res[i]] = [res[i], res[l]]), l++, i--;
7+
return res.join('');
8+
}
9+
10+
console.log(reversePrefix('abcdefd', 'd'), ' == dcbaefd');
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function myAtoi(s: string): number {
2+
let [l, isPositive, sum, currInt] = [0, true, 0, 0];
3+
while (s[l] === ' ') l++;
4+
if (s[l] === '-') isPositive = false;
5+
if (['+', '-'].includes(s[l])) l++;
6+
while (s[l] === '0') l++;
7+
while (l < s.length) {
8+
currInt = s.charCodeAt(l);
9+
if (currInt < 48 || currInt > 57) break;
10+
sum += parseInt(s[l]);
11+
sum *= 10;
12+
l++;
13+
}
14+
sum /= 10;
15+
return isPositive
16+
? Math.min(Math.pow(2, 31) - 1, sum)
17+
: Math.max(Math.pow(-2, 31), -sum);
18+
}
19+
20+
console.log(myAtoi('42'), ' == 42');
21+
console.log(myAtoi(' -042'), ' == -42');
22+
console.log(myAtoi('1337c0d3'), ' == 1337');
23+
console.log(myAtoi('0-1'), ' == 0');
24+
console.log(myAtoi('words and 987'), ' == 0');
25+
console.log(myAtoi('+1'), ' == 1');
26+
console.log(myAtoi('21474836460'), ' == 2147483647');
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function findSubstring(s: string, words: string[]): number[] {
2+
if (!words.length || !words[0].length) return [];
3+
const [firstLetterMap, res, wordLen, allWordLen] = [
4+
new Map<string, number>(words.map((word, i) => [word[0], i])),
5+
[] as number[],
6+
words[0].length,
7+
words[0].length * words.length,
8+
];
9+
for (let i = 0; i < s.length; i++) {
10+
const c = s[i];
11+
if (firstLetterMap.has(c)) {
12+
let [l, r] = [i, i - 1 + allWordLen];
13+
}
14+
}
15+
return [];
16+
}
17+
18+
findSubstring('', ['bar', 'foo', 'the']);

ts/src/random/zigzag-conversion.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function convert(s: string, numRows: number): string {
2+
if (numRows === 1 || numRows >= s.length) return s;
3+
4+
// Create an array of empty strings for each row
5+
let rows = new Array(numRows).fill('').map(() => '');
6+
7+
let curRow = 0;
8+
let goingDown = false;
9+
10+
// Iterate over each character in the string
11+
for (let char of s) {
12+
rows[curRow] += char;
13+
if (curRow === 0 || curRow === numRows - 1) goingDown = !goingDown; // Reverse direction
14+
curRow += goingDown ? 1 : -1;
15+
}
16+
17+
// Join all rows to get the final string
18+
console.log(rows);
19+
return rows.join('');
20+
}
21+
22+
console.log(convert('PAYPALISHIRING', 3));

0 commit comments

Comments
 (0)