LeetCode - Blind 75 - Longest Consecutive Sequence

The Problem 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. A consecutive sequence is a sequence of elements in which each element is exactly 1 greater than the previous element. Examples Input: nums = [100, 4, 200, 1, 3, 2] Output: 4 Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. Input: nums = [0, 3, 7, 2, 5, 8, 4, 6, 0, 1] Output: 9 Constraints 0 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9 Brute Force Solution func longestConsecutive(_ nums: [Int]) -> Int { var res = 0 let setOfNums: Set<Int> = Set(nums) for num in nums { var streak = 0 var curr = num while setOfNums.contains(curr) { streak += 1 curr += 1 } res = max(res, streak) } return res } Explanation Let’s start with the brute force solution. We need to increment each element by 1 to check if the incremented value exists in our input array. For example, if we have an array nums with [3, 2, 1, 100, 5], the longest consecutive sequence will be 3. If you look closely at the array [3, 2, 1, 100, 5], it has a sequence of 2, 1, which when incremented forms elements that already exist in the array (1 -> 2, 2 -> 3). By counting the number of increments, you can determine the longest sequence. The solution above does exactly that: ...

November 10, 2024 · 4 min · Dmytro Chumakov

LeetCode - Blind 75 - Product of Array Except Self

The problem Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. You must write an algorithm that runs in O(n) time and without using the division operation. Examples Input: nums = [1,2,3,4] Output: [24,12,8,6] Input: nums = [-1,1,0,-3,3] Output: [0,0,9,0,0] Constraints 2 <= nums.length <= 10^5 -30 <= nums[i] <= 30 The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.) ...

November 5, 2024 · 4 min · Dmytro Chumakov

LeetCode - Blind 75 - Top K Frequent Elements

The problem Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. Examples Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Input: nums = [1], k = 1 Output: [1] Constraints 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 k is in the range [1, the number of unique elements in the array]. It is guaranteed that the answer is unique. Follow-up: Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size. ...

November 3, 2024 · 3 min · Dmytro Chumakov

LeetCode - Blind 75 - Group Anagrams

The problem Given an array of strings strs, group the anagrams together. You can return the answer in any order. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once. Examples Input: strs = ["eat","tea","tan","ate","nat","bat"] Output: [["bat"],["nat","tan"],["ate","eat","tea"]] Explanation: * There is no string in strs that can be rearranged to form "bat." * The strings "nat" and "tan" are anagrams as they can be rearranged to form each other. * The strings "ate," "eat," and "tea" are anagrams as they can be rearranged to form each other. Input: strs = [""] Output: [[""]] Input: strs = ["a"] Output: [["a"]] Constraints 1 <= strs.length <= 1000 0 <= strs[i].length <= 100 strs[i] is made up of lowercase English letters. Brute force solution func groupAnagrams(_ strs: [String]) -> [[String]] { var res: [String: [String]] = [:] for s in strs { let sortedS = String(s.sorted()) res[sortedS, default: []].append(s) } return Array(res.values) } Explanation The brute force solution: ...

November 1, 2024 · 2 min · Dmytro Chumakov

LeetCode - Blind 75 - Valid Anagram

The Problem Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once. Examples Input: s = "anagram", t = "nagaram" Output: true Input: s = "rat", t = "car" Output: false Follow-up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case? ...

October 30, 2024 · 3 min · Dmytro Chumakov