Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
9f00585
217 / Contains Duplicate / Easy / 8m
alexgoni Sep 24, 2024
c3c0a1f
268 / Missing Number / Easy / 4m
alexgoni Sep 24, 2024
e1bf0a7
Meeting Schedule / Easy / 10m
alexgoni Sep 24, 2024
130847c
121 / Best Time to Buy and Sell Stock / Easy
alexgoni Sep 24, 2024
83256b2
15 / 3Sum / Medium
alexgoni Sep 24, 2024
ac52d2b
Merge branch 'master' of https://github.com/forStudyingJavaScript/leeโ€ฆ
alexgoni Oct 3, 2024
8810c0d
242 / Valid Anagram / Easy / 7m
alexgoni Oct 3, 2024
427e436
238 / Product of Array Except Self / Medium
alexgoni Oct 4, 2024
a8ae2fc
191 / Number of 1 Bits / Easy / 3m
alexgoni Oct 4, 2024
96461d9
100 / Same Tree / Easy / 30m
alexgoni Oct 4, 2024
169db8c
746 / Min Cost Climbing Stairs / Easy
alexgoni Oct 5, 2024
2beb97d
190 / Reverse Bits / Easy / 13m
alexgoni Oct 6, 2024
14f6422
125 / Valid Palindrome / Easy / 4m
alexgoni Oct 7, 2024
8021ccb
Merge branch 'master' of https://github.com/forStudyingJavaScript/leeโ€ฆ
alexgoni Oct 8, 2024
85daeb7
206 / Reverse Linked List / Easy / 24m
alexgoni Oct 8, 2024
c95b2b5
49 / Group Anagrams / Medium / 30m
alexgoni Oct 8, 2024
aa029fb
322 / Coin Change / Medium
alexgoni Oct 9, 2024
feedc3a
Merge branch 'master' of https://github.com/forStudyingJavaScript/leeโ€ฆ
alexgoni Oct 16, 2024
8b0f8bb
1 / Two Sum / Easy / 4m
alexgoni Oct 16, 2024
d99ad18
104 / Maximum Depth of Binary Tree / Easy
alexgoni Oct 16, 2024
672e4d0
141 / Linked List Cycle / Easy / 15m
alexgoni Oct 16, 2024
2c26983
20 / Valid Parentheses / Easy / 12m
alexgoni Oct 16, 2024
8ff63f8
202 / Happy Number / Easy / 20m
alexgoni Oct 16, 2024
e7f36b9
Merge branch 'master' of https://github.com/forStudyingJavaScript/leeโ€ฆ
alexgoni Nov 4, 2024
33b6af7
136 / Single Number / Easy / 3m
alexgoni Nov 4, 2024
cf7a089
110 / Balanced Binary Tree / Easy
alexgoni Nov 4, 2024
f480da2
973 / K Closest Points to Origin / Medium / 5m
alexgoni Nov 4, 2024
d321a7b
155 / Min Stack / Medium / 11m
alexgoni Nov 5, 2024
9b96eb4
54 / Spiral Matrix / Medium / 60m
alexgoni Nov 5, 2024
856d432
338 / Counting Bits / Easy / 23m
alexgoni Nov 5, 2024
50cc2e8
70 / Climbing Stairs / Easy / 25m
alexgoni Nov 5, 2024
5a2e109
21 / Merge Two Sorted Lists / Easy
alexgoni Nov 5, 2024
51c2532
1046 / Last Stone Weight / Easy / 55m
alexgoni Nov 5, 2024
086da91
48 / Rotate Image / Medium / 20m
alexgoni Nov 5, 2024
d707d81
Merge branch 'master' of https://github.com/forStudyingJavaScript/leeโ€ฆ
alexgoni Nov 13, 2024
0b15f28
543 / Diameter of Binary Tree / Easy / 15m
alexgoni Nov 13, 2024
6a6bf32
128 / Longest Consecutive Sequence / Medium / 23m
alexgoni Nov 13, 2024
4c9c8a0
371 / Sum of Two Integers / Medium
alexgoni Nov 13, 2024
8146bd2
208 / Implement Trie / Medium / 30m
alexgoni Nov 13, 2024
790d80a
226 / Invert Binary Tree / Easy / 13m
alexgoni Nov 13, 2024
b23d72e
102 / Binary Tree Level Order Traversal / Medium
alexgoni Nov 26, 2024
1bf6c2e
Merge branch 'alexgoni' of https://github.com/forStudyingJavaScript/lโ€ฆ
alexgoni Nov 26, 2024
ab11a2f
213 / House Rober ll / Medium / 25m
alexgoni Nov 26, 2024
c93b49c
207 / Course Schedule / Medium
alexgoni Nov 26, 2024
6e01ac4
150 / Evaluate Reverse Polish Notation / Medium / 36m
alexgoni Nov 26, 2024
df0dc15
572 / Subtree of Another Tree / Easy
alexgoni Nov 26, 2024
5473baa
56 / Merge Intervals / Medium / 30m
alexgoni Nov 26, 2024
3ba5e6c
1584 / Min Cost to Connect All Points / Medium
alexgoni Nov 26, 2024
fcd3812
235 / Lowest Common Ancestor of a Binary Search Tree / Medium
alexgoni Nov 26, 2024
09a485f
139 / Word Break / Medium
alexgoni Nov 26, 2024
bdcf977
215 / Kth Largest Element in an Array / Medium
alexgoni Nov 26, 2024
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
37 changes: 37 additions & 0 deletions alexgoni/102_Binary_Tree_Level_Order_Traversal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// ๐Ÿ˜ข

