bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 659. Split Array into Consecutive Subsequences

659. Split Array into Consecutive Subsequences

Leetcode Solutions

Greedy Approach Using Maps

Algorithm

  1. Initialize two maps: frequency to store the frequency of each number in nums, and subsequences to store the frequency of subsequences ending with a particular number.
  2. Iterate over the nums array to update the frequency map with the count of each number.
  3. Iterate over the nums array again and for each number num:
    • If frequency[num] is 0, continue to the next number.
    • If there is a subsequence ending with num - 1, decrement its count in subsequences and increment the count for num.
    • If no such subsequence exists, check if num + 1 and num + 2 are present in frequency. If not, return false. Otherwise, decrement their counts in frequency and increment the count for num + 2 in subsequences.
  4. If the loop completes without returning false, return true.
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...