From f10cc73c4ea50445a521670d6eeeb86d14cb0a3b Mon Sep 17 00:00:00 2001 From: tree Date: Sat, 24 Feb 2024 16:45:34 +0900 Subject: [PATCH 1/6] solve topKFrequent v1 --- topKFrequent.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 topKFrequent.js diff --git a/topKFrequent.js b/topKFrequent.js new file mode 100644 index 0000000..503e798 --- /dev/null +++ b/topKFrequent.js @@ -0,0 +1,30 @@ +// Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. + +const topKFrequent = (nums, k) => { + const obj = {}; + const answer = []; + + for (let i = 0; i < nums.length; i++) { + const v = nums[i]; + if (obj[v]) { + obj[v] += 1; + } else { + obj[v] = 1; + } + } + + const keyValueArray = Object.entries(obj); + const sortedArray = keyValueArray.sort((a, b) => b[1] - a[1]); + + for (let i = 0; i < k; i++) { + const x = Number(sortedArray[i][0]); + answer.push(x); + } + + return answer; +}; + +const nums = [1, 1, 1, 2, 2, 3, 3, 3]; + +const k = 2; +const a = topKFrequent(nums, k); From 2998f33ece9deb75cd65dab7bc524f782ba55c30 Mon Sep 17 00:00:00 2001 From: tree Date: Sat, 24 Feb 2024 16:50:27 +0900 Subject: [PATCH 2/6] solve TopKFrequentElements v2 --- topKFrequent.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/topKFrequent.js b/topKFrequent.js index 503e798..4ae5b7b 100644 --- a/topKFrequent.js +++ b/topKFrequent.js @@ -2,7 +2,6 @@ const topKFrequent = (nums, k) => { const obj = {}; - const answer = []; for (let i = 0; i < nums.length; i++) { const v = nums[i]; @@ -13,14 +12,8 @@ const topKFrequent = (nums, k) => { } } - const keyValueArray = Object.entries(obj); - const sortedArray = keyValueArray.sort((a, b) => b[1] - a[1]); - - for (let i = 0; i < k; i++) { - const x = Number(sortedArray[i][0]); - answer.push(x); - } - + const sortedArray = Object.entries(obj).sort((a, b) => b[1] - a[1]); + const answer = sortedArray.slice(0, k).map(([num]) => Number(num)); return answer; }; From 1ae5ed6280664d058d9069c517ae64fc677dcf02 Mon Sep 17 00:00:00 2001 From: tree Date: Sat, 24 Feb 2024 16:57:55 +0900 Subject: [PATCH 3/6] add annotation --- topKFrequent.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/topKFrequent.js b/topKFrequent.js index 4ae5b7b..2c138d2 100644 --- a/topKFrequent.js +++ b/topKFrequent.js @@ -13,7 +13,12 @@ const topKFrequent = (nums, k) => { } const sortedArray = Object.entries(obj).sort((a, b) => b[1] - a[1]); - const answer = sortedArray.slice(0, k).map(([num]) => Number(num)); + + //[ [ '1', 3 ], [ '2', 2 ], [ '3', 3 ] ] + // 0:숫자 1:몇개가 존재하는지 + // [1] 번째 인덱스 () 순서대로 정렬. + + const answer = sortedArray.slice(0, k).map(([num, _]) => Number(num)); return answer; }; From 0ba6c4362c1e68482374114d3ef21287befd6135 Mon Sep 17 00:00:00 2001 From: tree Date: Sat, 24 Feb 2024 17:29:43 +0900 Subject: [PATCH 4/6] solve LongestConsecutive --- LongestConsecutiveSequence.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 LongestConsecutiveSequence.js diff --git a/LongestConsecutiveSequence.js b/LongestConsecutiveSequence.js new file mode 100644 index 0000000..28d8986 --- /dev/null +++ b/LongestConsecutiveSequence.js @@ -0,0 +1,27 @@ +// Given an unsorted array of integers nums, +// return the length of the longest consecutive elements sequence. +// You must write an algorithm that runs in O(n) time. + +const longestConsecutive = (nums) => { + if (!nums.length) return 0; + const x = [...new Set(nums)].sort((a, b) => a - b); + const answer = []; + let cnt = 0; + + x.forEach((v, i) => { + if (v + 1 === x[i + 1]) { + cnt += 1; + } else { + answer.push(cnt); + cnt = 0; + } + }); + + return Math.max(...answer) + 1; +}; + +const nums = [1, 2, 0, 1]; +const x = longestConsecutive(nums); +console.log(x); + +// https://leetcode.com/problems/longest-consecutive-sequence/description/ From d23e3c1fb57b90367b2a44aae4c65183d00e8c57 Mon Sep 17 00:00:00 2001 From: tree Date: Sat, 24 Feb 2024 17:30:08 +0900 Subject: [PATCH 5/6] cleat up file list --- Contains Duplicate.js => ContainsDuplicate.js | 2 ++ TwoSum.js | 2 ++ topKFrequent.js | 5 ++--- 3 files changed, 6 insertions(+), 3 deletions(-) rename Contains Duplicate.js => ContainsDuplicate.js (80%) diff --git a/ Contains Duplicate.js b/ContainsDuplicate.js similarity index 80% rename from Contains Duplicate.js rename to ContainsDuplicate.js index 1121971..184d33c 100644 --- a/ Contains Duplicate.js +++ b/ContainsDuplicate.js @@ -13,3 +13,5 @@ const containsDuplicate = function (nums) { return newArr.some((x) => x >= 2); }; + +// https://leetcode.com/problems/contains-duplicate/description/ diff --git a/TwoSum.js b/TwoSum.js index 27013e2..0866bf3 100644 --- a/TwoSum.js +++ b/TwoSum.js @@ -20,3 +20,5 @@ const nums = [3, 2, 3]; const target = 6; const a = twoSum(nums, target); console.log(a); + +// https://leetcode.com/problems/two-sum/description/ diff --git a/topKFrequent.js b/topKFrequent.js index 2c138d2..532f7a1 100644 --- a/topKFrequent.js +++ b/topKFrequent.js @@ -13,13 +13,12 @@ const topKFrequent = (nums, k) => { } const sortedArray = Object.entries(obj).sort((a, b) => b[1] - a[1]); + const answer = sortedArray.slice(0, k).map(([num, _]) => Number(num)); + return answer; //[ [ '1', 3 ], [ '2', 2 ], [ '3', 3 ] ] // 0:숫자 1:몇개가 존재하는지 // [1] 번째 인덱스 () 순서대로 정렬. - - const answer = sortedArray.slice(0, k).map(([num, _]) => Number(num)); - return answer; }; const nums = [1, 1, 1, 2, 2, 3, 3, 3]; From d5e998d2d08c74f8e7d639b8a247c0324ed00071 Mon Sep 17 00:00:00 2001 From: tree Date: Sat, 24 Feb 2024 17:32:15 +0900 Subject: [PATCH 6/6] solve LongestConsecutive v2 --- LongestConsecutiveSequence.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/LongestConsecutiveSequence.js b/LongestConsecutiveSequence.js index 28d8986..258ba4a 100644 --- a/LongestConsecutiveSequence.js +++ b/LongestConsecutiveSequence.js @@ -4,20 +4,22 @@ const longestConsecutive = (nums) => { if (!nums.length) return 0; + const x = [...new Set(nums)].sort((a, b) => a - b); const answer = []; - let cnt = 0; + + let cnt = 1; x.forEach((v, i) => { if (v + 1 === x[i + 1]) { cnt += 1; } else { answer.push(cnt); - cnt = 0; + cnt = 1; } }); - return Math.max(...answer) + 1; + return Math.max(...answer); }; const nums = [1, 2, 0, 1];