Skip to content

Commit 8bad0fb

Browse files
authored
Merge pull request neetcode-gh#412 from loczek/main
Added 62 solutions for typescript
2 parents f1bfcc9 + c525834 commit 8bad0fb

File tree

62 files changed

+1587
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1587
-0
lines changed

typescript/100-Same-Tree.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
16+
if (p === null && q === null) return true;
17+
18+
if ((p === null && q !== null) || (p !== null && q === null)) return false;
19+
20+
let leftSame = isSameTree(p.left, q.left);
21+
let rightSame = isSameTree(p.right, q.right);
22+
23+
if (p.val === q.val && leftSame && rightSame) {
24+
return true;
25+
}
26+
27+
return false;
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function levelOrder(root: TreeNode | null): number[][] {
16+
const levels: number[][] = [];
17+
18+
function getHeight(node: TreeNode | null, height: number) {
19+
if (!node) return 0;
20+
21+
if (node.left || node.right) {
22+
getHeight(node.left, height + 1);
23+
getHeight(node.right, height + 1);
24+
}
25+
26+
if (levels[height]) {
27+
levels[height].push(node.val);
28+
} else {
29+
levels[height] = [node.val];
30+
}
31+
}
32+
33+
getHeight(root, 0);
34+
35+
return levels;
36+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function maxDepth(root: TreeNode | null): number {
16+
if (!root) return 0;
17+
18+
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function maxArea(height: number[]): number {
2+
let l = 0;
3+
let r = height.length - 1;
4+
let max = 0;
5+
6+
while (l < r) {
7+
let area = (r - l) * Math.min(height[l], height[r]);
8+
9+
max = Math.max(max, area);
10+
11+
if (height[l] < height[r]) {
12+
l += 1;
13+
} else {
14+
r -= 1;
15+
}
16+
}
17+
18+
return max;
19+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function isBalanced(root: TreeNode | null): boolean {
16+
let array = getHeight(root);
17+
return array[0];
18+
}
19+
20+
function getHeight(root: TreeNode | null) {
21+
if (!root) return [true, 0];
22+
23+
let [leftBalanced, leftHeight] = getHeight(root.left);
24+
let [rightBalanced, rightHeight] = getHeight(root.right);
25+
26+
let balanced = leftBalanced && rightBalanced && Math.abs(rightHeight - leftHeight) <= 1;
27+
28+
return [balanced, 1 + Math.max(leftHeight, rightHeight)];
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function maxProfit(prices: number[]): number {
2+
let max = 0;
3+
let l = 0;
4+
let r = 1;
5+
6+
while (r < prices.length) {
7+
if (prices[l] < prices[r]) {
8+
let profit = prices[r] - prices[l];
9+
if (profit > max) {
10+
max = profit;
11+
}
12+
} else {
13+
l = r;
14+
}
15+
r++;
16+
}
17+
18+
return max;
19+
}

typescript/125-Valid-Palindrome.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function isPalindrome(s: string): boolean {
2+
const array = s.toLowerCase().replace(/[^A-Za-z0-9]/g, "").replace(/\s/g, "").split("");
3+
4+
for (let i = 0; i < array.length; i++) {
5+
const first = array[i];
6+
const second = array[array.length - 1 - i];
7+
8+
if (first !== second) {
9+
return false;
10+
}
11+
}
12+
return true;
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function longestConsecutive(nums: number[]): number {
2+
const number = new Set(nums);
3+
let longest = 0;
4+
5+
for (const n of nums) {
6+
if (!number.has(n - 1)) {
7+
let length = 0;
8+
while (number.has(n + length)) {
9+
length += 1;
10+
}
11+
longest = Math.max(length, longest);
12+
}
13+
}
14+
15+
return longest;
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function partition(s: string): string[][] {
2+
let res: string[][] = [];
3+
let part: string[] = [];
4+
5+
function dfs(i: number) {
6+
if (i >= s.length) {
7+
res.push(part.slice());
8+
return;
9+
}
10+
11+
for (let j = i; j < s.length; j++) {
12+
if (isPali(s, i, j)) {
13+
part.push(s.slice(i, j + 1));
14+
dfs(j + 1);
15+
part.pop();
16+
}
17+
}
18+
}
19+
20+
dfs(0);
21+
return res;
22+
23+
function isPali(s: string, l: number, r: number) {
24+
while (l < r) {
25+
if (s[l] != s[r]) {
26+
return false;
27+
}
28+
l = l + 1;
29+
r = r - 1;
30+
}
31+
return true;
32+
}
33+
}

typescript/136-Single-Number.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function singleNumber(nums: number[]): number {
2+
let res = 0;
3+
4+
for (let i = 0; i < nums.length; i++) {
5+
res = nums[i] ^ res;
6+
}
7+
8+
return res;
9+
}

0 commit comments

Comments
 (0)