From eae9bb06c5501849a34c1ca9259d6aaefe2f6943 Mon Sep 17 00:00:00 2001 From: Taewan Lim Date: Wed, 11 Dec 2024 23:47:28 +0900 Subject: [PATCH 1/5] contains-duplicate --- contains-duplicate/taewanseoul.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 contains-duplicate/taewanseoul.ts diff --git a/contains-duplicate/taewanseoul.ts b/contains-duplicate/taewanseoul.ts new file mode 100644 index 000000000..97233136c --- /dev/null +++ b/contains-duplicate/taewanseoul.ts @@ -0,0 +1,14 @@ +/** + * 217. Contains Duplicate + * Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. + * https://leetcode.com/problems/contains-duplicate/description/ + */ +function containsDuplicate(nums: number[]): boolean { + const isDuped = new Set(nums).size !== nums.length; + + return isDuped ? true : false; +} + +// TC: O(n) +// https://262.ecma-international.org/15.0/index.html#sec-get-set.prototype.size +// Set objects must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection. The data structure used in this specification is only intended to describe the required observable semantics of Set objects. It is not intended to be a viable implementation model. From 9a3c3f3ffd635bae7cae181d7cca973fecb6539a Mon Sep 17 00:00:00 2001 From: Taewan Lim Date: Fri, 13 Dec 2024 00:11:03 +0900 Subject: [PATCH 2/5] valid-palindrome --- valid-palindrome/taewanseoul.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 valid-palindrome/taewanseoul.ts diff --git a/valid-palindrome/taewanseoul.ts b/valid-palindrome/taewanseoul.ts new file mode 100644 index 000000000..88003d33b --- /dev/null +++ b/valid-palindrome/taewanseoul.ts @@ -0,0 +1,16 @@ +/** + * 125. Valid Palindrome + * A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all + * non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters + * and numbers. + * Given a string s, return true if it is a palindrome, or false otherwise. + * https://leetcode.com/problems/valid-palindrome/description/ + */ +function isPalindrome(s: string): boolean { + const chars = s.replace(/[^a-z0-9]/gi, "").toLowerCase(); + + return chars === [...chars].reverse().join(""); +} + +// O(n) time +// O(n) space From b31642f7536b60a908d90bdd1bfc3459165ada43 Mon Sep 17 00:00:00 2001 From: Taewan Lim Date: Fri, 13 Dec 2024 00:38:18 +0900 Subject: [PATCH 3/5] refactor: contains-duplicate --- contains-duplicate/taewanseoul.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/contains-duplicate/taewanseoul.ts b/contains-duplicate/taewanseoul.ts index 97233136c..85a3992a0 100644 --- a/contains-duplicate/taewanseoul.ts +++ b/contains-duplicate/taewanseoul.ts @@ -4,9 +4,16 @@ * https://leetcode.com/problems/contains-duplicate/description/ */ function containsDuplicate(nums: number[]): boolean { - const isDuped = new Set(nums).size !== nums.length; + const set = new Set(); - return isDuped ? true : false; + for (let i = 0; i < nums.length; i++) { + if (set.has(nums[i])) { + return true; + } + set.add(nums[i]); + } + + return false; } // TC: O(n) From f3326a8132c709b9bb91ac9db39f933db1a17949 Mon Sep 17 00:00:00 2001 From: Taewan Lim Date: Fri, 13 Dec 2024 00:43:52 +0900 Subject: [PATCH 4/5] top-k-frequent-elements --- top-k-frequent-elements/taewanseoul.ts | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 top-k-frequent-elements/taewanseoul.ts diff --git a/top-k-frequent-elements/taewanseoul.ts b/top-k-frequent-elements/taewanseoul.ts new file mode 100644 index 000000000..71b317398 --- /dev/null +++ b/top-k-frequent-elements/taewanseoul.ts @@ -0,0 +1,31 @@ +/** + * 347. Top K Frequent Elements + * Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in + * any order. + * https://leetcode.com/problems/top-k-frequent-elements/description/ + */ +function topKFrequent(nums: number[], k: number): number[] { + const map = new Map(); + + nums.forEach((n) => { + const count = map.get(n); + if (count) { + map.set(n, count + 1); + return; + } + map.set(n, 1); + }); + + const kvArray = [...map]; + const sorted = kvArray.sort(([, v1], [, v2]) => v2 - v1); + + const result: number[] = []; + for (let i = 0; i < k; i++) { + result.push(sorted[i][0]); + } + + return result; +} + +// O(n) time +// O(n) space From 054e552d129c239893b917e28e1909bacd154b75 Mon Sep 17 00:00:00 2001 From: Taewan Lim Date: Fri, 13 Dec 2024 01:52:54 +0900 Subject: [PATCH 5/5] longest-consecutive-sequence --- longest-consecutive-sequence/taewanseoul.ts | 33 +++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 longest-consecutive-sequence/taewanseoul.ts diff --git a/longest-consecutive-sequence/taewanseoul.ts b/longest-consecutive-sequence/taewanseoul.ts new file mode 100644 index 000000000..4d6c9cd1e --- /dev/null +++ b/longest-consecutive-sequence/taewanseoul.ts @@ -0,0 +1,33 @@ +/** + * 128. Longest Consecutive Sequence + * 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. + * https://leetcode.com/problems/longest-consecutive-sequence/description/ + */ +function longestConsecutive(nums: number[]): number { + const set = new Set(nums); + const sorted = [...set].sort((a, b) => a - b); + + if (sorted.length === 0) { + return 0; + } + + let longestSequence = 1; + let currentSequence = 1; + for (let i = 0; i - 1 < sorted.length; i++) { + if (Math.abs(sorted[i + 1] - sorted[i]) === 1) { + currentSequence++; + } else { + if (currentSequence > longestSequence) { + longestSequence = currentSequence; + } + currentSequence = 1; + } + } + + return longestSequence; +} + +// O(n) time +// O(n) space