diff --git a/contains-duplicate/taewanseoul.ts b/contains-duplicate/taewanseoul.ts new file mode 100644 index 000000000..85a3992a0 --- /dev/null +++ b/contains-duplicate/taewanseoul.ts @@ -0,0 +1,21 @@ +/** + * 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 set = new Set(); + + for (let i = 0; i < nums.length; i++) { + if (set.has(nums[i])) { + return true; + } + set.add(nums[i]); + } + + return 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. 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 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 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