Advertisement
jaibatrik

harmonicSequence

Jan 13th, 2021
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /**
  3.  * We define a harmonious array as an array where the difference between its
  4.  * maximum value and its minimum value is exactly `1`.
  5.  *
  6.  * Given an integer array `nums`, return the length of its longest harmonious
  7.  * subsequence among all its possible subsequences.
  8.  *
  9.  * A subsequence of array is a sequence that can be derived from the array by
  10.  * deleting some or no elements without changing the order of the remaining
  11.  * elements.
  12.  *
  13.  * Example 1:
  14.  * Input: nums = [1,3,2,2,5,2,3,7]
  15.  * Output: 5
  16.  * Explanation: The longest harmonious subsequence is [3,2,2,2,3].
  17.  *
  18.  * Example 2:
  19.  * Input: [1,2,3,4]
  20.  * Output: 2
  21.  *
  22.  * Example 3:
  23.  * Input: [1,1,1,1]
  24.  * Output: 0
  25.  *
  26.  * Example: 4
  27.  * Input: [1,3,5,7,9,11,13,15,17]
  28.  * Output: 0
  29.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement