frequency
to store the frequency of each number in nums
, and subsequences
to store the frequency of subsequences ending with a particular number.nums
array to update the frequency
map with the count of each number.nums
array again and for each number num
:
frequency[num]
is 0
, continue to the next number.num - 1
, decrement its count in subsequences
and increment the count for num
.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
.false
, return true
.