From 5695d130678a842f40b81c47e2848a0d15edc884 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 24 Sep 2024 19:16:10 +0900 Subject: [PATCH 01/51] 217 / Contains Duplicate / Easy / 12m --- oh-chaeyeon/217_Contains_Duplicate.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 oh-chaeyeon/217_Contains_Duplicate.js diff --git a/oh-chaeyeon/217_Contains_Duplicate.js b/oh-chaeyeon/217_Contains_Duplicate.js new file mode 100644 index 0000000..f3b462e --- /dev/null +++ b/oh-chaeyeon/217_Contains_Duplicate.js @@ -0,0 +1,16 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function (nums) { + const seen = new Set(); + + for (let num of nums) { + if (seen.has(num)) { + return true; + } + seen.add(num); + } + + return false; +}; From 9b2a2df6948e186b05181d3a0bb7dbe4e13680c4 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 24 Sep 2024 19:29:52 +0900 Subject: [PATCH 02/51] 268 / Missing number / Easy / 10m --- oh-chaeyeon/268_Missing_number.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 oh-chaeyeon/268_Missing_number.js diff --git a/oh-chaeyeon/268_Missing_number.js b/oh-chaeyeon/268_Missing_number.js new file mode 100644 index 0000000..5ffc0ca --- /dev/null +++ b/oh-chaeyeon/268_Missing_number.js @@ -0,0 +1,15 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var missingNumber = function (nums) { + const n = nums.length; + const totalSum = (n * (n + 1)) / 2; + + let numsSum = 0; + for (let num of nums) { + numsSum += num; + } + + return totalSum - numsSum; +}; From a20252eff05b69e13f5c6b7983f78174a11ae595 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 24 Sep 2024 20:13:47 +0900 Subject: [PATCH 03/51] 121 / Best Time to buy and sell stock / Easy / 32m --- .../121_Best_Time_to_buy_and_sell_stock.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 oh-chaeyeon/121_Best_Time_to_buy_and_sell_stock.js diff --git a/oh-chaeyeon/121_Best_Time_to_buy_and_sell_stock.js b/oh-chaeyeon/121_Best_Time_to_buy_and_sell_stock.js new file mode 100644 index 0000000..6f13172 --- /dev/null +++ b/oh-chaeyeon/121_Best_Time_to_buy_and_sell_stock.js @@ -0,0 +1,21 @@ +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function (prices) { + let minValue = Infinity; + let maxProfit = 0; + + for (let i = 0; i < prices.length; i++) { + if (prices[i] < minValue) { + minValue = prices[i]; + } else { + const profit = prices[i] - minValue; + if (profit > maxProfit) { + maxProfit = profit; + } + } + } + + return maxProfit; +}; From 8f136a5b51373e013f3a65919afbde45e0d404db Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 24 Sep 2024 20:38:18 +0900 Subject: [PATCH 04/51] 253 / Meeting Rooms / Easy / 20m --- oh-chaeyeon/253_Meeting_Rooms.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 oh-chaeyeon/253_Meeting_Rooms.js diff --git a/oh-chaeyeon/253_Meeting_Rooms.js b/oh-chaeyeon/253_Meeting_Rooms.js new file mode 100644 index 0000000..33a3916 --- /dev/null +++ b/oh-chaeyeon/253_Meeting_Rooms.js @@ -0,0 +1,23 @@ +/** + * Definition of Interval: + * class Interval { + * constructor(start, end) { + * this.start = start; + * this.end = end; + * } + * } + */ + +class Solution { + canAttendMeetings(intervals) { + intervals.sort((a, b) => a.start - b.start); + + for (let i = 0; i < intervals.length - 1; i++) { + if (intervals[i].end > intervals[i + 1].start) { + return false; + } + } + + return true; + } +} From 3024e5376c4900aa637598f2919092df7a6921ee Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 24 Sep 2024 21:18:41 +0900 Subject: [PATCH 05/51] 15 / 3Sum / Medium / 22m --- oh-chaeyeon/15_3Sum.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 oh-chaeyeon/15_3Sum.js diff --git a/oh-chaeyeon/15_3Sum.js b/oh-chaeyeon/15_3Sum.js new file mode 100644 index 0000000..f360bc5 --- /dev/null +++ b/oh-chaeyeon/15_3Sum.js @@ -0,0 +1,33 @@ +/** + * @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; + + let left = i + 1; + let right = nums.length - 1; + + while (left < right) { + const sum = nums[i] + nums[left] + nums[right]; + if (sum === 0) { + result.push([nums[i], 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) { + left++; + } else { + right--; + } + } + } + return result; +}; From ed4b872c1f7fdd7174141d9b4000929e82cb158a Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 24 Sep 2024 21:58:54 +0900 Subject: [PATCH 06/51] =?UTF-8?q?252=20/=20Meeting=20Rooms=20/=20Easy=20/?= =?UTF-8?q?=2020m=20(=EB=AC=B8=EC=A0=9C=EB=B2=88=ED=98=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- oh-chaeyeon/{253_Meeting_Rooms.js => 252_Meeting_Rooms.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename oh-chaeyeon/{253_Meeting_Rooms.js => 252_Meeting_Rooms.js} (100%) diff --git a/oh-chaeyeon/253_Meeting_Rooms.js b/oh-chaeyeon/252_Meeting_Rooms.js similarity index 100% rename from oh-chaeyeon/253_Meeting_Rooms.js rename to oh-chaeyeon/252_Meeting_Rooms.js From 53a320d142927579cacdd8250246328df24e8e26 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 1 Oct 2024 19:23:37 +0900 Subject: [PATCH 07/51] 242 / Valid Anagram / Easy / 11m --- oh-chaeyeon/242_Valid_Anagram.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 oh-chaeyeon/242_Valid_Anagram.js diff --git a/oh-chaeyeon/242_Valid_Anagram.js b/oh-chaeyeon/242_Valid_Anagram.js new file mode 100644 index 0000000..4e30dc4 --- /dev/null +++ b/oh-chaeyeon/242_Valid_Anagram.js @@ -0,0 +1,20 @@ +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function (s, t) { + if (s.length != t.length) return false; + + let countMap = {}; + for (let char of s) { + countMap[char] = (countMap[char] || 0) + 1; + } + + for (let char of t) { + if (!countMap[char]) return false; + countMap[char]--; + } + + return true; +}; From c4a58be6756dd02d938af78998d457d291469b1e Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 1 Oct 2024 19:39:12 +0900 Subject: [PATCH 08/51] 238 / Product of Arrays Except Self / Medium / 14m --- .../238_Product_of_Arrays_Except_Self.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 oh-chaeyeon/238_Product_of_Arrays_Except_Self.js diff --git a/oh-chaeyeon/238_Product_of_Arrays_Except_Self.js b/oh-chaeyeon/238_Product_of_Arrays_Except_Self.js new file mode 100644 index 0000000..17f1623 --- /dev/null +++ b/oh-chaeyeon/238_Product_of_Arrays_Except_Self.js @@ -0,0 +1,22 @@ +/** + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function (nums) { + const n = nums.length; + const answer = new Array(n).fill(1); + + let left = 1; + for (let i = 0; i < n; i++) { + answer[i] = left; + left *= nums[i]; + } + + let right = 1; + for (let i = n - 1; i >= 0; i--) { + answer[i] *= right; + right *= nums[i]; + } + + return answer; +}; From fd63ce556a1d7c35d26e0f9d68debcb2131205d5 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 1 Oct 2024 19:54:14 +0900 Subject: [PATCH 09/51] 191 / Number of 1 Bits / Easy / 14m --- oh-chaeyeon/191_Number_of _1_Bits.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 oh-chaeyeon/191_Number_of _1_Bits.js diff --git a/oh-chaeyeon/191_Number_of _1_Bits.js b/oh-chaeyeon/191_Number_of _1_Bits.js new file mode 100644 index 0000000..e40dfbb --- /dev/null +++ b/oh-chaeyeon/191_Number_of _1_Bits.js @@ -0,0 +1,16 @@ +/** + * @param {number} n + * @return {number} + */ +var hammingWeight = function (n) { + const num = n.toString(2); + let count = 0; + + for (let char of num) { + if (char === "1") { + count++; + } + } + + return count; +}; From fea74058e4552e67425e34c22b412b38fe252a1a Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 1 Oct 2024 20:07:21 +0900 Subject: [PATCH 10/51] 100 / Same Tree / Easy / 12m --- oh-chaeyeon/100_Same_Tree.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 oh-chaeyeon/100_Same_Tree.js diff --git a/oh-chaeyeon/100_Same_Tree.js b/oh-chaeyeon/100_Same_Tree.js new file mode 100644 index 0000000..37b5b14 --- /dev/null +++ b/oh-chaeyeon/100_Same_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} p + * @param {TreeNode} q + * @return {boolean} + */ +var isSameTree = function (p, q) { + if (p === null && q === null) { + return true; + } + + if (p === null || q === null) { + return false; + } + + if (p.val !== q.val) { + return false; + } + + return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); +}; From ff58ada99772cbabc888fd1e185866458632046f Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 1 Oct 2024 21:50:29 +0900 Subject: [PATCH 11/51] 746 / Min Cost Climbing Stairs / Easy / 50m --- oh-chaeyeon/746_Min_Cost_Climbing_Stairs.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 oh-chaeyeon/746_Min_Cost_Climbing_Stairs.js diff --git a/oh-chaeyeon/746_Min_Cost_Climbing_Stairs.js b/oh-chaeyeon/746_Min_Cost_Climbing_Stairs.js new file mode 100644 index 0000000..f6c1d79 --- /dev/null +++ b/oh-chaeyeon/746_Min_Cost_Climbing_Stairs.js @@ -0,0 +1,17 @@ +/** + * @param {number[]} cost + * @return {number} + */ +var minCostClimbingStairs = function (cost) { + const n = cost.length; + const minCost = new Array(n + 1).fill(0); + + minCost[0] = 0; + minCost[1] = cost[0]; + + for (let i = 2; i <= n; i++) { + minCost[i] = cost[i - 1] + Math.min(minCost[i - 1], minCost[i - 2]); + } + + return Math.min(minCost[n], minCost[n - 1]); +}; From 7735104186bafdcd2c200477db2526b683a0a87d Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Wed, 9 Oct 2024 03:12:28 +0900 Subject: [PATCH 12/51] 49 / Group Anagrams / Medium / 43m --- oh-chaeyeon/49_Group_Anagrams.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 oh-chaeyeon/49_Group_Anagrams.js diff --git a/oh-chaeyeon/49_Group_Anagrams.js b/oh-chaeyeon/49_Group_Anagrams.js new file mode 100644 index 0000000..5cb274c --- /dev/null +++ b/oh-chaeyeon/49_Group_Anagrams.js @@ -0,0 +1,27 @@ +/** + * @param {string[]} strs + * @return {string[][]} + */ +var groupAnagrams = function (strs) { + const map = new Map(); + + for (let str of strs) { + const count = new Array(26).fill(0); + + for (let char of str) { + count[char.charCodeAt(0) - "a".charCodeAt(0)]++; + } + + const key = count.join("#"); + + if (map.has(key)) { + map.get(key).push(str); + } else { + map.set(key, [str]); + } + } + return Array.from(map.values()); +}; + +// Runtime : 117ms +// Memory : 63.07MB From 84ca09359e8d70827e81fdfb6532a92de202134b Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Wed, 9 Oct 2024 03:13:19 +0900 Subject: [PATCH 13/51] 190 / Reverse Bits / Easy / 33m --- oh-chaeyeon/190_Reverse_Bits.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 oh-chaeyeon/190_Reverse_Bits.js diff --git a/oh-chaeyeon/190_Reverse_Bits.js b/oh-chaeyeon/190_Reverse_Bits.js new file mode 100644 index 0000000..713c14d --- /dev/null +++ b/oh-chaeyeon/190_Reverse_Bits.js @@ -0,0 +1,16 @@ +/** + * @param {number} n - a positive integer + * @return {number} - a positive integer + */ +var reverseBits = function (n) { + let result = 0; + for (let i = 0; i < 32; i++) { + result <<= 1; + result |= n & 1; + n >>>= 1; + } + return result >>> 0; +}; + +// Runtime : 44ms +// Memory : 50.72MB From 2acdf2cf688d90cb637763cd6d263e9cd92eff3d Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Wed, 9 Oct 2024 03:14:05 +0900 Subject: [PATCH 14/51] 125 / Valid Palindrome / Easy / 28m --- oh-chaeyeon/125_Valid_Palindrome.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 oh-chaeyeon/125_Valid_Palindrome.js diff --git a/oh-chaeyeon/125_Valid_Palindrome.js b/oh-chaeyeon/125_Valid_Palindrome.js new file mode 100644 index 0000000..df09d37 --- /dev/null +++ b/oh-chaeyeon/125_Valid_Palindrome.js @@ -0,0 +1,12 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isPalindrome = function (s) { + let filteredStr = s.toLowerCase().replace(/[^a-z0-9]/g, ""); + let reversedStr = filteredStr.split("").reverse().join(""); + return filteredStr === reversedStr; +}; + +// Runtime : 58ms +// Memory : 52.88MB From 4937727b2705b0583c1e231305c70433c460f898 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Wed, 9 Oct 2024 03:15:22 +0900 Subject: [PATCH 15/51] 322 / Coin Change / Medium / 1h 18m --- oh-chaeyeon/322_Coin_Change.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 oh-chaeyeon/322_Coin_Change.js diff --git a/oh-chaeyeon/322_Coin_Change.js b/oh-chaeyeon/322_Coin_Change.js new file mode 100644 index 0000000..a534aae --- /dev/null +++ b/oh-chaeyeon/322_Coin_Change.js @@ -0,0 +1,20 @@ +/** + * @param {number[]} coins + * @param {number} amount + * @return {number} + */ +var coinChange = function (coins, amount) { + const dp = new Array(amount + 1).fill(Infinity); + dp[0] = 0; + + for (let coin of coins) { + for (let i = coin; i <= amount; i++) { + dp[i] = Math.min(dp[i], dp[i - coin] + 1); + } + } + + return dp[amount] === Infinity ? -1 : dp[amount]; +}; + +// Runtime : 83ms +// Memory : 53.06MB From 2daaa4c791afe2dfde8f716843c64b7311d4831a Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Wed, 9 Oct 2024 03:16:04 +0900 Subject: [PATCH 16/51] 206 / Reverse Linked list / Easy / 27m --- oh-chaeyeon/206_Reverse_Linked_list.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 oh-chaeyeon/206_Reverse_Linked_list.js diff --git a/oh-chaeyeon/206_Reverse_Linked_list.js b/oh-chaeyeon/206_Reverse_Linked_list.js new file mode 100644 index 0000000..88c9237 --- /dev/null +++ b/oh-chaeyeon/206_Reverse_Linked_list.js @@ -0,0 +1,26 @@ +/** + * 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 prev = null; + + while (head) { + let temp = head.next; + head.next = prev; + prev = head; + head = temp; + } + + return prev; +}; + +// Runtime : 62ms +// Memory : 50.97MB From 64fd3b5b7ecda2af23632353e3d1eac90ea8413f Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 15 Oct 2024 22:41:49 +0900 Subject: [PATCH 17/51] 1 / Two Sum / Easy / 15m 55s --- oh-chaeyeon/1_Two_Sum.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 oh-chaeyeon/1_Two_Sum.js diff --git a/oh-chaeyeon/1_Two_Sum.js b/oh-chaeyeon/1_Two_Sum.js new file mode 100644 index 0000000..1976a78 --- /dev/null +++ b/oh-chaeyeon/1_Two_Sum.js @@ -0,0 +1,19 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function (nums, target) { + let map = new Map(); + for (let i = 0; i < nums.length; i++) { + let requiredValue = target - nums[i]; + if (map.has(requiredValue)) { + return [map.get(requiredValue), i]; + } + map.set(nums[i], i); + } +}; + +// Time taken : 15m 55s +// Runtime : 51ms +// Memory : 51.54MB From 86623d5f89ed829d54f8b1708064677bba23fd49 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 15 Oct 2024 22:42:25 +0900 Subject: [PATCH 18/51] 104 / Maximum Depth of Binary Tree / Easy / 11m 46s --- .../104_Maximum_Depth_of_Binary_Tree.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 oh-chaeyeon/104_Maximum_Depth_of_Binary_Tree.js diff --git a/oh-chaeyeon/104_Maximum_Depth_of_Binary_Tree.js b/oh-chaeyeon/104_Maximum_Depth_of_Binary_Tree.js new file mode 100644 index 0000000..a3e1766 --- /dev/null +++ b/oh-chaeyeon/104_Maximum_Depth_of_Binary_Tree.js @@ -0,0 +1,26 @@ +/** + * 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 === null) { + return 0; + } + + let leftDepth = maxDepth(root.left); + let rightDepth = maxDepth(root.right); + + return Math.max(leftDepth, rightDepth) + 1; +}; + +// Time taken : 11m 46s +// Runtime : 59ms +// Memory : 51.59MB From 2831c872116395b351efa0088cc52679995f22cd Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 15 Oct 2024 22:42:52 +0900 Subject: [PATCH 19/51] 141 / Linked List Cycle / Easy / 37m 26s --- oh-chaeyeon/141_Linked_List_Cycle.js | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 oh-chaeyeon/141_Linked_List_Cycle.js diff --git a/oh-chaeyeon/141_Linked_List_Cycle.js b/oh-chaeyeon/141_Linked_List_Cycle.js new file mode 100644 index 0000000..bd371ed --- /dev/null +++ b/oh-chaeyeon/141_Linked_List_Cycle.js @@ -0,0 +1,30 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ + +/** + * @param {ListNode} head + * @return {boolean} + */ +var hasCycle = function (head) { + let visited = new Set(); + let current = head; + + while (current !== null) { + if (visited.has(current)) { + return true; + } + visited.add(current); + current = current.next; + } + + return false; +}; + +// Time taken : 37m 26s +// Runtime : 67ms +// Memory : 52.91MB From f86608ebada6830c8e067f5e77028cd4047b2b5d Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 15 Oct 2024 22:43:14 +0900 Subject: [PATCH 20/51] 202 /Happy Number / Easy / 20m 6s --- oh-chaeyeon/202_Happy_Number.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 oh-chaeyeon/202_Happy_Number.js diff --git a/oh-chaeyeon/202_Happy_Number.js b/oh-chaeyeon/202_Happy_Number.js new file mode 100644 index 0000000..e8b03f3 --- /dev/null +++ b/oh-chaeyeon/202_Happy_Number.js @@ -0,0 +1,24 @@ +/** + * @param {number} n + * @return {boolean} + */ +var isHappy = function (n) { + const record = new Set(); + + while (n !== 1) { + if (record.has(n)) { + return false; + } + record.add(n); + + n = n + .toString() + .split("") + .reduce((sum, digit) => sum + digit * digit, 0); + } + return true; +}; + +// Time taken : 20m 6s +// Runtime : 55ms +// Memory : 50.80MB From e0fccd7d35004ce80a971d6818ed4bf87091f3bd Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 15 Oct 2024 22:43:36 +0900 Subject: [PATCH 21/51] 20 / Valid Parentheses / Easy / 18m 24s --- oh-chaeyeon/20_Valid_Parentheses.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 oh-chaeyeon/20_Valid_Parentheses.js diff --git a/oh-chaeyeon/20_Valid_Parentheses.js b/oh-chaeyeon/20_Valid_Parentheses.js new file mode 100644 index 0000000..15ecd2e --- /dev/null +++ b/oh-chaeyeon/20_Valid_Parentheses.js @@ -0,0 +1,29 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isValid = function (s) { + const stack = []; + const map = { + "(": ")", + "{": "}", + "[": "]", + }; + + for (let char of s) { + if (char in map) { + stack.push(char); + } else { + const last = stack.pop(); + if (map[last] !== char) { + return false; + } + } + } + + return stack.length === 0; +}; + +// Time taken : 18m 24s +// Runtime : 55ms +// Memory : 50.87MB From 61753aaeae4dacbacc732b3cec202e78d98a4834 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 22 Oct 2024 22:43:39 +0900 Subject: [PATCH 22/51] 338 / Counting Bits / Easy / 16m --- oh-chaeyeon/338_Counting_Bits.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 oh-chaeyeon/338_Counting_Bits.js diff --git a/oh-chaeyeon/338_Counting_Bits.js b/oh-chaeyeon/338_Counting_Bits.js new file mode 100644 index 0000000..ee14125 --- /dev/null +++ b/oh-chaeyeon/338_Counting_Bits.js @@ -0,0 +1,15 @@ +/** + * @param {number} n + * @return {number[]} + */ +var countBits = function (n) { + const bit = new Array(n + 1).fill(0); + for (let i = 0; i <= n; i++) { + bit[i] = bit[i >> 1] + (i & 1); + } + return bit; +}; + +// Time taken : 16m +// Runtime : 2ms +// Memory : 56.14MB From 47ea9b01e0eabc5c4e8a2910cc4ccdcbe0d30ce5 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 22 Oct 2024 22:44:08 +0900 Subject: [PATCH 23/51] 70 / Climbing Stairs / Easy / 16m 30s --- oh-chaeyeon/70_Climbing_Stairs.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 oh-chaeyeon/70_Climbing_Stairs.js diff --git a/oh-chaeyeon/70_Climbing_Stairs.js b/oh-chaeyeon/70_Climbing_Stairs.js new file mode 100644 index 0000000..57ca9b1 --- /dev/null +++ b/oh-chaeyeon/70_Climbing_Stairs.js @@ -0,0 +1,23 @@ +/** + * @param {number} n + * @return {number} + */ +var climbStairs = function (n) { + if (n === 1) return 1; + if (n === 2) return 2; + + let prev1 = 2; + let prev2 = 1; + + for (let i = 3; i <= n; i++) { + let current = prev1 + prev2; + prev2 = prev1; + prev1 = current; + } + + return prev1; +}; + +// Time taken : 16m 30s +// Runtime : 0ms +// Memory : 49.04MB From f72bab80458d9c93f6d9a5641056930ae13fb7b4 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 22 Oct 2024 22:44:29 +0900 Subject: [PATCH 24/51] 21 / Merge two Sorted Lists / Easy / 40m 32s --- oh-chaeyeon/21_Merge_two_Sorted_Lists.js | 70 ++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 oh-chaeyeon/21_Merge_two_Sorted_Lists.js diff --git a/oh-chaeyeon/21_Merge_two_Sorted_Lists.js b/oh-chaeyeon/21_Merge_two_Sorted_Lists.js new file mode 100644 index 0000000..7976f42 --- /dev/null +++ b/oh-chaeyeon/21_Merge_two_Sorted_Lists.js @@ -0,0 +1,70 @@ +//풀이방법1. 두 리스트를 배열로 변환 후 정렬하는 방식 +// Runtime : 4ms +// Memory : 51.24MB +/** + * 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) { + let arr = []; + + while (list1 !== null) { + arr.push(list1.val); + list1 = list1.next; + } + + while (list2 !== null) { + arr.push(list2.val); + list2 = list2.next; + } + + arr.sort((a, b) => a - b); + + let dummy = new ListNode(); + let current = dummy; + + for (let val of arr) { + current.next = new ListNode(val); + current = current.next; + } + + return dummy.next; +}; + +//풀이방법2. 재귀를 활용한 방식 +// Runtime : 0ms +// Memory : 52.40MB +/** + * 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 === null) return list2; + if (list2 === null) return list1; + + if (list1.val <= list2.val) { + list1.next = mergeTwoLists(list1.next, list2); + return list1; + } else { + list2.next = mergeTwoLists(list1, list2.next); + return list2; + } +}; + +// Time taken : 40m 32s From d3637a1ca829502809041e126f3fe64bfd03afc4 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 22 Oct 2024 22:44:59 +0900 Subject: [PATCH 25/51] 48 / Rotate Image / Medium / 36m 15s --- oh-chaeyeon/48_Rotate_Image.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 oh-chaeyeon/48_Rotate_Image.js diff --git a/oh-chaeyeon/48_Rotate_Image.js b/oh-chaeyeon/48_Rotate_Image.js new file mode 100644 index 0000000..07b1f43 --- /dev/null +++ b/oh-chaeyeon/48_Rotate_Image.js @@ -0,0 +1,21 @@ +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +var rotate = function (matrix) { + const n = matrix.length; + + for (let i = 0; i < n; i++) { + for (let j = i + 1; j < n; j++) { + [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]]; + } + } + + for (let i = 0; i < n; i++) { + matrix[i].reverse(); + } +}; + +// Time taken : 36m 15s +// Runtime : 0ms +// Memory : 49.28MB From 91dc0f0561c3cd6d7e2b4ad8b75673aabfb06e28 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 22 Oct 2024 22:45:23 +0900 Subject: [PATCH 26/51] 1046 / Last Stone Weight / Easy / 7m 51s --- oh-chaeyeon/1046_Last_Stone_Weight.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 oh-chaeyeon/1046_Last_Stone_Weight.js diff --git a/oh-chaeyeon/1046_Last_Stone_Weight.js b/oh-chaeyeon/1046_Last_Stone_Weight.js new file mode 100644 index 0000000..1bb55dd --- /dev/null +++ b/oh-chaeyeon/1046_Last_Stone_Weight.js @@ -0,0 +1,21 @@ +/** + * @param {number[]} stones + * @return {number} + */ +var lastStoneWeight = function (stones) { + while (stones.length > 1) { + stones.sort((a, b) => b - a); + const big1 = stones.shift(); + const big2 = stones.shift(); + + if (big1 !== big2) { + stones.push(big1 - big2); + } + } + + return stones.length === 0 ? 0 : stones[0]; +}; + +// Time taken : 7m 51s +// Runtime : 2ms +// Memory : 51.29MB From 1f60059360f788604a3c00000caf03c82fb04e6a Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 29 Oct 2024 21:34:15 +0900 Subject: [PATCH 27/51] 271 / Encode and Decode Strings / Medium / 14m --- oh-chaeyeon/271_Encode_and_Decode_Strings.js | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 oh-chaeyeon/271_Encode_and_Decode_Strings.js diff --git a/oh-chaeyeon/271_Encode_and_Decode_Strings.js b/oh-chaeyeon/271_Encode_and_Decode_Strings.js new file mode 100644 index 0000000..39a43f1 --- /dev/null +++ b/oh-chaeyeon/271_Encode_and_Decode_Strings.js @@ -0,0 +1,33 @@ +class Solution { + /** + * @param {string[]} strs + * @returns {string} + */ + encode(strs) { + let encodedStr = ""; + for (const str of strs) { + encodedStr += str.length + "#" + str; + } + return encodedStr; + } + + /** + * @param {string} str + * @returns {string[]} + */ + decode(str) { + const decodedList = []; + let i = 0; + + while (i < str.length) { + let j = i; + while (str[j] !== "#") j++; + const len = parseInt(str.slice(i, j)); + decodedList.push(str.slice(j + 1, j + 1 + len)); + i = j + 1 + len; + } + return decodedList; + } +} + +// Time taken : 14m From 9de1b77dd7229aa018a226a46fbc2f26db6876a5 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 29 Oct 2024 21:35:25 +0900 Subject: [PATCH 28/51] 198 / House Robber / Medium / 26m 36s --- oh-chaeyeon/198_House_Robber.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 oh-chaeyeon/198_House_Robber.js diff --git a/oh-chaeyeon/198_House_Robber.js b/oh-chaeyeon/198_House_Robber.js new file mode 100644 index 0000000..909e363 --- /dev/null +++ b/oh-chaeyeon/198_House_Robber.js @@ -0,0 +1,23 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var rob = function (nums) { + if (nums.length === 0) return 0; + if (nums.length === 1) return nums[0]; + + let prev2 = 0; + let prev1 = nums[0]; + + for (let i = 1; i < nums.length; i++) { + let current = Math.max(prev1, nums[i] + prev2); + prev2 = prev1; + prev1 = current; + } + + return prev1; +}; + +// Time taken : 26m 36s +// Runtime : 0ms +// Memory : 49.07MB From f031a31fc7a916c1ffde2e6cd1e52b7d5a7286ab Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 29 Oct 2024 21:35:57 +0900 Subject: [PATCH 29/51] 66 / Plus One / Easy / 9m 28s --- oh-chaeyeon/66_Plus_One.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 oh-chaeyeon/66_Plus_One.js diff --git a/oh-chaeyeon/66_Plus_One.js b/oh-chaeyeon/66_Plus_One.js new file mode 100644 index 0000000..acd91db --- /dev/null +++ b/oh-chaeyeon/66_Plus_One.js @@ -0,0 +1,20 @@ +/** + * @param {number[]} digits + * @return {number[]} + */ +var plusOne = function (digits) { + for (let i = digits.length - 1; i >= 0; i--) { + if (digits[i] < 9) { + digits[i] += 1; + return digits; + } + + digits[i] = 0; + } + + return [1, ...digits]; +}; + +// Time taken : 9m 28s +// Runtime : 0ms +// Memory : 48.85MB From 38a2fe1078e998ca3ea54fbfe55545c3452f44fa Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 29 Oct 2024 21:36:37 +0900 Subject: [PATCH 30/51] 703 / Kth Largest Element in a Stream / Easy / 40m 51s --- .../703_Kth_Largest_Element_in_a_Stream.js | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 oh-chaeyeon/703_Kth_Largest_Element_in_a_Stream.js diff --git a/oh-chaeyeon/703_Kth_Largest_Element_in_a_Stream.js b/oh-chaeyeon/703_Kth_Largest_Element_in_a_Stream.js new file mode 100644 index 0000000..4b510cb --- /dev/null +++ b/oh-chaeyeon/703_Kth_Largest_Element_in_a_Stream.js @@ -0,0 +1,39 @@ +/** + * @param {number} k + * @param {number[]} nums + */ +var KthLargest = function (k, nums) { + this.k = k; + this.minHeap = new MinPriorityQueue(); + + for (let num of nums) { + this.minHeap.enqueue(num); + if (this.minHeap.size() > k) { + this.minHeap.dequeue(); + } + } +}; + +/** + * @param {number} val + * @return {number} + */ +KthLargest.prototype.add = function (val) { + if (this.minHeap.size() < this.k || val > this.minHeap.front().element) { + this.minHeap.enqueue(val); + if (this.minHeap.size() > this.k) { + this.minHeap.dequeue(); + } + } + return this.minHeap.front().element; +}; + +/** + * Your KthLargest object will be instantiated and called as such: + * var obj = new KthLargest(k, nums) + * var param_1 = obj.add(val) + */ + +// Time taken : 40m 51s +// Runtime : 51ms +// Memory : 64.8MB From 5210093a549f43c914c4670cea5c9456f270d3ba Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 29 Oct 2024 21:37:08 +0900 Subject: [PATCH 31/51] 704 / Binary Search / Easy / 9m 26s --- oh-chaeyeon/704_Binary_Search.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 oh-chaeyeon/704_Binary_Search.js diff --git a/oh-chaeyeon/704_Binary_Search.js b/oh-chaeyeon/704_Binary_Search.js new file mode 100644 index 0000000..26bd6ff --- /dev/null +++ b/oh-chaeyeon/704_Binary_Search.js @@ -0,0 +1,27 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +var search = function (nums, target) { + let left = 0; + let right = nums.length - 1; + + while (left <= right) { + let mid = Math.floor(left + (right - left) / 2); + + if (nums[mid] === target) { + return mid; + } else if (nums[mid] < target) { + left = mid + 1; + } else { + right = mid - 1; + } + } + + return -1; +}; + +// Time taken : 9m 26s +// Runtime : 0ms +// Memory : 53.13MB From 59e79d83b50e4bb1047ffbfca5b4b7f534c6d7c3 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 5 Nov 2024 21:46:45 +0900 Subject: [PATCH 32/51] 136 / Single Number / Easy / 4m 52s --- oh-chaeyeon/136_Single_Number.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 oh-chaeyeon/136_Single_Number.js diff --git a/oh-chaeyeon/136_Single_Number.js b/oh-chaeyeon/136_Single_Number.js new file mode 100644 index 0000000..f8b0389 --- /dev/null +++ b/oh-chaeyeon/136_Single_Number.js @@ -0,0 +1,17 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var singleNumber = function (nums) { + let result = 0; + + for (let num of nums) { + result ^= num; + } + + return result; +}; + +// Time taken : 4m 52s +// Runtime : 3ms +// Memory : 54.06MB From df48108d849672c64eb72af705caf92b0c37e757 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 5 Nov 2024 21:47:19 +0900 Subject: [PATCH 33/51] 110 / Balanced Binary Tree / Easy / 16m 44s --- oh-chaeyeon/110_Balanced_Binary_Tree.js | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 oh-chaeyeon/110_Balanced_Binary_Tree.js diff --git a/oh-chaeyeon/110_Balanced_Binary_Tree.js b/oh-chaeyeon/110_Balanced_Binary_Tree.js new file mode 100644 index 0000000..1842245 --- /dev/null +++ b/oh-chaeyeon/110_Balanced_Binary_Tree.js @@ -0,0 +1,35 @@ +/** + * 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) { + const checkHeight = (node) => { + if (!node) return 0; + + const leftHeight = checkHeight(node.left); + if (leftHeight === -1) return -1; + + const rightHeight = checkHeight(node.right); + if (rightHeight === -1) return -1; + + if (Math.abs(leftHeight - rightHeight) > 1) { + return -1; + } + + return Math.max(leftHeight, rightHeight) + 1; + }; + + return checkHeight(root) !== -1; +}; + +// Time taken : 16m 44s +// Runtime : 1ms +// Memory : 53.60MB From bf2bb0e0a46ca3dcd976268fcc59a7cea9eaf4e2 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 5 Nov 2024 21:47:55 +0900 Subject: [PATCH 34/51] 54 / Spiral Matrix / Medium / 53m 19s --- oh-chaeyeon/54_Spiral_Matrix.js | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 oh-chaeyeon/54_Spiral_Matrix.js diff --git a/oh-chaeyeon/54_Spiral_Matrix.js b/oh-chaeyeon/54_Spiral_Matrix.js new file mode 100644 index 0000000..cc03e92 --- /dev/null +++ b/oh-chaeyeon/54_Spiral_Matrix.js @@ -0,0 +1,46 @@ +/** + * @param {number[][]} matrix + * @return {number[]} + */ +var spiralOrder = function (matrix) { + const result = []; + + if (matrix.length === 0) return result; + + let top = 0; + let bottom = matrix.length - 1; + let left = 0; + let right = matrix[0].length - 1; + + while (top <= bottom && left <= right) { + for (let i = left; i <= right; i++) { + result.push(matrix[top][i]); + } + top++; + + for (let i = top; i <= bottom; i++) { + result.push(matrix[i][right]); + } + right--; + + if (top <= bottom) { + for (let i = right; i >= left; i--) { + result.push(matrix[bottom][i]); + } + bottom--; + } + + if (left <= right) { + for (let i = bottom; i >= top; i--) { + result.push(matrix[i][left]); + } + left++; + } + } + + return result; +}; + +// Time taken : 53m 19s +// Runtime : 0ms +// Memory : 48.79MB From d75f703155fea51289a4fa979461537c9a1515b2 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 5 Nov 2024 21:48:40 +0900 Subject: [PATCH 35/51] 973 / K Closest points to Origin / Medium / 12m 42s --- oh-chaeyeon/973_K_Closest_points_to_Origin.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 oh-chaeyeon/973_K_Closest_points_to_Origin.js diff --git a/oh-chaeyeon/973_K_Closest_points_to_Origin.js b/oh-chaeyeon/973_K_Closest_points_to_Origin.js new file mode 100644 index 0000000..8871b29 --- /dev/null +++ b/oh-chaeyeon/973_K_Closest_points_to_Origin.js @@ -0,0 +1,18 @@ +/** + * @param {number[][]} points + * @param {number} k + * @return {number[][]} + */ +var kClosest = function (points, k) { + points.sort((a, b) => { + const A = a[0] * a[0] + a[1] * a[1]; + const B = b[0] * b[0] + b[1] * b[1]; + return A - B; + }); + + return points.slice(0, k); +}; + +// Time taken : 12m 42s +// Runtime : 34ms +// Memory : 62.73MB From eae2d4ceb2f0f3e2cb4e152af9e3d88875c61afd Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 5 Nov 2024 21:49:17 +0900 Subject: [PATCH 36/51] 155 / Min Stack / Medium / 1h 2m 56s --- oh-chaeyeon/155_Min_Stack.js | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 oh-chaeyeon/155_Min_Stack.js diff --git a/oh-chaeyeon/155_Min_Stack.js b/oh-chaeyeon/155_Min_Stack.js new file mode 100644 index 0000000..425af76 --- /dev/null +++ b/oh-chaeyeon/155_Min_Stack.js @@ -0,0 +1,55 @@ +var MinStack = function () { + this.stack = []; + this.minStack = []; +}; + +/** + * @param {number} val + * @return {void} + */ +MinStack.prototype.push = function (val) { + this.stack.push(val); + if ( + this.minStack.length === 0 || + val <= this.minStack[this.minStack.length - 1] + ) { + this.minStack.push(val); + } +}; + +/** + * @return {void} + */ +MinStack.prototype.pop = function () { + const val = this.stack.pop(); + if (val === this.minStack[this.minStack.length - 1]) { + this.minStack.pop(); + } +}; + +/** + * @return {number} + */ +MinStack.prototype.top = function () { + return this.stack[this.stack.length - 1]; +}; + +/** + * @return {number} + */ +MinStack.prototype.getMin = function () { + return this.minStack[this.minStack.length - 1]; +}; + +/** + * 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() + */ + +// Time taken : 1h 2m 56s +// Runtime : 5ms +// Memory : 59.62MB From d1ae91938fd3a06c8a9e24b20f66cf35d26dba72 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Wed, 13 Nov 2024 00:13:02 +0900 Subject: [PATCH 37/51] 128 / Longest consecutive sequence / Medium / 31m 17s --- .../128_Longest_consecutive_sequence.js | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 oh-chaeyeon/128_Longest_consecutive_sequence.js diff --git a/oh-chaeyeon/128_Longest_consecutive_sequence.js b/oh-chaeyeon/128_Longest_consecutive_sequence.js new file mode 100644 index 0000000..89d1eba --- /dev/null +++ b/oh-chaeyeon/128_Longest_consecutive_sequence.js @@ -0,0 +1,25 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var longestConsecutive = function (nums) { + const numSet = new Set(nums); + let max = 0; + + for (let num of numSet) { + if (!numSet.has(num - 1)) { + let length = 1; + + while (numSet.has(num + length)) { + length += 1; + } + + max = Math.max(max, length); + } + } + return max; +}; + +// Time taken : 31m 17s +// Runtime : 35ms +// Memory : 72.97MB From db6e78416de663f4e9680c72a4285314b6e707fa Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Wed, 13 Nov 2024 00:13:32 +0900 Subject: [PATCH 38/51] 371 / Sum of two Integers / Medium / 15m 48s --- oh-chaeyeon/371_Sum_of_two_Integers.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 oh-chaeyeon/371_Sum_of_two_Integers.js diff --git a/oh-chaeyeon/371_Sum_of_two_Integers.js b/oh-chaeyeon/371_Sum_of_two_Integers.js new file mode 100644 index 0000000..5703597 --- /dev/null +++ b/oh-chaeyeon/371_Sum_of_two_Integers.js @@ -0,0 +1,22 @@ +/** + * @param {number} a + * @param {number} b + * @return {number} + */ +var getSum = function (a, b) { + if (a === 0) return b; + if (b === 0) return a; + + while (b != 0) { + let sum = a ^ b; + let carry = (a & b) << 1; + + a = sum; + b = carry; + } + return a; +}; + +// Time taken : 15m 48s +// Runtime : 0ms +// Memory : 49.15MB From efe5ca20fbf9020767975d58e1f9e1dfe675b44d Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Wed, 13 Nov 2024 00:13:59 +0900 Subject: [PATCH 39/51] 208 / Implement Trie Prefix Tree / Medium / 57m 29s --- oh-chaeyeon/208_Implement_Trie_Prefix_Tree.js | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 oh-chaeyeon/208_Implement_Trie_Prefix_Tree.js diff --git a/oh-chaeyeon/208_Implement_Trie_Prefix_Tree.js b/oh-chaeyeon/208_Implement_Trie_Prefix_Tree.js new file mode 100644 index 0000000..9dd97ec --- /dev/null +++ b/oh-chaeyeon/208_Implement_Trie_Prefix_Tree.js @@ -0,0 +1,60 @@ +var Trie = function () { + this.root = {}; +}; + +/** + * @param {string} word + * @return {void} + */ +Trie.prototype.insert = function (word) { + let node = this.root; + for (let char of word) { + if (!node[char]) { + node[char] = {}; + } + node = node[char]; + } + node.isEnd = true; +}; + +/** + * @param {string} word + * @return {boolean} + */ +Trie.prototype.search = function (word) { + let node = this.root; + for (let char of word) { + if (!node[char]) { + return false; + } + node = node[char]; + } + return node.isEnd === true; +}; + +/** + * @param {string} prefix + * @return {boolean} + */ +Trie.prototype.startsWith = function (prefix) { + let node = this.root; + for (let char of prefix) { + if (!node[char]) { + return false; + } + node = node[char]; + } + return true; +}; + +/** + * Your Trie object will be instantiated and called as such: + * var obj = new Trie() + * obj.insert(word) + * var param_2 = obj.search(word) + * var param_3 = obj.startsWith(prefix) + */ + +// Time taken : 57m 29s +// Runtime : 50ms +// Memory : 64.72MB From cd94c0294584b1c559621ee0a6a946c6a9720217 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Wed, 13 Nov 2024 00:14:22 +0900 Subject: [PATCH 40/51] 226 / Invert Binary Tree / Easy / 30m 34s --- oh-chaeyeon/226_Invert_Binary_Tree.js | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 oh-chaeyeon/226_Invert_Binary_Tree.js diff --git a/oh-chaeyeon/226_Invert_Binary_Tree.js b/oh-chaeyeon/226_Invert_Binary_Tree.js new file mode 100644 index 0000000..2dc04ea --- /dev/null +++ b/oh-chaeyeon/226_Invert_Binary_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 + * @return {TreeNode} + */ +var invertTree = function (root) { + if (root === null) { + return null; + } + + let temp = root.left; + root.left = root.right; + root.right = temp; + + invertTree(root.left); + invertTree(root.right); + + return root; +}; + +// Time taken : 30m 34s +// Runtime : 0ms +// Memory : 49.28MB From bff831052f94942d65b01fa338fb6196fc5d5abe Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Wed, 13 Nov 2024 00:14:45 +0900 Subject: [PATCH 41/51] 543 / Diameter of Binary Tree / Easy / 31m 55s --- oh-chaeyeon/543_Diameter_of_Binary_Tree.js | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 oh-chaeyeon/543_Diameter_of_Binary_Tree.js diff --git a/oh-chaeyeon/543_Diameter_of_Binary_Tree.js b/oh-chaeyeon/543_Diameter_of_Binary_Tree.js new file mode 100644 index 0000000..8aca5b3 --- /dev/null +++ b/oh-chaeyeon/543_Diameter_of_Binary_Tree.js @@ -0,0 +1,33 @@ +/** + * 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 diameter = 0; + + const Tree = (node) => { + if (!node) return 0; + + let leftHeight = Tree(node.left); + let rightHeight = Tree(node.right); + + diameter = Math.max(diameter, leftHeight + rightHeight); + + return Math.max(leftHeight, rightHeight) + 1; + }; + + Tree(root); + return diameter; +}; + +// Time taken : 31m 55s +// Runtime : 2ms +// Memory : 55.43MB From 5f23ce0cd986706f99c73816c292dd40d11630fc Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 19 Nov 2024 21:13:04 +0900 Subject: [PATCH 42/51] 201 / Binary Tree Level Order Traversal / Medium / 21m 49s --- .../201_Binary_Tree_Level_Order_Traversal.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 oh-chaeyeon/201_Binary_Tree_Level_Order_Traversal.js diff --git a/oh-chaeyeon/201_Binary_Tree_Level_Order_Traversal.js b/oh-chaeyeon/201_Binary_Tree_Level_Order_Traversal.js new file mode 100644 index 0000000..7cf6b41 --- /dev/null +++ b/oh-chaeyeon/201_Binary_Tree_Level_Order_Traversal.js @@ -0,0 +1,33 @@ +/** + * 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) { + const result = []; + const Tree = (node, level) => { + if (!node) return; + + if (result.length === level) { + result.push([]); + } + + result[level].push(node.val); + Tree(node.left, level + 1); + Tree(node.right, level + 1); + }; + + Tree(root, 0); + return result; +}; + +// Time taken : 21m 49s +// Runtime : 1ms +// Memory : 55.59MB From 91c0914d51910f076964d04daac935024b309240 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 19 Nov 2024 21:13:37 +0900 Subject: [PATCH 43/51] 572 / Subtree Of Another Tree / Easy / 25m 09s --- oh-chaeyeon/572_Subtree_Of_Another_Tree.js | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 oh-chaeyeon/572_Subtree_Of_Another_Tree.js diff --git a/oh-chaeyeon/572_Subtree_Of_Another_Tree.js b/oh-chaeyeon/572_Subtree_Of_Another_Tree.js new file mode 100644 index 0000000..4ffffe7 --- /dev/null +++ b/oh-chaeyeon/572_Subtree_Of_Another_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} root + * @param {TreeNode} subRoot + * @return {boolean} + */ +var isSubtree = function (root, subRoot) { + if (!subRoot) return true; + if (!root) return false; + + if (root.val === subRoot.val) { + const checkSame = (node1, node2) => { + if (!node1 && !node2) return true; + if (!node1 || !node2) return false; + if (node1.val !== node2.val) return false; + + return ( + checkSame(node1.left, node2.left) && checkSame(node1.right, node2.right) + ); + }; + + if (checkSame(root, subRoot)) { + return true; + } + } + + return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot); +}; + +// Time taken : 25m 09s +// Runtime : 6ms +// Memory : 55.18MB From a5d9426d5cfaaa830f80fd65d38968e331de2fc4 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 19 Nov 2024 21:14:26 +0900 Subject: [PATCH 44/51] 213 / House Robber II / Medium / 20m 31s --- oh-chaeyeon/213_House_Robber_II.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 oh-chaeyeon/213_House_Robber_II.js diff --git a/oh-chaeyeon/213_House_Robber_II.js b/oh-chaeyeon/213_House_Robber_II.js new file mode 100644 index 0000000..b2e8140 --- /dev/null +++ b/oh-chaeyeon/213_House_Robber_II.js @@ -0,0 +1,27 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var rob = function (nums) { + const n = nums.length; + + if (n === 0) return 0; + if (n === 1) return nums[0]; + + const FindMoney = (houses) => { + let one = 0, + two = 0; + for (let money of houses) { + const temp = one; + one = Math.max(one, two + money); + two = temp; + } + return one; + }; + + return Math.max(FindMoney(nums.slice(0, n - 1)), FindMoney(nums.slice(1))); +}; + +// Time taken : 20m 31s +// Runtime : 0ms +// Memory : 48.34MB From 89099b63a4ced465d7ad7e8ce02f851421ca1f10 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 19 Nov 2024 21:14:51 +0900 Subject: [PATCH 45/51] 207 / Course Schedule / Medium / 21m 49s --- oh-chaeyeon/207_Course_Schedule.js | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 oh-chaeyeon/207_Course_Schedule.js diff --git a/oh-chaeyeon/207_Course_Schedule.js b/oh-chaeyeon/207_Course_Schedule.js new file mode 100644 index 0000000..cfdc143 --- /dev/null +++ b/oh-chaeyeon/207_Course_Schedule.js @@ -0,0 +1,35 @@ +/** + * @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 = new Array(numCourses).fill(0); + + const dfs = (course) => { + if (visited[course] === 1) return false; + if (visited[course] === 2) return true; + + visited[course] = 1; + for (const nextCourse of graph[course]) { + if (!dfs(nextCourse)) return false; + } + visited[course] = 2; + return true; + }; + + for (let i = 0; i < numCourses; i++) { + if (!dfs(i)) return false; + } + + return true; +}; + +// Time taken : 21m 49s +// Runtime : 14ms +// Memory : 55.35MB From fccee21e4994579a4e1624a0f62b3b85da51cef0 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 19 Nov 2024 21:15:14 +0900 Subject: [PATCH 46/51] 150 / Evaluate Reverse Polish Notation / Medium / 14m 58s --- .../150_Evaluate_Reverse_Polish_Notation.js | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 oh-chaeyeon/150_Evaluate_Reverse_Polish_Notation.js diff --git a/oh-chaeyeon/150_Evaluate_Reverse_Polish_Notation.js b/oh-chaeyeon/150_Evaluate_Reverse_Polish_Notation.js new file mode 100644 index 0000000..dd94b85 --- /dev/null +++ b/oh-chaeyeon/150_Evaluate_Reverse_Polish_Notation.js @@ -0,0 +1,38 @@ +/** + * @param {string[]} tokens + * @return {number} + */ +var evalRPN = function (tokens) { + const stack = []; + for (const token of tokens) { + if (!isNaN(token)) { + stack.push(Number(token)); + } else { + const b = stack.pop(); + const a = stack.pop(); + + let result; + switch (token) { + case "+": + result = a + b; + break; + case "-": + result = a - b; + break; + case "*": + result = a * b; + break; + case "/": + result = Math.trunc(a / b); + break; + } + stack.push(result); + } + } + + return stack.pop(); +}; + +// Time taken : 14m 58s +// Runtime : 2ms +// Memory : 51.92MB From 2720f06ae83aa28c5b8edf834e3766dcd532daa6 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 26 Nov 2024 21:59:36 +0900 Subject: [PATCH 47/51] 56 / Merge Intervals / Medium / 23m 25s --- oh-chaeyeon/56_Merge_Intervals.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 oh-chaeyeon/56_Merge_Intervals.js diff --git a/oh-chaeyeon/56_Merge_Intervals.js b/oh-chaeyeon/56_Merge_Intervals.js new file mode 100644 index 0000000..5b518dd --- /dev/null +++ b/oh-chaeyeon/56_Merge_Intervals.js @@ -0,0 +1,29 @@ +/** + * @param {number[][]} intervals + * @return {number[][]} + */ +var merge = function (intervals) { + if (intervals.length === 0) return []; + intervals.sort((a, b) => a[0] - b[0]); + + const merged = [intervals[0]]; + + for (let i = 1; i < intervals.length; i++) { + const current = intervals[i]; + + if (current[0] <= merged[merged.length - 1][1]) { + merged[merged.length - 1][1] = Math.max( + merged[merged.length - 1][1], + current[1] + ); + } else { + merged.push(current); + } + } + + return merged; +}; + +// Time taken : 23m 25s +// Runtime : 7ms +// Memory : 59.04MB From 13fc2c3a9fec64c79020dee326db6b91fa4d3812 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 26 Nov 2024 22:00:05 +0900 Subject: [PATCH 48/51] 1584 / Min Cost to Connect all points / Medium / 1h+ --- .../1584_Min_Cost_to_Connect_all_points.js | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 oh-chaeyeon/1584_Min_Cost_to_Connect_all_points.js diff --git a/oh-chaeyeon/1584_Min_Cost_to_Connect_all_points.js b/oh-chaeyeon/1584_Min_Cost_to_Connect_all_points.js new file mode 100644 index 0000000..7ca304b --- /dev/null +++ b/oh-chaeyeon/1584_Min_Cost_to_Connect_all_points.js @@ -0,0 +1,41 @@ +/** + * @param {number[][]} points + * @return {number} + */ +var minCostConnectPoints = function (points) { + const n = points.length; + const dist = new Array(n).fill(Infinity); + const visited = new Array(n).fill(false); + dist[0] = 0; + let cost = 0; + + for (let i = 0; i < n; i++) { + let minCost = Infinity; + let currentNode = -1; + + for (let j = 0; j < n; j++) { + if (!visited[j] && dist[j] < minCost) { + minCost = dist[j]; + currentNode = j; + } + } + + visited[currentNode] = true; + cost += minCost; + + for (let nextNode = 0; nextNode < n; nextNode++) { + if (!visited[nextNode]) { + const manhattanDistance = + Math.abs(points[currentNode][0] - points[nextNode][0]) + + Math.abs(points[currentNode][1] - points[nextNode][1]); + dist[nextNode] = Math.min(dist[nextNode], manhattanDistance); + } + } + } + + return cost; +}; + +// Time taken : 1h+ +// Runtime : 33ms +// Memory : 52.37MB From 78f1f2cfa1cd2046ab0818f9abe8f58cad0d9b56 Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 26 Nov 2024 22:00:31 +0900 Subject: [PATCH 49/51] 235 / Lowest Common ancestor of a Binary Search Tree / Medium / 9m 37s --- ...Common_ancestor_of_a_Binary_Search_Tree.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 oh-chaeyeon/235_Lowest_Common_ancestor_of_a_Binary_Search_Tree.js diff --git a/oh-chaeyeon/235_Lowest_Common_ancestor_of_a_Binary_Search_Tree.js b/oh-chaeyeon/235_Lowest_Common_ancestor_of_a_Binary_Search_Tree.js new file mode 100644 index 0000000..32182d3 --- /dev/null +++ b/oh-chaeyeon/235_Lowest_Common_ancestor_of_a_Binary_Search_Tree.js @@ -0,0 +1,30 @@ +/** + * 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) { + while (root) { + if (p.val < root.val && q.val < root.val) { + root = root.left; + } else if (p.val > root.val && q.val > root.val) { + root = root.right; + } else { + return root; + } + } + return null; +}; + +// Time taken : 9m 37s +// Runtime : 56ms +// Memory : 58.70MB From 812d16f4afb262425c9448bee403b136823043ed Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 26 Nov 2024 22:01:02 +0900 Subject: [PATCH 50/51] 139 / Word Break / Medium / 12m 49s --- oh-chaeyeon/139_Word_Break.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 oh-chaeyeon/139_Word_Break.js diff --git a/oh-chaeyeon/139_Word_Break.js b/oh-chaeyeon/139_Word_Break.js new file mode 100644 index 0000000..022de5f --- /dev/null +++ b/oh-chaeyeon/139_Word_Break.js @@ -0,0 +1,30 @@ +/** + * @param {string} s + * @param {string[]} wordDict + * @return {boolean} + */ +var wordBreak = function (s, wordDict) { + const wordSet = new Set(wordDict); + const dp = new Array(s.length + 1).fill(false); + dp[0] = true; + + for (let i = 1; i <= s.length; i++) { + for (const word of wordSet) { + const wordLength = word.length; + if ( + i >= wordLength && + dp[i - wordLength] && + s.substring(i - wordLength, i) === word + ) { + dp[i] = true; + break; + } + } + } + + return dp[s.length]; +}; + +// Time taken : 12m 49s +// Runtime : 3ms +// Memory : 51.24MB From 622454d73e5112526be12e84470650d80be87c5b Mon Sep 17 00:00:00 2001 From: oh-chaeyeon Date: Tue, 26 Nov 2024 22:01:33 +0900 Subject: [PATCH 51/51] 215 / Kth largest element in an Array / Medium / 30m+ --- .../215_Kth largest_element_in_an_Array.js | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 oh-chaeyeon/215_Kth largest_element_in_an_Array.js diff --git a/oh-chaeyeon/215_Kth largest_element_in_an_Array.js b/oh-chaeyeon/215_Kth largest_element_in_an_Array.js new file mode 100644 index 0000000..01dc25a --- /dev/null +++ b/oh-chaeyeon/215_Kth largest_element_in_an_Array.js @@ -0,0 +1,46 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var findKthLargest = function (nums, k) { + const maxHeap = new MaxPriorityQueue(); + + for (const num of nums) { + maxHeap.enqueue(num); + } + + for (let i = 0; i < k - 1; i++) { + maxHeap.dequeue(); + } + return maxHeap.front().element; +}; + +// Time taken : 16m 50s +// Runtime : 148ms +// Memory : 77.60MB + +//---------------------------------------------------- + +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var findKthLargest = function (nums, k) { + const offset = 10000; + const count = new Array(20001).fill(0); + + for (let num of nums) { + count[num + offset]++; + } + + let total = 0; + for (let i = count.length - 1; i >= 0; i--) { + total += count[i]; + if (total >= k) return i - offset; + } +}; + +// Runtime : 5ms +// Memory : 58.40MB