From b487b4eadedf997b4d2b9f2f3b0e07928a20680f Mon Sep 17 00:00:00 2001 From: bus710 Date: Thu, 12 Dec 2024 17:32:02 -0800 Subject: [PATCH 01/10] contains duplicate --- contains-duplicate/bus710.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 contains-duplicate/bus710.go diff --git a/contains-duplicate/bus710.go b/contains-duplicate/bus710.go new file mode 100644 index 000000000..d62bc7aec --- /dev/null +++ b/contains-duplicate/bus710.go @@ -0,0 +1,14 @@ +package week01 + +func containsDuplicate(nums []int) bool { + dup := make(map[int]bool, 0) + for _, n := range nums { + if _, ok := dup[n]; !ok { + dup[n] = true + } else { + return true + } + } + + return false +} From 289c07e9378ca73055fe164be6fc3094dcb9fa4a Mon Sep 17 00:00:00 2001 From: bus710 Date: Thu, 12 Dec 2024 17:39:31 -0800 Subject: [PATCH 02/10] comments --- contains-duplicate/bus710.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/contains-duplicate/bus710.go b/contains-duplicate/bus710.go index d62bc7aec..c0f2179e3 100644 --- a/contains-duplicate/bus710.go +++ b/contains-duplicate/bus710.go @@ -1,5 +1,11 @@ +// 주어진 배열을 선형 순회 하므로, 시간 복잡도와 공간 복잡도 모두 O(n)일 것으로 생각합니다. + package week01 +// 주어진 배열 nums를 순회하며 준비한 맵에 표시를 하되, +// - 키값에 해당하는 값이 없으면 맵의 해당 키값에 true를 저장하고 +// - 키값에 해당하는 값이 있으면 즉시 true를 반환. +// - 순회 후에도 반환하지 않은 경우 중복이 발견되지 않았으므로 false를 반환. func containsDuplicate(nums []int) bool { dup := make(map[int]bool, 0) for _, n := range nums { From 0df1835df792853f0f29cf48ce134a563731d9b1 Mon Sep 17 00:00:00 2001 From: bus710 Date: Fri, 13 Dec 2024 09:53:14 -0800 Subject: [PATCH 03/10] add a new line --- contains-duplicate/bus710.go | 1 + 1 file changed, 1 insertion(+) diff --git a/contains-duplicate/bus710.go b/contains-duplicate/bus710.go index c0f2179e3..2a2c67338 100644 --- a/contains-duplicate/bus710.go +++ b/contains-duplicate/bus710.go @@ -18,3 +18,4 @@ func containsDuplicate(nums []int) bool { return false } + From 658e7452489720723e7a77ade648f6f5d23af118 Mon Sep 17 00:00:00 2001 From: Paik Date: Sun, 8 Dec 2024 17:02:47 +0900 Subject: [PATCH 04/10] 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 ac171d765858c9851ba98cf9075b6154762d4e79 Mon Sep 17 00:00:00 2001 From: Paik Date: Sun, 8 Dec 2024 17:15:06 +0900 Subject: [PATCH 05/10] 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 a23fadd0c74837ee42a36415889fae25c7180840 Mon Sep 17 00:00:00 2001 From: Paik Date: Mon, 9 Dec 2024 11:26:59 +0900 Subject: [PATCH 06/10] 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 765ba5af83ad9e088a20ee8e5729123976910465 Mon Sep 17 00:00:00 2001 From: Paik Date: Tue, 10 Dec 2024 08:12:22 +0900 Subject: [PATCH 07/10] 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 df69e0ef60f442d53cea4a5ed49b7687829228b9 Mon Sep 17 00:00:00 2001 From: Paik Date: Wed, 11 Dec 2024 09:15:55 +0900 Subject: [PATCH 08/10] 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 3da7d435d4acbd03326b28d02c5fb99fb7b87272 Mon Sep 17 00:00:00 2001 From: Paik Date: Thu, 12 Dec 2024 07:59:54 +0900 Subject: [PATCH 09/10] 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 3acda42a89f1169a4cb0e2883df70ac2e980d255 Mon Sep 17 00:00:00 2001 From: Gunwoo Baik Date: Fri, 13 Dec 2024 06:32:37 +0900 Subject: [PATCH 10/10] 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;