Skip to content

Commit c9840fb

Browse files
authored
[alexgoni] 24.11.26 (#108)
* 56 / Merge Intervals / Medium / 30m * 1584 / Min Cost to Connect All Points / Medium * 235 / Lowest Common Ancestor of a Binary Search Tree / Medium * 139 / Word Break / Medium * 215 / Kth Largest Element in an Array / Medium
1 parent e8c1eaf commit c9840fb

10 files changed

+386
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 😢
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* function TreeNode(val, left, right) {
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+
* @param {TreeNode} root
13+
* @return {number[][]}
14+
*/
15+
var levelOrder = function (root) {
16+
if (!root) return [];
17+
18+
const result = [];
19+
const q = [root];
20+
21+
while (q.length > 0) {
22+
const level = [];
23+
const size = q.length;
24+
25+
for (let i = 0; i < size; i++) {
26+
const node = q.shift();
27+
level.push(node.val);
28+
29+
if (node.left) q.push(node.left);
30+
if (node.right) q.push(node.right);
31+
}
32+
33+
result.push(level);
34+
}
35+
36+
return result;
37+
};

alexgoni/139_Word_Break.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// 😢
2+
3+
/**
4+
* @param {string} s
5+
* @param {string[]} wordDict
6+
* @return {boolean}
7+
*/
8+
var wordBreak = function (s, wordDict) {
9+
const wordSet = new Set(wordDict);
10+
const dp = Array.from({ length: s.length + 1 }).fill(false);
11+
dp[0] = true;
12+
13+
for (let i = 1; i <= s.length; i++) {
14+
for (let j = 0; j < i; j++) {
15+
if (dp[j] && wordSet.has(s.slice(j, i))) {
16+
dp[i] = true;
17+
break;
18+
}
19+
}
20+
}
21+
22+
return dp[s.length];
23+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// /**
2+
// * @param {string[]} tokens
3+
// * @return {number}
4+
// */
5+
// var evalRPN = function(tokens) {
6+
// let i = 0;
7+
8+
// while(tokens.length > 1) {
9+
// if(isNaN(tokens[i])) {
10+
// let temp;
11+
12+
// if(tokens[i] === "+") temp = Number(tokens[i-2]) + Number(tokens[i-1]);
13+
// if(tokens[i] === "-") temp = Number(tokens[i-2]) - Number(tokens[i-1]);
14+
// if(tokens[i] === "*") temp = Number(tokens[i-2]) * Number(tokens[i-1]);
15+
// if(tokens[i] === "/") temp = Math.trunc(Number(tokens[i-2]) / Number(tokens[i-1]));
16+
17+
// tokens.splice(i-2, 3, temp);
18+
// i = 0;
19+
// }
20+
21+
// i++;
22+
// }
23+
24+
// return +tokens[0];
25+
// };
26+
27+
/**
28+
* @param {string[]} tokens
29+
* @return {number}
30+
*/
31+
var evalRPN = function (tokens) {
32+
const stack = [];
33+
34+
for (let token of tokens) {
35+
if (!isNaN(token)) stack.push(Number(token));
36+
else {
37+
const b = stack.pop();
38+
const a = stack.pop();
39+
40+
if (token === "+") stack.push(a + b);
41+
if (token === "-") stack.push(a - b);
42+
if (token === "*") stack.push(a * b);
43+
if (token === "/") stack.push(Math.trunc(a / b));
44+
}
45+
}
46+
47+
return stack.pop();
48+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// 😢
2+
3+
/**
4+
* @param {number[][]} points
5+
* @return {number}
6+
*/
7+
var minCostConnectPoints = function (points) {
8+
const n = points.length;
9+
const visited = Array(n).fill(false);
10+
const minDist = Array(n).fill(Infinity);
11+
minDist[0] = 0;
12+
let result = 0;
13+
14+
for (let i = 0; i < n; i++) {
15+
let currMin = Infinity;
16+
let currNode = -1;
17+
18+
for (let j = 0; j < n; j++) {
19+
if (!visited[j] && minDist[j] < currMin) {
20+
currMin = minDist[j];
21+
currNode = j;
22+
}
23+
}
24+
25+
visited[currNode] = true;
26+
result += currMin;
27+
28+
for (let j = 0; j < n; j++) {
29+
if (!visited[j]) {
30+
const cost =
31+
Math.abs(points[currNode][0] - points[j][0]) +
32+
Math.abs(points[currNode][1] - points[j][1]);
33+
minDist[j] = Math.min(minDist[j], cost);
34+
}
35+
}
36+
}
37+
38+
return result;
39+
};

alexgoni/207_Course_Schedule.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// 😢
2+
3+
/**
4+
* @param {number} numCourses
5+
* @param {number[][]} prerequisites
6+
* @return {boolean}
7+
*/
8+
var canFinish = function (numCourses, prerequisites) {
9+
const graph = Array.from({ length: numCourses }, () => []);
10+
for (const [a, b] of prerequisites) {
11+
graph[b].push(a);
12+
}
13+
14+
const visited = Array(numCourses).fill(0);
15+
16+
const hasCycle = (cource) => {
17+
if (visited[cource] === 1) return true;
18+
if (visited[cource] === 2) return false;
19+
20+
visited[cource] = 1;
21+
for (const neighbor of graph[cource]) {
22+
if (hasCycle(neighbor)) return true;
23+
}
24+
visited[cource] = 2;
25+
26+
return false;
27+
};
28+
29+
for (let i = 0; i < numCourses; i++) {
30+
if (hasCycle(i)) return false;
31+
}
32+
33+
return true;
34+
};

alexgoni/213_House_Robber_ll.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var rob = function (nums) {
6+
if (nums.length <= 3) return Math.max(...nums);
7+
8+
const firstNums = nums.slice(0, nums.length - 1);
9+
const secondNums = nums.slice(1);
10+
const firstDp = [firstNums[0], firstNums[1]];
11+
const secondDp = [secondNums[0], secondNums[1]];
12+
13+
for (let i = 2; i < firstNums.length; i++) {
14+
if (i === 2) firstDp[i] = firstDp[0] + firstNums[i];
15+
else firstDp[i] = Math.max(firstDp[i - 2], firstDp[i - 3]) + firstNums[i];
16+
}
17+
18+
for (let i = 2; i < secondNums.length; i++) {
19+
if (i === 2) secondDp[i] = secondDp[0] + secondNums[i];
20+
else
21+
secondDp[i] = Math.max(secondDp[i - 2], secondDp[i - 3]) + secondNums[i];
22+
}
23+
24+
return Math.max(
25+
firstDp[firstNums.length - 1],
26+
firstDp[firstNums.length - 2],
27+
secondDp[secondNums.length - 1],
28+
secondDp[secondNums.length - 2]
29+
);
30+
};
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// 😢
2+
3+
/**
4+
* @param {number[]} nums
5+
* @param {number} k
6+
* @return {number}
7+
*/
8+
var findKthLargest = function (nums, k) {
9+
const heap = new MaxBinaryHeap();
10+
11+
for (const num of nums) {
12+
heap.insert(num);
13+
}
14+
15+
for (let i = 0; i < k; i++) {
16+
const extractedValue = heap.extractMax();
17+
if (i === k - 1) return extractedValue;
18+
}
19+
};
20+
21+
class MaxBinaryHeap {
22+
constructor() {
23+
this.values = [];
24+
}
25+
26+
insert(value) {
27+
this.values.push(value);
28+
this.#bubbleUp();
29+
30+
return this.values;
31+
}
32+
33+
extractMax() {
34+
const extractedValue = this.values[0];
35+
const poppedValue = this.values.pop();
36+
37+
if (this.values.length === 0) return extractedValue;
38+
39+
this.values[0] = poppedValue;
40+
this.#sinkDown();
41+
42+
return extractedValue;
43+
}
44+
45+
#bubbleUp() {
46+
let currentIndex = this.values.length - 1;
47+
const currentValue = this.values[currentIndex];
48+
49+
while (currentIndex > 0) {
50+
const parentIndex = Math.floor((currentIndex - 1) / 2);
51+
const parentValue = this.values[parentIndex];
52+
53+
if (parentValue >= currentValue) break;
54+
55+
this.values[parentIndex] = currentValue;
56+
this.values[currentIndex] = parentValue;
57+
58+
currentIndex = parentIndex;
59+
}
60+
}
61+
62+
#sinkDown() {
63+
let currentIndex = 0;
64+
const currentValue = this.values[currentIndex];
65+
const length = this.values.length;
66+
67+
while (true) {
68+
let leftChildIndex = currentIndex * 2 + 1;
69+
let rightChildIndex = currentIndex * 2 + 2;
70+
let leftChildValue, rightChildValue;
71+
let swapWith = null;
72+
73+
if (leftChildIndex < length) {
74+
leftChildValue = this.values[leftChildIndex];
75+
if (leftChildValue > currentValue) swapWith = leftChildIndex;
76+
}
77+
78+
if (rightChildIndex < length) {
79+
rightChildValue = this.values[rightChildIndex];
80+
if (
81+
(!swapWith && rightChildValue > currentValue) ||
82+
(swapWith && rightChildValue > leftChildValue)
83+
) {
84+
swapWith = rightChildIndex;
85+
}
86+
}
87+
88+
if (!swapWith) break;
89+
90+
this.values[currentIndex] = this.values[swapWith];
91+
this.values[swapWith] = currentValue;
92+
93+
currentIndex = swapWith;
94+
}
95+
}
96+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// 😢
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* function TreeNode(val) {
6+
* this.val = val;
7+
* this.left = this.right = null;
8+
* }
9+
*/
10+
11+
/**
12+
* @param {TreeNode} root
13+
* @param {TreeNode} p
14+
* @param {TreeNode} q
15+
* @return {TreeNode}
16+
*/
17+
var lowestCommonAncestor = function (root, p, q) {
18+
if (p.val < root.val && q.val < root.val) {
19+
return lowestCommonAncestor(root.left, p, q);
20+
}
21+
22+
if (p.val > root.val && q.val > root.val) {
23+
return lowestCommonAncestor(root.right, p, q);
24+
}
25+
26+
return root;
27+
};

alexgoni/56_Merge_Intervals.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[][]} intervals
3+
* @return {number[][]}
4+
*/
5+
var merge = function (intervals) {
6+
const result = [];
7+
intervals.sort((a, b) => a[0] - b[0]);
8+
9+
let i = 0;
10+
while (i < intervals.length - 1) {
11+
if (intervals[i][1] >= intervals[i + 1][0]) {
12+
intervals.splice(i, 2, [
13+
intervals[i][0],
14+
Math.max(intervals[i][1], intervals[i + 1][1]),
15+
]);
16+
} else {
17+
i++;
18+
}
19+
}
20+
21+
return intervals;
22+
};
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+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @param {TreeNode} subRoot
12+
* @return {boolean}
13+
*/
14+
var isSubtree = function (root, subRoot) {
15+
if (!root) return false;
16+
17+
if (isSameTree(root, subRoot)) return true;
18+
19+
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
20+
};
21+
22+
function isSameTree(root1, root2) {
23+
if (!root1 && !root2) return true;
24+
if (!root1 || !root2) return false;
25+
if (root1.val !== root2.val) return false;
26+
27+
return (
28+
isSameTree(root1.left, root2.left) && isSameTree(root1.right, root2.right)
29+
);
30+
}

0 commit comments

Comments
 (0)