From 9f00585d9f056414419d50e1b2ceeaf700354ffc Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 24 Sep 2024 19:34:13 +0900 Subject: [PATCH 01/45] 217 / Contains Duplicate / Easy / 8m --- alexgoni/217_Contains_Duplicate.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 alexgoni/217_Contains_Duplicate.js diff --git a/alexgoni/217_Contains_Duplicate.js b/alexgoni/217_Contains_Duplicate.js new file mode 100644 index 0000000..10404cb --- /dev/null +++ b/alexgoni/217_Contains_Duplicate.js @@ -0,0 +1,14 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function (nums) { + const numberDict = {}; + + for (const num of nums) { + if (numberDict[num] === undefined) numberDict[num] = true; + else return true; + } + + return false; +}; From c3c0a1f75f48ae1da8293999780d36ef62bc829a Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 24 Sep 2024 19:42:08 +0900 Subject: [PATCH 02/45] 268 / Missing Number / Easy / 4m --- alexgoni/268_Missing_Number.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 alexgoni/268_Missing_Number.js diff --git a/alexgoni/268_Missing_Number.js b/alexgoni/268_Missing_Number.js new file mode 100644 index 0000000..6d4dc1f --- /dev/null +++ b/alexgoni/268_Missing_Number.js @@ -0,0 +1,11 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var missingNumber = function (nums) { + const n = nums.length; + const sum = (n * (n + 1)) / 2; + const missingSum = nums.reduce((a, c) => a + c, 0); + + return sum - missingSum; +}; From e1bf0a76c85956e7693d7d07d2cc816b996c59b2 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 24 Sep 2024 20:31:41 +0900 Subject: [PATCH 03/45] Meeting Schedule / Easy / 10m --- alexgoni/Meeting_Schedule.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 alexgoni/Meeting_Schedule.js diff --git a/alexgoni/Meeting_Schedule.js b/alexgoni/Meeting_Schedule.js new file mode 100644 index 0000000..a32672f --- /dev/null +++ b/alexgoni/Meeting_Schedule.js @@ -0,0 +1,25 @@ +/** + * Definition of Interval: + * class Interval { + * constructor(start, end) { + * this.start = start; + * this.end = end; + * } + * } + */ + +class Solution { + /** + * @param {Interval[]} intervals + * @returns {boolean} + */ + canAttendMeetings(intervals) { + intervals.sort((a, b) => a.start - b.start); + + for (let i = 0; i < intervals.length - 1; i++) { + if (intervals[i + 1].start < intervals[i].end) return false; + } + + return true; + } +} From 130847cc4ec435f83fd6ad8e1de7383419056438 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 24 Sep 2024 22:02:26 +0900 Subject: [PATCH 04/45] 121 / Best Time to Buy and Sell Stock / Easy --- .../121_Best_Time_to_Buy_and_Sell_Stock.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 alexgoni/121_Best_Time_to_Buy_and_Sell_Stock.js diff --git a/alexgoni/121_Best_Time_to_Buy_and_Sell_Stock.js b/alexgoni/121_Best_Time_to_Buy_and_Sell_Stock.js new file mode 100644 index 0000000..a6b11da --- /dev/null +++ b/alexgoni/121_Best_Time_to_Buy_and_Sell_Stock.js @@ -0,0 +1,19 @@ +// 😢 + +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function (prices) { + let minPrice = Number.POSITIVE_INFINITY; + let maxProfit = 0; + + for (const price of prices) { + if (minPrice > price) minPrice = price; + + const profit = price - minPrice; + if (profit > maxProfit) maxProfit = profit; + } + + return maxProfit; +}; From 83256b25f6faf48279ba58b52d59235412cb0200 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 24 Sep 2024 22:46:42 +0900 Subject: [PATCH 05/45] 15 / 3Sum / Medium --- alexgoni/15_3Sum.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 alexgoni/15_3Sum.js diff --git a/alexgoni/15_3Sum.js b/alexgoni/15_3Sum.js new file mode 100644 index 0000000..51a6125 --- /dev/null +++ b/alexgoni/15_3Sum.js @@ -0,0 +1,43 @@ +// 😢 + +/** + * @param {number[]} nums + * @return {number[][]} + */ +var threeSum = function (nums) { + const result = []; + + nums.sort((a, b) => a - b); + + for (let i = 0; i < nums.length - 2; i++) { + if (i > 0 && nums[i] === nums[i - 1]) continue; + + const fixed = nums[i]; + let left = i + 1; + let right = nums.length - 1; + + while (left < right) { + const sum = fixed + nums[left] + nums[right]; + + if (sum === 0) { + result.push([fixed, nums[left], nums[right]]); + + while (left < right && nums[left] === nums[left + 1]) left++; + while (left < right && nums[right] === nums[right - 1]) right--; + + left++; + right--; + } else if (sum > 0) { + right--; + } else { + left++; + } + } + } + + return result; +}; + +console.log(threeSum([0, 0, 0, 0])); + +// -4, -1, -1 ,0 ,1, 2 From 8810c0d9ab0128bb948dd28ab32bfbd720046ff8 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Thu, 3 Oct 2024 14:32:31 +0900 Subject: [PATCH 06/45] 242 / Valid Anagram / Easy / 7m --- alexgoni/242_Valid_Anagram.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 alexgoni/242_Valid_Anagram.js diff --git a/alexgoni/242_Valid_Anagram.js b/alexgoni/242_Valid_Anagram.js new file mode 100644 index 0000000..bb4cee8 --- /dev/null +++ b/alexgoni/242_Valid_Anagram.js @@ -0,0 +1,29 @@ +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function (s, t) { + const frequencyCounter1 = {}; + const frequencyCounter2 = {}; + + [...s].forEach((str) => { + frequencyCounter1[str] = (frequencyCounter1[str] || 0) + 1; + }); + + [...t].forEach((str) => { + frequencyCounter2[str] = (frequencyCounter2[str] || 0) + 1; + }); + + if ( + Object.keys(frequencyCounter1).length !== + Object.keys(frequencyCounter2).length + ) + return false; + + for (const key in frequencyCounter1) { + if (frequencyCounter1[key] !== frequencyCounter2[key]) return false; + } + + return true; +}; From 427e43611700cb0a01cdbb6e2df3a045003934bf Mon Sep 17 00:00:00 2001 From: alexgoni Date: Fri, 4 Oct 2024 10:22:23 +0900 Subject: [PATCH 07/45] 238 / Product of Array Except Self / Medium --- alexgoni/238_Product_of_Array_Except_Self.js | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 alexgoni/238_Product_of_Array_Except_Self.js diff --git a/alexgoni/238_Product_of_Array_Except_Self.js b/alexgoni/238_Product_of_Array_Except_Self.js new file mode 100644 index 0000000..842aa70 --- /dev/null +++ b/alexgoni/238_Product_of_Array_Except_Self.js @@ -0,0 +1,23 @@ +// 😢 + +/** + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function (nums) { + const result = Array.from({ length: nums.length }, () => 1); + + let leftProduct = 1; + for (let i = 0; i < nums.length; i++) { + result[i] = leftProduct; + leftProduct *= nums[i]; + } + + let rightProduct = 1; + for (let i = nums.length - 1; i >= 0; i--) { + result[i] *= rightProduct; + rightProduct *= nums[i]; + } + + return result; +}; From a8ae2fcf56d9f30326cf1ccfc77a4c1ffa3bdb7d Mon Sep 17 00:00:00 2001 From: alexgoni Date: Fri, 4 Oct 2024 13:55:31 +0900 Subject: [PATCH 08/45] 191 / Number of 1 Bits / Easy / 3m --- alexgoni/191_Number_of_1_Bits.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 alexgoni/191_Number_of_1_Bits.js diff --git a/alexgoni/191_Number_of_1_Bits.js b/alexgoni/191_Number_of_1_Bits.js new file mode 100644 index 0000000..a94726c --- /dev/null +++ b/alexgoni/191_Number_of_1_Bits.js @@ -0,0 +1,15 @@ +/** + * @param {number} n + * @return {number} + */ +var hammingWeight = function (n) { + let result = 0; + + [...n.toString(2)].forEach((each) => { + if (each === "1") result++; + }); + + return result; +}; + +console.log(hammingWeight(2147483645)); From 96461d9592509ec84158ac22785f99e21b4c7231 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Sat, 5 Oct 2024 00:20:20 +0900 Subject: [PATCH 09/45] 100 / Same Tree / Easy / 30m --- alexgoni/100_Same_Tree.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 alexgoni/100_Same_Tree.js diff --git a/alexgoni/100_Same_Tree.js b/alexgoni/100_Same_Tree.js new file mode 100644 index 0000000..133db63 --- /dev/null +++ b/alexgoni/100_Same_Tree.js @@ -0,0 +1,39 @@ +/** + * 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} p + * @param {TreeNode} q + * @return {boolean} + */ +var isSameTree = function (p, q) { + const bfs = (start) => { + const result = []; + const queue = [start]; + + while (queue.length > 0) { + const current = queue.shift(); + + if (current === null) { + result.push(null); + continue; + } else result.push(current.val); + + if (current.left) queue.push(current.left); + if (!current.left && current.right) queue.push(null); + if (current.right) queue.push(current.right); + } + + return result; + }; + + const pArr = bfs(p); + const qArr = bfs(q); + + return JSON.stringify(pArr) === JSON.stringify(qArr); +}; From 169db8c346f989f1d6e09a06adaa22fc6581f94c Mon Sep 17 00:00:00 2001 From: alexgoni Date: Sat, 5 Oct 2024 13:43:48 +0900 Subject: [PATCH 10/45] 746 / Min Cost Climbing Stairs / Easy --- alexgoni/15_3Sum.js | 4 ---- alexgoni/191_Number_of_1_Bits.js | 2 -- alexgoni/746_Min_Cost_Climbing_Stairs.js | 23 +++++++++++++++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 alexgoni/746_Min_Cost_Climbing_Stairs.js diff --git a/alexgoni/15_3Sum.js b/alexgoni/15_3Sum.js index 51a6125..88df475 100644 --- a/alexgoni/15_3Sum.js +++ b/alexgoni/15_3Sum.js @@ -37,7 +37,3 @@ var threeSum = function (nums) { return result; }; - -console.log(threeSum([0, 0, 0, 0])); - -// -4, -1, -1 ,0 ,1, 2 diff --git a/alexgoni/191_Number_of_1_Bits.js b/alexgoni/191_Number_of_1_Bits.js index a94726c..f63c4ab 100644 --- a/alexgoni/191_Number_of_1_Bits.js +++ b/alexgoni/191_Number_of_1_Bits.js @@ -11,5 +11,3 @@ var hammingWeight = function (n) { return result; }; - -console.log(hammingWeight(2147483645)); diff --git a/alexgoni/746_Min_Cost_Climbing_Stairs.js b/alexgoni/746_Min_Cost_Climbing_Stairs.js new file mode 100644 index 0000000..772cb2d --- /dev/null +++ b/alexgoni/746_Min_Cost_Climbing_Stairs.js @@ -0,0 +1,23 @@ +// 😢 + +/** + * @param {number[]} cost + * @return {number} + */ +var minCostClimbingStairs = function (cost) { + const stairMinCost = Array.from({ length: cost.length }, () => 0); + + stairMinCost[0] = cost[0]; + stairMinCost[1] = cost[1]; + + for (let i = 2; i < cost.length; i++) { + if (stairMinCost[i - 1] >= stairMinCost[i - 2]) { + stairMinCost[i] = stairMinCost[i - 2] + cost[i]; + } else stairMinCost[i] = stairMinCost[i - 1] + cost[i]; + } + + return Math.min( + stairMinCost[stairMinCost.length - 1], + stairMinCost[stairMinCost.length - 2] + ); +}; From 2beb97d606c929263121c75baf6d582e30702f7f Mon Sep 17 00:00:00 2001 From: alexgoni Date: Sun, 6 Oct 2024 13:34:10 +0900 Subject: [PATCH 11/45] 190 / Reverse Bits / Easy / 13m --- alexgoni/190_Reverse_Bits.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 alexgoni/190_Reverse_Bits.js diff --git a/alexgoni/190_Reverse_Bits.js b/alexgoni/190_Reverse_Bits.js new file mode 100644 index 0000000..09b09bf --- /dev/null +++ b/alexgoni/190_Reverse_Bits.js @@ -0,0 +1,16 @@ +/** + * @param {number} n - a positive integer + * @return {number} - a positive integer + */ +var reverseBits = function (n) { + let bitsOf32 = ""; + const binaryStr = n.toString(2); + + for (let i = 0; i < 32 - binaryStr.length; i++) { + bitsOf32 += "0"; + } + bitsOf32 += binaryStr; + + const reversedBitsOf32 = [...bitsOf32].reverse().join(""); + return parseInt(reversedBitsOf32, 2); +}; From 14f6422de74078ef48dea84c87b20e5fd4ada708 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Mon, 7 Oct 2024 09:22:33 +0900 Subject: [PATCH 12/45] 125 / Valid Palindrome / Easy / 4m --- alexgoni/125_Valid_Palindrome.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 alexgoni/125_Valid_Palindrome.js diff --git a/alexgoni/125_Valid_Palindrome.js b/alexgoni/125_Valid_Palindrome.js new file mode 100644 index 0000000..dd6bb0b --- /dev/null +++ b/alexgoni/125_Valid_Palindrome.js @@ -0,0 +1,12 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isPalindrome = function (s) { + let newStr = s.toLowerCase(); + newStr = newStr.replace(/[^a-z0-9]/g, ""); + + const reverseStr = [...newStr].reverse().join(""); + + return newStr === reverseStr; +}; From 85daeb7f20639e3511b30bab9797a47385953fcd Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 8 Oct 2024 21:09:55 +0900 Subject: [PATCH 13/45] 206 / Reverse Linked List / Easy / 24m --- alexgoni/206_Reverse_Linked_List.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 alexgoni/206_Reverse_Linked_List.js diff --git a/alexgoni/206_Reverse_Linked_List.js b/alexgoni/206_Reverse_Linked_List.js new file mode 100644 index 0000000..a570969 --- /dev/null +++ b/alexgoni/206_Reverse_Linked_List.js @@ -0,0 +1,25 @@ +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var reverseList = function (head) { + let current = head; + let prev = null; + let next; + + while (current) { + next = current.next; + current.next = prev; + prev = current; + current = next; + } + + return prev; +}; From c95b2b5e8e08ffbc050fc6829c626fdb2ac68afa Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 8 Oct 2024 23:53:56 +0900 Subject: [PATCH 14/45] 49 / Group Anagrams / Medium / 30m --- alexgoni/49_Group_Anagrams.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 alexgoni/49_Group_Anagrams.js diff --git a/alexgoni/49_Group_Anagrams.js b/alexgoni/49_Group_Anagrams.js new file mode 100644 index 0000000..c8de8c0 --- /dev/null +++ b/alexgoni/49_Group_Anagrams.js @@ -0,0 +1,19 @@ +/** + * @param {string[]} strs + * @return {string[][]} + */ +var groupAnagrams = function (strs) { + const dict = {}; + + for (const str of strs) { + const sortedStr = [...str].sort().join(""); + + if (dict[sortedStr]) { + dict[sortedStr].push(str); + } else { + dict[sortedStr] = [str]; + } + } + + return Object.values(dict); +}; From aa029fb14d7cea9528dab38aa5da50a896c4c364 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Wed, 9 Oct 2024 10:51:01 +0900 Subject: [PATCH 15/45] 322 / Coin Change / Medium --- alexgoni/322_Coin_Change.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 alexgoni/322_Coin_Change.js diff --git a/alexgoni/322_Coin_Change.js b/alexgoni/322_Coin_Change.js new file mode 100644 index 0000000..2c2f0d2 --- /dev/null +++ b/alexgoni/322_Coin_Change.js @@ -0,0 +1,21 @@ +// 😢 + +/** + * @param {number[]} coins + * @param {number} amount + * @return {number} + */ +var coinChange = function (coins, amount) { + const dp = Array(amount + 1).fill(Infinity); + dp[0] = 0; + + for (let i = 1; i <= amount; i++) { + for (let coin of coins) { + if (i >= coin) { + dp[i] = Math.min(dp[i], dp[i - coin] + 1); + } + } + } + + return dp[amount] === Infinity ? -1 : dp[amount]; +}; From 8b0f8bb1675f37874c059c9e63cf46177bf2bfc7 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Wed, 16 Oct 2024 09:03:52 +0900 Subject: [PATCH 16/45] 1 / Two Sum / Easy / 4m --- alexgoni/1_Two_Sum.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 alexgoni/1_Two_Sum.js diff --git a/alexgoni/1_Two_Sum.js b/alexgoni/1_Two_Sum.js new file mode 100644 index 0000000..df8b5a5 --- /dev/null +++ b/alexgoni/1_Two_Sum.js @@ -0,0 +1,30 @@ +// /** +// * @param {number[]} nums +// * @param {number} target +// * @return {number[]} +// */ +// var twoSum = function (nums, target) { +// for (let i = 0; i < nums.length - 1; i++) { +// for (let j = i + 1; j < nums.length; j++) { +// const sum = nums[i] + nums[j]; + +// if (sum === target) return [i, j]; +// } +// } +// }; + +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function (nums, target) { + const obj = {}; + + for (let i = 0; i < nums.length; i++) { + const value = nums[i]; + if (target - value in obj) return [i, obj[target - value]]; + + obj[value] = i; + } +}; From d99ad18d7ea150932459f3c24fa85fc9f406aae0 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Wed, 16 Oct 2024 09:07:07 +0900 Subject: [PATCH 17/45] 104 / Maximum Depth of Binary Tree / Easy --- alexgoni/104_Maximum_Depth_of_Binary_Tree.js | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 alexgoni/104_Maximum_Depth_of_Binary_Tree.js diff --git a/alexgoni/104_Maximum_Depth_of_Binary_Tree.js b/alexgoni/104_Maximum_Depth_of_Binary_Tree.js new file mode 100644 index 0000000..832b19c --- /dev/null +++ b/alexgoni/104_Maximum_Depth_of_Binary_Tree.js @@ -0,0 +1,22 @@ +// 😢 + +/** + * 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 maxDepth = function (root) { + if (!root) return 0; + + const leftDepth = maxDepth(root.left); + const rightDepth = maxDepth(root.right); + + return Math.max(leftDepth, rightDepth) + 1; +}; From 672e4d03fcbb001e48d21d26fe253a1774f8bc75 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Wed, 16 Oct 2024 09:08:09 +0900 Subject: [PATCH 18/45] 141 / Linked List Cycle / Easy / 15m --- alexgoni/141_Linked_List_Cycle.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 alexgoni/141_Linked_List_Cycle.js diff --git a/alexgoni/141_Linked_List_Cycle.js b/alexgoni/141_Linked_List_Cycle.js new file mode 100644 index 0000000..12475a8 --- /dev/null +++ b/alexgoni/141_Linked_List_Cycle.js @@ -0,0 +1,25 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ + +/** + * @param {ListNode} head + * @return {boolean} + */ +var hasCycle = function (head) { + const visited = new Map(); + let current = head; + + while (current) { + if (visited.has(current)) return true; + + visited.set(current, true); + current = current.next; + } + + return false; +}; From 2c26983382fadcc9ee13f86346fbc00be7e75cc1 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Wed, 16 Oct 2024 09:10:00 +0900 Subject: [PATCH 19/45] 20 / Valid Parentheses / Easy / 12m --- alexgoni/20_Valid_Parentheses.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 alexgoni/20_Valid_Parentheses.js diff --git a/alexgoni/20_Valid_Parentheses.js b/alexgoni/20_Valid_Parentheses.js new file mode 100644 index 0000000..d77d2fc --- /dev/null +++ b/alexgoni/20_Valid_Parentheses.js @@ -0,0 +1,28 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isValid = function (s) { + const stack = []; + + for (let i = 0; i < s.length; i++) { + if (stack.length === 0) { + stack.push(s[i]); + continue; + } + + if (s[i] === "(" || s[i] === "{" || s[i] === "[") stack.push(s[i]); + else if (s[i] === ")") { + if (stack[stack.length - 1] === "(") stack.pop(); + else stack.push(s[i]); + } else if (s[i] === "}") { + if (stack[stack.length - 1] === "{") stack.pop(); + else stack.push(s[i]); + } else if (s[i] === "]") { + if (stack[stack.length - 1] === "[") stack.pop(); + else stack.push(s[i]); + } + } + + return stack.length === 0; +}; From 8ff63f8bafd579fae123fc93d44b76c87922d470 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Wed, 16 Oct 2024 09:19:47 +0900 Subject: [PATCH 20/45] 202 / Happy Number / Easy / 20m --- alexgoni/202_Happy_Number.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 alexgoni/202_Happy_Number.js diff --git a/alexgoni/202_Happy_Number.js b/alexgoni/202_Happy_Number.js new file mode 100644 index 0000000..87d8b84 --- /dev/null +++ b/alexgoni/202_Happy_Number.js @@ -0,0 +1,20 @@ +/** + * @param {number} n + * @return {boolean} + */ +var isHappy = function (n) { + const visited = {}; + + while (true) { + let sum = 0; + [...String(n)].forEach((str) => { + sum += Number(str) ** 2; + }); + + if (sum === 0) return true; + if (visited[sum]) return false; + + visited[sum] = true; + n = sum; + } +}; From 33b6af70e76eb2831baa99f6014fb34494fb4da6 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Mon, 4 Nov 2024 09:51:22 +0900 Subject: [PATCH 21/45] 136 / Single Number / Easy / 3m --- alexgoni/136_Single_Number.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 alexgoni/136_Single_Number.js diff --git a/alexgoni/136_Single_Number.js b/alexgoni/136_Single_Number.js new file mode 100644 index 0000000..954952b --- /dev/null +++ b/alexgoni/136_Single_Number.js @@ -0,0 +1,15 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var singleNumber = function (nums) { + const obj = {}; + + for (const num of nums) { + obj[num] = (obj[num] || 0) + 1; + } + + for (const key in obj) { + if (obj[key] === 1) return Number(key); + } +}; From cf7a089b5466975914d5f5c2f27ceb1ac1323111 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Mon, 4 Nov 2024 10:27:01 +0900 Subject: [PATCH 22/45] 110 / Balanced Binary Tree / Easy --- alexgoni/110_Balanced_Binary_Tree.js | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 alexgoni/110_Balanced_Binary_Tree.js diff --git a/alexgoni/110_Balanced_Binary_Tree.js b/alexgoni/110_Balanced_Binary_Tree.js new file mode 100644 index 0000000..b9f20ff --- /dev/null +++ b/alexgoni/110_Balanced_Binary_Tree.js @@ -0,0 +1,31 @@ +// 😢 + +/** + * 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 {boolean} + */ +var isBalanced = function (root) { + let result = true; + + const maxDepth = (root) => { + if (!root) return 0; + + const leftDepth = maxDepth(root.left); + const rightDepth = maxDepth(root.right); + + if (Math.abs(leftDepth - rightDepth) > 1) result = false; + + return Math.max(leftDepth, rightDepth) + 1; + }; + + maxDepth(root); + return result; +}; From f480da2d81cf79e8453a0916da8691d94863e6bb Mon Sep 17 00:00:00 2001 From: alexgoni Date: Mon, 4 Nov 2024 11:00:27 +0900 Subject: [PATCH 23/45] 973 / K Closest Points to Origin / Medium / 5m --- alexgoni/973_K_Closest_Points_to_Origin.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 alexgoni/973_K_Closest_Points_to_Origin.js diff --git a/alexgoni/973_K_Closest_Points_to_Origin.js b/alexgoni/973_K_Closest_Points_to_Origin.js new file mode 100644 index 0000000..b8065b0 --- /dev/null +++ b/alexgoni/973_K_Closest_Points_to_Origin.js @@ -0,0 +1,9 @@ +/** + * @param {number[][]} points + * @param {number} k + * @return {number[][]} + */ +var kClosest = function (points, k) { + points.sort((a, b) => a[0] ** 2 + a[1] ** 2 - (b[0] ** 2 + b[1] ** 2)); + return points.slice(0, k); +}; From d321a7b4bb62ee3f4dc04e0be242ba814869ab1f Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 5 Nov 2024 09:51:17 +0900 Subject: [PATCH 24/45] 155 / Min Stack / Medium / 11m --- alexgoni/155_Min_Stack.js | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 alexgoni/155_Min_Stack.js diff --git a/alexgoni/155_Min_Stack.js b/alexgoni/155_Min_Stack.js new file mode 100644 index 0000000..8d88fed --- /dev/null +++ b/alexgoni/155_Min_Stack.js @@ -0,0 +1,44 @@ +var MinStack = function () { + this.min = Number.POSITIVE_INFINITY; + this.stack = []; +}; + +/** + * @param {number} val + * @return {void} + */ +MinStack.prototype.push = function (val) { + this.stack.push(val); + if (this.min > val) this.min = val; +}; + +/** + * @return {void} + */ +MinStack.prototype.pop = function () { + this.stack.pop(); + this.min = Math.min(...this.stack); +}; + +/** + * @return {number} + */ +MinStack.prototype.top = function () { + return this.stack[this.stack.length - 1]; +}; + +/** + * @return {number} + */ +MinStack.prototype.getMin = function () { + return this.min; +}; + +/** + * Your MinStack object will be instantiated and called as such: + * var obj = new MinStack() + * obj.push(val) + * obj.pop() + * var param_3 = obj.top() + * var param_4 = obj.getMin() + */ From 9b96eb4bf61b22d51de3f131970bb6536b047fbb Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 5 Nov 2024 10:39:19 +0900 Subject: [PATCH 25/45] 54 / Spiral Matrix / Medium / 60m --- alexgoni/54_Spiral_Matrix.js | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 alexgoni/54_Spiral_Matrix.js diff --git a/alexgoni/54_Spiral_Matrix.js b/alexgoni/54_Spiral_Matrix.js new file mode 100644 index 0000000..66fd542 --- /dev/null +++ b/alexgoni/54_Spiral_Matrix.js @@ -0,0 +1,46 @@ +var spiralOrder = function (matrix) { + const result = []; + let m = matrix[0].length; + let n = matrix.length; + let direction = 0; + let spiralCount = 0; + let i = 0, + j = 0; + + for (let count = 0; count < m * n; count++) { + result.push(matrix[i][j]); + + if (direction === 0) { + if (j < m - 1 - spiralCount) { + j++; + } else { + direction = 1; + i++; + } + } else if (direction === 1) { + if (i < n - 1 - spiralCount) { + i++; + } else { + direction = 2; + j--; + } + } else if (direction === 2) { + if (j > spiralCount) { + j--; + } else { + direction = 3; + i--; + } + } else if (direction === 3) { + if (i > spiralCount + 1) { + i--; + } else { + direction = 0; + spiralCount++; + j++; + } + } + } + + return result; +}; From 856d432b7e2b5b1aba795c0dd2f9b19700ed0f63 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 5 Nov 2024 20:17:47 +0900 Subject: [PATCH 26/45] 338 / Counting Bits / Easy / 23m --- alexgoni/338_Counting_Bits.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 alexgoni/338_Counting_Bits.js diff --git a/alexgoni/338_Counting_Bits.js b/alexgoni/338_Counting_Bits.js new file mode 100644 index 0000000..3d46cc5 --- /dev/null +++ b/alexgoni/338_Counting_Bits.js @@ -0,0 +1,19 @@ +/** + * @param {number} n + * @return {number[]} + */ +var countBits = function (n) { + const result = []; + + for (let i = 0; i <= n; i++) { + const str = i.toString(2); + let count = 0; + [...str].forEach((char) => { + if (char === "1") count++; + }); + + result.push(count); + } + + return result; +}; From 50cc2e8aae483e8920c7cc94bf6b7306e68c8c59 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 5 Nov 2024 20:19:55 +0900 Subject: [PATCH 27/45] 70 / Climbing Stairs / Easy / 25m --- alexgoni/70_Climbing_Stairs.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 alexgoni/70_Climbing_Stairs.js diff --git a/alexgoni/70_Climbing_Stairs.js b/alexgoni/70_Climbing_Stairs.js new file mode 100644 index 0000000..606676b --- /dev/null +++ b/alexgoni/70_Climbing_Stairs.js @@ -0,0 +1,25 @@ +/** + * @param {number} n + * @return {number} + */ +var climbStairs = function (n) { + let result = 0; + + for (let i = 0; i <= n / 2; i++) { + const count2 = i; + const count1 = n - 2 * count2; + + result += combination(count1 + count2, count2); + } + + return result; +}; + +function combination(n, r) { + let result = 1; + + for (let i = 0; i < r; i++) result *= n - i; + for (let i = 1; i <= r; i++) result /= i; + + return result; +} From 5a2e1090a9a1ca903df5fc6cfbb14c3a62c09c68 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 5 Nov 2024 20:21:40 +0900 Subject: [PATCH 28/45] 21 / Merge Two Sorted Lists / Easy --- alexgoni/21_Merge_Two_Sorted_Lists.js | 54 +++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 alexgoni/21_Merge_Two_Sorted_Lists.js diff --git a/alexgoni/21_Merge_Two_Sorted_Lists.js b/alexgoni/21_Merge_Two_Sorted_Lists.js new file mode 100644 index 0000000..312ca9d --- /dev/null +++ b/alexgoni/21_Merge_Two_Sorted_Lists.js @@ -0,0 +1,54 @@ +// 😢 + +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} list1 + * @param {ListNode} list2 + * @return {ListNode} + */ +var mergeTwoLists = function (list1, list2) { + if (!list1 && !list2) return null; + else if (!list1) return list2; + else if (!list2) return list1; + + let head; + let current1 = list1; + let current2 = list2; + if (current1.val >= current2.val) { + head = current2; + current2 = current2.next; + } else { + head = current1; + current1 = current1.next; + } + + let current = head; + while (current1 || current2) { + if (current1 === null) { + current.next = current2; + break; + } + if (current2 === null) { + current.next = current1; + break; + } + + if (current1.val >= current2.val) { + current.next = current2; + current2 = current2.next; + } else { + current.next = current1; + current1 = current1.next; + } + + current = current.next; + } + + return head; +}; From 51c2532adc6b3e340da6c8f792b5f0eae53b2276 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 5 Nov 2024 20:23:42 +0900 Subject: [PATCH 29/45] 1046 / Last Stone Weight / Easy / 55m --- alexgoni/1046_Last_Stone_Weight.js | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 alexgoni/1046_Last_Stone_Weight.js diff --git a/alexgoni/1046_Last_Stone_Weight.js b/alexgoni/1046_Last_Stone_Weight.js new file mode 100644 index 0000000..cc3475a --- /dev/null +++ b/alexgoni/1046_Last_Stone_Weight.js @@ -0,0 +1,36 @@ +/** + * @param {number[]} stones + * @return {number} + */ +var lastStoneWeight = function (stones) { + while (stones.length > 1) { + let maxIdx, secondMaxIdx; + if (stones[0] >= stones[1]) { + maxIdx = 0; + secondMaxIdx = 1; + } else { + maxIdx = 1; + secondMaxIdx = 0; + } + + for (let i = 2; i < stones.length; i++) { + if (stones[maxIdx] <= stones[i]) { + secondMaxIdx = maxIdx; + maxIdx = i; + } else if (stones[secondMaxIdx] < stones[i]) { + secondMaxIdx = i; + } + } + + if (stones[maxIdx] === stones[secondMaxIdx]) { + stones = stones.filter( + (_, idx) => idx !== maxIdx && idx !== secondMaxIdx + ); + } else { + stones[maxIdx] = stones[maxIdx] - stones[secondMaxIdx]; + stones.splice(secondMaxIdx, 1); + } + } + + return stones.length > 0 ? stones[0] : 0; +}; From 086da91258e7b9f1cef143b00bd49093a6f15095 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 5 Nov 2024 20:53:18 +0900 Subject: [PATCH 30/45] 48 / Rotate Image / Medium / 20m --- alexgoni/48_Rotate_Image.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 alexgoni/48_Rotate_Image.js diff --git a/alexgoni/48_Rotate_Image.js b/alexgoni/48_Rotate_Image.js new file mode 100644 index 0000000..e7bc42e --- /dev/null +++ b/alexgoni/48_Rotate_Image.js @@ -0,0 +1,22 @@ +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +var rotate = function (matrix) { + const copy = []; + const n = matrix.length; + + for (let i = 0; i < n; i++) { + const row = []; + for (let j = 0; j < n; j++) { + row.push(matrix[i][j]); + } + copy.push(row); + } + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + matrix[i][j] = copy[n - 1 - j][i]; + } + } +}; From 0b15f28e265cccfbd3e145a235c5746b5f6d186c Mon Sep 17 00:00:00 2001 From: alexgoni Date: Wed, 13 Nov 2024 11:15:12 +0900 Subject: [PATCH 31/45] 543 / Diameter of Binary Tree / Easy / 15m --- alexgoni/543_Diameter_of_Binary_Tree.js | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 alexgoni/543_Diameter_of_Binary_Tree.js diff --git a/alexgoni/543_Diameter_of_Binary_Tree.js b/alexgoni/543_Diameter_of_Binary_Tree.js new file mode 100644 index 0000000..09b443d --- /dev/null +++ b/alexgoni/543_Diameter_of_Binary_Tree.js @@ -0,0 +1,28 @@ +/** + * 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 diameterOfBinaryTree = function (root) { + let result = 0; + + const maxDepth = (node) => { + if (node === null) return 0; + + const leftDepth = maxDepth(node.left); + const rightDepth = maxDepth(node.right); + + result = Math.max(result, leftDepth + rightDepth); + return Math.max(leftDepth, rightDepth) + 1; + }; + + maxDepth(root); + return result; +}; From 6a6bf322aa3c8b6c65086cafd40033f09a438587 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Wed, 13 Nov 2024 11:16:57 +0900 Subject: [PATCH 32/45] 128 / Longest Consecutive Sequence / Medium / 23m --- alexgoni/128_Longest_Consecutive_Sequence.js | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 alexgoni/128_Longest_Consecutive_Sequence.js diff --git a/alexgoni/128_Longest_Consecutive_Sequence.js b/alexgoni/128_Longest_Consecutive_Sequence.js new file mode 100644 index 0000000..c980776 --- /dev/null +++ b/alexgoni/128_Longest_Consecutive_Sequence.js @@ -0,0 +1,23 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var longestConsecutive = function (nums) { + if (nums.length === 0) return 0; + + let result = 1; + let temp = 1; + nums = [...new Set(nums)]; + nums.sort((a, b) => a - b); + + for (let i = 0; i < nums.length - 1; i++) { + if (nums[i + 1] === nums[i] + 1) temp++; + else { + result = Math.max(result, temp); + temp = 1; + } + } + + result = Math.max(result, temp); + return result; +}; From 4c9c8a021bb77ed88089995d73ed1bd9e416dbae Mon Sep 17 00:00:00 2001 From: alexgoni Date: Wed, 13 Nov 2024 11:19:01 +0900 Subject: [PATCH 33/45] 371 / Sum of Two Integers / Medium --- alexgoni/371_Sum_of_Two_Integers.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 alexgoni/371_Sum_of_Two_Integers.js diff --git a/alexgoni/371_Sum_of_Two_Integers.js b/alexgoni/371_Sum_of_Two_Integers.js new file mode 100644 index 0000000..18e324e --- /dev/null +++ b/alexgoni/371_Sum_of_Two_Integers.js @@ -0,0 +1,20 @@ +// 😢 + +/** + * @param {number} a + * @param {number} b + * @return {number} + */ +var getSum = function (a, b) { + let x = a; + let y = b; + + while (y !== 0) { + const sumWithoutCarry = x ^ y; + const carry = (x & y) << 1; + + x = sumWithoutCarry; + y = carry; + } + return x; +}; From 8146bd27ab2e888858b6ff99f14046d7cae44be7 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Wed, 13 Nov 2024 11:20:09 +0900 Subject: [PATCH 34/45] 208 / Implement Trie / Medium / 30m --- alexgoni/208_Implement_Trie.js | 51 ++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 alexgoni/208_Implement_Trie.js diff --git a/alexgoni/208_Implement_Trie.js b/alexgoni/208_Implement_Trie.js new file mode 100644 index 0000000..a7e2abb --- /dev/null +++ b/alexgoni/208_Implement_Trie.js @@ -0,0 +1,51 @@ +class Node { + constructor({ key, data = null }) { + this.key = key; + this.data = data; + this.children = {}; + } +} + +class Trie { + constructor() { + this.head = new Node({ key: null }); + } + + insert(word) { + let currentNode = this.head; + + for (let i = 0; i < word.length; i++) { + const str = word[i]; + const newNode = currentNode.children?.[str] + ? currentNode.children[str] + : new Node({ key: str }); + + if (i === word.length - 1) newNode.data = word; + currentNode.children[str] = newNode; + currentNode = newNode; + } + } + + search(word) { + let currentNode = this.head; + + for (const str of word) { + if (currentNode.children[str]) currentNode = currentNode.children[str]; + else return false; + } + + if (currentNode.data) return true; + else return false; + } + + startsWith(prefix) { + let currentNode = this.head; + + for (const str of prefix) { + if (currentNode.children[str]) currentNode = currentNode.children[str]; + else return false; + } + + return true; + } +} From 790d80a00e16438e6ac17e6281b6a2738c204e83 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Wed, 13 Nov 2024 11:21:03 +0900 Subject: [PATCH 35/45] 226 / Invert Binary Tree / Easy / 13m --- alexgoni/226_Invert_Binary_Tree.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 alexgoni/226_Invert_Binary_Tree.js diff --git a/alexgoni/226_Invert_Binary_Tree.js b/alexgoni/226_Invert_Binary_Tree.js new file mode 100644 index 0000000..d19d098 --- /dev/null +++ b/alexgoni/226_Invert_Binary_Tree.js @@ -0,0 +1,22 @@ +/** + * 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 {TreeNode} + */ +var invertTree = function (root) { + if (!root) return root; + + invertTree(root.left); + invertTree(root.right); + + [root.left, root.right] = [root.right, root.left]; + + return root; +}; From b23d72e0b634c1d69682e3c1406a75cd72e27593 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 26 Nov 2024 21:54:14 +0900 Subject: [PATCH 36/45] 102 / Binary Tree Level Order Traversal / Medium --- .../102_Binary_Tree_Level_Order_Traversal.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 alexgoni/102_Binary_Tree_Level_Order_Traversal.js diff --git a/alexgoni/102_Binary_Tree_Level_Order_Traversal.js b/alexgoni/102_Binary_Tree_Level_Order_Traversal.js new file mode 100644 index 0000000..893aefd --- /dev/null +++ b/alexgoni/102_Binary_Tree_Level_Order_Traversal.js @@ -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; +}; From ab11a2f4b412827e186f96ce5dbea4121409b22c Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 26 Nov 2024 21:58:06 +0900 Subject: [PATCH 37/45] 213 / House Rober ll / Medium / 25m --- alexgoni/213_House_Robber_ll.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 alexgoni/213_House_Robber_ll.js diff --git a/alexgoni/213_House_Robber_ll.js b/alexgoni/213_House_Robber_ll.js new file mode 100644 index 0000000..f348bb7 --- /dev/null +++ b/alexgoni/213_House_Robber_ll.js @@ -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] + ); +}; From c93b49c72498721219f3c1aba8a9806b656ded0f Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 26 Nov 2024 21:59:32 +0900 Subject: [PATCH 38/45] 207 / Course Schedule / Medium --- alexgoni/207_Course_Schedule.js | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 alexgoni/207_Course_Schedule.js diff --git a/alexgoni/207_Course_Schedule.js b/alexgoni/207_Course_Schedule.js new file mode 100644 index 0000000..2423e36 --- /dev/null +++ b/alexgoni/207_Course_Schedule.js @@ -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; +}; From 6e01ac4caeb42f2fc00863dafdeac2f7302b9937 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 26 Nov 2024 22:01:23 +0900 Subject: [PATCH 39/45] 150 / Evaluate Reverse Polish Notation / Medium / 36m --- .../150_Evaluate_Reverse_Polish_Notation.js | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 alexgoni/150_Evaluate_Reverse_Polish_Notation.js diff --git a/alexgoni/150_Evaluate_Reverse_Polish_Notation.js b/alexgoni/150_Evaluate_Reverse_Polish_Notation.js new file mode 100644 index 0000000..454d27d --- /dev/null +++ b/alexgoni/150_Evaluate_Reverse_Polish_Notation.js @@ -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(); +}; From df0dc158b9e4b449706d439a6351e620dcb383fb Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 26 Nov 2024 22:47:32 +0900 Subject: [PATCH 40/45] 572 / Subtree of Another Tree / Easy --- alexgoni/572_Subtree_of_Another_Tree.js | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 alexgoni/572_Subtree_of_Another_Tree.js diff --git a/alexgoni/572_Subtree_of_Another_Tree.js b/alexgoni/572_Subtree_of_Another_Tree.js new file mode 100644 index 0000000..893eaf4 --- /dev/null +++ b/alexgoni/572_Subtree_of_Another_Tree.js @@ -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) + ); +} From 5473baabe01d21d2d2729e9de57ba0c4e07c4615 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 26 Nov 2024 22:49:07 +0900 Subject: [PATCH 41/45] 56 / Merge Intervals / Medium / 30m --- alexgoni/56_Merge_Intervals.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 alexgoni/56_Merge_Intervals.js diff --git a/alexgoni/56_Merge_Intervals.js b/alexgoni/56_Merge_Intervals.js new file mode 100644 index 0000000..9b74555 --- /dev/null +++ b/alexgoni/56_Merge_Intervals.js @@ -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; +}; From 3ba5e6cf3818dd79eed477d32f5f500277830190 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 26 Nov 2024 22:50:52 +0900 Subject: [PATCH 42/45] 1584 / Min Cost to Connect All Points / Medium --- .../1584_Min_Cost_to_Connect_All_Points.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 alexgoni/1584_Min_Cost_to_Connect_All_Points.js diff --git a/alexgoni/1584_Min_Cost_to_Connect_All_Points.js b/alexgoni/1584_Min_Cost_to_Connect_All_Points.js new file mode 100644 index 0000000..16419c7 --- /dev/null +++ b/alexgoni/1584_Min_Cost_to_Connect_All_Points.js @@ -0,0 +1,37 @@ +/** + * @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; +}; From fcd3812d4aa4846d8a83ccbe69099d3487659b05 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 26 Nov 2024 22:52:45 +0900 Subject: [PATCH 43/45] 235 / Lowest Common Ancestor of a Binary Search Tree / Medium --- .../1584_Min_Cost_to_Connect_All_Points.js | 2 ++ ...Common_Ancestor_of_a_Binary_Search_Tree.js | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 alexgoni/235_Lowest_Common_Ancestor_of_a_Binary_Search_Tree.js diff --git a/alexgoni/1584_Min_Cost_to_Connect_All_Points.js b/alexgoni/1584_Min_Cost_to_Connect_All_Points.js index 16419c7..9882b6d 100644 --- a/alexgoni/1584_Min_Cost_to_Connect_All_Points.js +++ b/alexgoni/1584_Min_Cost_to_Connect_All_Points.js @@ -1,3 +1,5 @@ +// 😢 + /** * @param {number[][]} points * @return {number} diff --git a/alexgoni/235_Lowest_Common_Ancestor_of_a_Binary_Search_Tree.js b/alexgoni/235_Lowest_Common_Ancestor_of_a_Binary_Search_Tree.js new file mode 100644 index 0000000..d2c48bc --- /dev/null +++ b/alexgoni/235_Lowest_Common_Ancestor_of_a_Binary_Search_Tree.js @@ -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; +}; From 09a485fb63d8f6a4eb0a6f50903974c2835b9e35 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 26 Nov 2024 22:53:34 +0900 Subject: [PATCH 44/45] 139 / Word Break / Medium --- alexgoni/139_Word_Break.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 alexgoni/139_Word_Break.js diff --git a/alexgoni/139_Word_Break.js b/alexgoni/139_Word_Break.js new file mode 100644 index 0000000..601f052 --- /dev/null +++ b/alexgoni/139_Word_Break.js @@ -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]; +}; From bdcf97708b4fc554e0055e281451a4728861f0c0 Mon Sep 17 00:00:00 2001 From: alexgoni Date: Tue, 26 Nov 2024 22:55:17 +0900 Subject: [PATCH 45/45] 215 / Kth Largest Element in an Array / Medium --- .../215_Kth_Largest_Element_in_an_Array.js | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 alexgoni/215_Kth_Largest_Element_in_an_Array.js diff --git a/alexgoni/215_Kth_Largest_Element_in_an_Array.js b/alexgoni/215_Kth_Largest_Element_in_an_Array.js new file mode 100644 index 0000000..e82715b --- /dev/null +++ b/alexgoni/215_Kth_Largest_Element_in_an_Array.js @@ -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; + } + } +}