Skip to content

Commit fadf5cf

Browse files
feat: initial commit
1 parent e10ec37 commit fadf5cf

14 files changed

+450
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// class TreeNode {
2+
// val: number;
3+
// left: TreeNode | null;
4+
// right: TreeNode | null;
5+
// constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
6+
// this.val = val === undefined ? 0 : val;
7+
// this.left = left === undefined ? null : left;
8+
// this.right = right === undefined ? null : right;
9+
// }
10+
// }
11+
12+
function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
13+
let pCurr = p;
14+
let qCurr = q;
15+
const pStack: TreeNode[] = [];
16+
const qStack: TreeNode[] = [];
17+
18+
if (q && p) {
19+
qStack.push(q);
20+
pStack.push(p);
21+
}
22+
23+
while (
24+
(pCurr !== null || pStack.length > 0) &&
25+
(qCurr !== null || qStack.length > 0)
26+
) {
27+
// 1. Reach the leftmost node of the pCurr node
28+
while (pCurr !== null && qCurr !== null) {
29+
pStack.push(pCurr);
30+
qStack.push(qCurr);
31+
pCurr = pCurr.left;
32+
qCurr = qCurr.left;
33+
}
34+
35+
if (
36+
(pCurr && qCurr && pCurr.val !== qCurr.val) ||
37+
(pCurr && !qCurr) ||
38+
(!pCurr && qCurr)
39+
)
40+
return false;
41+
42+
// 2. Current must be null at this point
43+
pCurr = pStack.pop()!;
44+
qCurr = qStack.pop()!;
45+
46+
if (
47+
(pCurr && qCurr && pCurr.val !== qCurr.val) ||
48+
(pCurr && !qCurr) ||
49+
(!pCurr && qCurr)
50+
)
51+
return false;
52+
53+
// 3. Visit the right subtree
54+
pCurr = pCurr.right;
55+
qCurr = qCurr.right;
56+
}
57+
return (
58+
pCurr === null &&
59+
qCurr === null &&
60+
pStack.length === 0 &&
61+
qStack.length === 0
62+
);
63+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 isSymmetric(root: TreeNode | null): boolean {
16+
if (!root) {
17+
return false;
18+
}
19+
const lhs = root.left;
20+
const rhs = root.right;
21+
let matched = true;
22+
23+
const dfs = (lhs: TreeNode | null, rhs: TreeNode | null) => {
24+
if (!matched) {
25+
return;
26+
}
27+
if ((lhs && !rhs) || (!lhs && rhs)) {
28+
matched = false;
29+
return;
30+
}
31+
if (!lhs || !rhs) {
32+
return;
33+
}
34+
if (lhs.val !== rhs.val) {
35+
matched = false;
36+
return;
37+
}
38+
39+
dfs(lhs.left, rhs.right);
40+
dfs(lhs.right, rhs.left);
41+
};
42+
43+
dfs(lhs, rhs);
44+
return matched;
45+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
const dfs = (node, depth) => {
19+
if (!node) return 0;
20+
21+
const lhsDepth = dfs(node.left, depth + 1);
22+
const rhsDepth = dfs(node.right, depth + 1);
23+
24+
const currentDepth =
25+
lhsDepth > rhsDepth ? lhsDepth : rhsDepth > depth ? rhsDepth : depth;
26+
return currentDepth;
27+
};
28+
29+
return dfs(root, 0);
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function generate(numRows: number): number[][] {
2+
if (numRows === 0) return [];
3+
if (numRows === 1) return [[1]];
4+
5+
let result: number[][] = [[1]];
6+
7+
for (let i: number = 1; i < numRows; i++) {
8+
let prevRow: number[] = result[i - 1];
9+
let newRow: number[] = [];
10+
11+
newRow.push(1);
12+
13+
for (let j: number = 1; j < prevRow.length; j++) {
14+
newRow.push(prevRow[j - 1] + prevRow[j]);
15+
}
16+
17+
newRow.push(1);
18+
result.push(newRow);
19+
}
20+
return result;
21+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
You are given an array prices where prices[i] is the price of a given stock on the ith day.
3+
4+
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
5+
6+
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
7+
8+
9+
10+
Example 1:
11+
12+
Input: prices = [7,1,5,3,6,4]
13+
Output: 5
14+
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
15+
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
16+
Example 2:
17+
18+
Input: prices = [7,6,4,3,1]
19+
Output: 0
20+
Explanation: In this case, no transactions are done and the max profit = 0.
21+
22+
23+
Constraints:
24+
25+
1 <= prices.length <= 105
26+
0 <= prices[i] <= 104
27+
*/
28+
29+
function maxProfit(prices: number[]): number {
30+
if (prices.length === 0) return 0;
31+
32+
let minPrice = prices[0];
33+
let maxProfit = 0;
34+
35+
for (let i = 1; i < prices.length; i++) {
36+
const currentPrice = prices[i];
37+
if (currentPrice < minPrice) {
38+
minPrice = currentPrice;
39+
} else {
40+
const profit = currentPrice - minPrice;
41+
if (profit > maxProfit) {
42+
maxProfit = profit;
43+
}
44+
}
45+
}
46+
47+
return maxProfit;
48+
}
49+
50+
maxProfit([7, 5, 4, 3, 2, 8]);

src/questions/Pre-Questions/122.Buy-Sell-Stock-II.ts

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function singleNumber(nums: number[]): number {
2+
const numSet = new Set<number>();
3+
4+
for (let i = 0; i < nums.length; i++) {
5+
const num = nums[i];
6+
if (numSet.has(num)) {
7+
numSet.delete(num);
8+
} else {
9+
numSet.add(num);
10+
}
11+
}
12+
13+
return numSet.values().next().value;
14+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function hasCycle(head: ListNode | null): boolean {
14+
const nodeSet = new Set();
15+
nodeSet.add(head?.val);
16+
let current = new ListNode(0, head);
17+
let bool = false;
18+
19+
while (bool) {
20+
if (current.next === null) break;
21+
if (nodeSet.has(current.next.val)) {
22+
bool = true;
23+
} else {
24+
nodeSet.add(current.next.val);
25+
current = current.next;
26+
}
27+
}
28+
return bool;
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function addTwoNumbers(
2+
l1: ListNode | null,
3+
l2: ListNode | null,
4+
): ListNode | null {
5+
let dummy = new ListNode(0);
6+
let current = dummy;
7+
let carry = 0;
8+
9+
while (l1 !== null || l2 !== null) {
10+
let sum = carry;
11+
if (l1 !== null) {
12+
sum += l1.val;
13+
l1 = l1.next;
14+
}
15+
if (l2 !== null) {
16+
sum += l2.val;
17+
l2 = l2.next;
18+
}
19+
20+
carry = Math.floor(sum / 10);
21+
current.next = new ListNode(sum % 10);
22+
current = current.next;
23+
}
24+
25+
if (carry > 0) {
26+
current.next = new ListNode(carry);
27+
}
28+
29+
return dummy.next;
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function lengthOfLongestSubstring(s: string): number {
2+
let count = 0;
3+
let finalCount = 0;
4+
let obj = {};
5+
let j = 1;
6+
7+
for (let i = 0; i <= s.length - 1; i++) {
8+
if (!obj[s[i]]) {
9+
obj[s[i]] = s[i];
10+
count += 1;
11+
finalCount = Math.max(count, finalCount);
12+
} else {
13+
count = 1;
14+
i = j;
15+
obj = {
16+
[s[i]]: s[i],
17+
};
18+
j++;
19+
}
20+
}
21+
22+
return finalCount;
23+
}

0 commit comments

Comments
 (0)