From 1907c3cdca074b285978beab3093ef65163b5bd5 Mon Sep 17 00:00:00 2001 From: Paik Date: Sun, 8 Dec 2024 17:02:47 +0900 Subject: [PATCH 1/7] feat: 217.Contains Duplicate --- contains-duplicate/ganu.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 contains-duplicate/ganu.js diff --git a/contains-duplicate/ganu.js b/contains-duplicate/ganu.js new file mode 100644 index 000000000..deeb8c753 --- /dev/null +++ b/contains-duplicate/ganu.js @@ -0,0 +1,12 @@ +// Time complexity: O(n) +// Space complexity: O(n) + +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function (nums) { + const set = new Set(nums); + + return set.size !== nums.length; +}; From c9407bbd9a4ae56d5dab1ea0acb8aa124d254c47 Mon Sep 17 00:00:00 2001 From: Paik Date: Sun, 8 Dec 2024 17:15:06 +0900 Subject: [PATCH 2/7] fix: Change file name --- contains-duplicate/{ganu.js => gwbaik9717.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename contains-duplicate/{ganu.js => gwbaik9717.js} (100%) diff --git a/contains-duplicate/ganu.js b/contains-duplicate/gwbaik9717.js similarity index 100% rename from contains-duplicate/ganu.js rename to contains-duplicate/gwbaik9717.js From 11e43f8032656d9782c8d7cb81614bd2a66118a9 Mon Sep 17 00:00:00 2001 From: Paik Date: Mon, 9 Dec 2024 11:26:59 +0900 Subject: [PATCH 3/7] feat: 220.Valid Palindrome --- valid-palindrome/gwbaik9717.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 valid-palindrome/gwbaik9717.js diff --git a/valid-palindrome/gwbaik9717.js b/valid-palindrome/gwbaik9717.js new file mode 100644 index 000000000..890929a89 --- /dev/null +++ b/valid-palindrome/gwbaik9717.js @@ -0,0 +1,17 @@ +// Time complexity: O(n) +// Space complexity: O(n) + +/** + * @param {string} s + * @return {boolean} + */ +var isPalindrome = function (s) { + const normalize = (s) => { + return s.toLowerCase().replace(/[^a-z0-9]/g, ""); + }; + + const normalized = normalize(s); + const reversed = normalized.split("").reverse().join(""); + + return normalized === reversed; +}; From 11b4bc53b46ddcae2b7aabe6fd948a1b4a563a7a Mon Sep 17 00:00:00 2001 From: Paik Date: Tue, 10 Dec 2024 08:12:22 +0900 Subject: [PATCH 4/7] feat: 347. Top K Frequent Elements --- top-k-frequent-elements/gwbaik9717.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 top-k-frequent-elements/gwbaik9717.js diff --git a/top-k-frequent-elements/gwbaik9717.js b/top-k-frequent-elements/gwbaik9717.js new file mode 100644 index 000000000..a539d1198 --- /dev/null +++ b/top-k-frequent-elements/gwbaik9717.js @@ -0,0 +1,24 @@ +// Time complexity: O(nlogn) +// Space complexity: O(n) + +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var topKFrequent = function (nums, k) { + const frequencyDict = new Map(); + + for (const num of nums) { + if (frequencyDict.has(num)) { + frequencyDict.set(num, frequencyDict.get(num) + 1); + } else { + frequencyDict.set(num, 1); + } + } + + const entries = [...frequencyDict.entries()]; + entries.sort((a, b) => b[1] - a[1]); + + return entries.slice(0, k).map((entry) => entry[0]); +}; From 1b3d9a0f6915b8837f393dfd4b0ebc57efe008b0 Mon Sep 17 00:00:00 2001 From: Paik Date: Wed, 11 Dec 2024 09:15:55 +0900 Subject: [PATCH 5/7] feat: 128. Longest Consecutive Sequence --- longest-consecutive-sequence/gwbaik9717.js | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 longest-consecutive-sequence/gwbaik9717.js diff --git a/longest-consecutive-sequence/gwbaik9717.js b/longest-consecutive-sequence/gwbaik9717.js new file mode 100644 index 000000000..0835fba7e --- /dev/null +++ b/longest-consecutive-sequence/gwbaik9717.js @@ -0,0 +1,30 @@ +// Time complexity: O(n) +// Space complexity: O(n) + +/** + * @param {number[]} nums + * @return {number} + */ +var longestConsecutive = function (nums) { + let answer = 0; + const consecutiveDict = new Map(); + + for (const num of nums) { + consecutiveDict.set(num, true); + } + + for (const num of nums) { + if (consecutiveDict.has(num - 1)) { + continue; + } + + let length = 1; + while (consecutiveDict.has(num + length)) { + length++; + } + + answer = Math.max(answer, length); + } + + return answer; +}; From 063cbd4e1bee05494751c7bbe68c695695902ff7 Mon Sep 17 00:00:00 2001 From: Paik Date: Thu, 12 Dec 2024 07:59:54 +0900 Subject: [PATCH 6/7] feat: 198. House Robber --- house-robber/gwbaik9717.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 house-robber/gwbaik9717.js diff --git a/house-robber/gwbaik9717.js b/house-robber/gwbaik9717.js new file mode 100644 index 000000000..e54c9c2e9 --- /dev/null +++ b/house-robber/gwbaik9717.js @@ -0,0 +1,18 @@ +// Time complexity: O(n) +// Space complexity: O(n) + +/** + * @param {number[]} nums + * @return {number} + */ +var rob = function (nums) { + const n = nums.length; + const dp = Array.from({ length: n + 1 }, () => 0); + dp[1] = nums[0]; + + for (let i = 2; i < n + 1; i++) { + dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]); + } + + return dp.at(-1); +}; From 73cd02d29e27a8b725a5dcc013000ce1c5f202d3 Mon Sep 17 00:00:00 2001 From: Gunwoo Baik Date: Fri, 13 Dec 2024 06:32:37 +0900 Subject: [PATCH 7/7] refactor: Remove unnecessary function Co-authored-by: Dongyeong Chon --- valid-palindrome/gwbaik9717.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/valid-palindrome/gwbaik9717.js b/valid-palindrome/gwbaik9717.js index 890929a89..4e2ac5871 100644 --- a/valid-palindrome/gwbaik9717.js +++ b/valid-palindrome/gwbaik9717.js @@ -6,11 +6,7 @@ * @return {boolean} */ var isPalindrome = function (s) { - const normalize = (s) => { - return s.toLowerCase().replace(/[^a-z0-9]/g, ""); - }; - - const normalized = normalize(s); + const normalized = s.toLowerCase().replace(/[^a-z0-9]/g, ""); const reversed = normalized.split("").reverse().join(""); return normalized === reversed;