/**
* 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 {number[][]}
*/
var levelOrder = function (root) {
if (!root) return [];

const result = [];
const q = [root];

while (q.length > 0) {
const level = [];
const size = q.length;

for (let i = 0; i < size; i++) {
const node = q.shift();
level.push(node.val);

if (node.left) q.push(node.left);
if (node.right) q.push(node.right);
}

result.push(level);
}

return result;
};
23 changes: 23 additions & 0 deletions alexgoni/139_Word_Break.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ๐Ÿ˜ข

/**
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
var wordBreak = function (s, wordDict) {
const wordSet = new Set(wordDict);
const dp = Array.from({ length: s.length + 1 }).fill(false);
dp[0] = true;

for (let i = 1; i <= s.length; i++) {
for (let j = 0; j < i; j++) {
if (dp[j] && wordSet.has(s.slice(j, i))) {
dp[i] = true;
break;
}
}
}

return dp[s.length];
};
48 changes: 48 additions & 0 deletions alexgoni/150_Evaluate_Reverse_Polish_Notation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// /**
// * @param {string[]} tokens
// * @return {number}
// */
// var evalRPN = function(tokens) {
// let i = 0;

// while(tokens.length > 1) {
// if(isNaN(tokens[i])) {
// let temp;

// if(tokens[i] === "+") temp = Number(tokens[i-2]) + Number(tokens[i-1]);
// if(tokens[i] === "-") temp = Number(tokens[i-2]) - Number(tokens[i-1]);
// if(tokens[i] === "*") temp = Number(tokens[i-2]) * Number(tokens[i-1]);
// if(tokens[i] === "/") temp = Math.trunc(Number(tokens[i-2]) / Number(tokens[i-1]));

// tokens.splice(i-2, 3, temp);
// i = 0;
// }

// i++;
// }

// return +tokens[0];
// };

/**
* @param {string[]} tokens
* @return {number}
*/
var evalRPN = function (tokens) {
const stack = [];

for (let token of tokens) {
if (!isNaN(token)) stack.push(Number(token));
else {
const b = stack.pop();
const a = stack.pop();

if (token === "+") stack.push(a + b);
if (token === "-") stack.push(a - b);
if (token === "*") stack.push(a * b);
if (token === "/") stack.push(Math.trunc(a / b));
}
}

return stack.pop();
};
39 changes: 39 additions & 0 deletions alexgoni/1584_Min_Cost_to_Connect_All_Points.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// ๐Ÿ˜ข

/**
* @param {number[][]} points
* @return {number}
*/
var minCostConnectPoints = function (points) {
const n = points.length;
const visited = Array(n).fill(false);
const minDist = Array(n).fill(Infinity);
minDist[0] = 0;
let result = 0;

for (let i = 0; i < n; i++) {
let currMin = Infinity;
let currNode = -1;

for (let j = 0; j < n; j++) {
if (!visited[j] && minDist[j] < currMin) {
currMin = minDist[j];
currNode = j;
}
}

visited[currNode] = true;
result += currMin;

for (let j = 0; j < n; j++) {
if (!visited[j]) {
const cost =
Math.abs(points[currNode][0] - points[j][0]) +
Math.abs(points[currNode][1] - points[j][1]);
minDist[j] = Math.min(minDist[j], cost);
}
}
}

return result;
};
34 changes: 34 additions & 0 deletions alexgoni/207_Course_Schedule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// ๐Ÿ˜ข

/**
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {boolean}
*/
var canFinish = function (numCourses, prerequisites) {
const graph = Array.from({ length: numCourses }, () => []);
for (const [a, b] of prerequisites) {
graph[b].push(a);
}

const visited = Array(numCourses).fill(0);

const hasCycle = (cource) => {
if (visited[cource] === 1) return true;
if (visited[cource] === 2) return false;

visited[cource] = 1;
for (const neighbor of graph[cource]) {
if (hasCycle(neighbor)) return true;
}
visited[cource] = 2;

return false;
};

for (let i = 0; i < numCourses; i++) {
if (hasCycle(i)) return false;
}

return true;
};
30 changes: 30 additions & 0 deletions alexgoni/213_House_Robber_ll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function (nums) {
if (nums.length <= 3) return Math.max(...nums);

const firstNums = nums.slice(0, nums.length - 1);
const secondNums = nums.slice(1);
const firstDp = [firstNums[0], firstNums[1]];
const secondDp = [secondNums[0], secondNums[1]];

for (let i = 2; i < firstNums.length; i++) {
if (i === 2) firstDp[i] = firstDp[0] + firstNums[i];
else firstDp[i] = Math.max(firstDp[i - 2], firstDp[i - 3]) + firstNums[i];
}

for (let i = 2; i < secondNums.length; i++) {
if (i === 2) secondDp[i] = secondDp[0] + secondNums[i];
else
secondDp[i] = Math.max(secondDp[i - 2], secondDp[i - 3]) + secondNums[i];
}

return Math.max(
firstDp[firstNums.length - 1],
firstDp[firstNums.length - 2],
secondDp[secondNums.length - 1],
secondDp[secondNums.length - 2]
);
};
96 changes: 96 additions & 0 deletions alexgoni/215_Kth_Largest_Element_in_an_Array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// ๐Ÿ˜ข

/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var findKthLargest = function (nums, k) {
const heap = new MaxBinaryHeap();

for (const num of nums) {
heap.insert(num);
}

for (let i = 0; i < k; i++) {
const extractedValue = heap.extractMax();
if (i === k - 1) return extractedValue;
}
};

class MaxBinaryHeap {
constructor() {
this.values = [];
}

insert(value) {
this.values.push(value);
this.#bubbleUp();

return this.values;
}

extractMax() {
const extractedValue = this.values[0];
const poppedValue = this.values.pop();

if (this.values.length === 0) return extractedValue;

this.values[0] = poppedValue;
this.#sinkDown();

return extractedValue;
}

#bubbleUp() {
let currentIndex = this.values.length - 1;
const currentValue = this.values[currentIndex];

while (currentIndex > 0) {
const parentIndex = Math.floor((currentIndex - 1) / 2);
const parentValue = this.values[parentIndex];

if (parentValue >= currentValue) break;

this.values[parentIndex] = currentValue;
this.values[currentIndex] = parentValue;

currentIndex = parentIndex;
}
}

#sinkDown() {
let currentIndex = 0;
const currentValue = this.values[currentIndex];
const length = this.values.length;

while (true) {
let leftChildIndex = currentIndex * 2 + 1;
let rightChildIndex = currentIndex * 2 + 2;
let leftChildValue, rightChildValue;
let swapWith = null;

if (leftChildIndex < length) {
leftChildValue = this.values[leftChildIndex];
if (leftChildValue > currentValue) swapWith = leftChildIndex;
}

if (rightChildIndex < length) {
rightChildValue = this.values[rightChildIndex];
if (
(!swapWith && rightChildValue > currentValue) ||
(swapWith && rightChildValue > leftChildValue)
) {
swapWith = rightChildIndex;
}
}

if (!swapWith) break;

this.values[currentIndex] = this.values[swapWith];
this.values[swapWith] = currentValue;

currentIndex = swapWith;
}
}
}
27 changes: 27 additions & 0 deletions alexgoni/235_Lowest_Common_Ancestor_of_a_Binary_Search_Tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ๐Ÿ˜ข

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/

/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
var lowestCommonAncestor = function (root, p, q) {
if (p.val < root.val && q.val < root.val) {
return lowestCommonAncestor(root.left, p, q);
}

if (p.val > root.val && q.val > root.val) {
return lowestCommonAncestor(root.right, p, q);
}

return root;
};
22 changes: 22 additions & 0 deletions alexgoni/56_Merge_Intervals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @param {number[][]} intervals
* @return {number[][]}
*/
var merge = function (intervals) {
const result = [];
intervals.sort((a, b) => a[0] - b[0]);

let i = 0;
while (i < intervals.length - 1) {
if (intervals[i][1] >= intervals[i + 1][0]) {
intervals.splice(i, 2, [
intervals[i][0],
Math.max(intervals[i][1], intervals[i + 1][1]),
]);
} else {
i++;
}
}

return intervals;
};
30 changes: 30 additions & 0 deletions alexgoni/572_Subtree_of_Another_Tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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) {
if (!root) return false;

if (isSameTree(root, subRoot)) return true;

return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
};

function isSameTree(root1, root2) {
if (!root1 && !root2) return true;
if (!root1 || !root2) return false;
if (root1.val !== root2.val) return false;

return (
isSameTree(root1.left, root2.left) && isSameTree(root1.right, root2.right)
);
}
Loading