|
| 1 | +/** |
| 2 | + We define a harmonious array as an array where the difference between its maximum value and its minimum value is exactly 1. |
| 3 | + |
| 4 | + Given an integer array nums, return the length of its longest harmonious subsequence among all its possible subsequences. |
| 5 | + |
| 6 | + A subsequence of array is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements. |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + Example 1: |
| 11 | + Input: nums = [1,3,2,2,5,2,3,7] |
| 12 | + Output: 5 |
| 13 | + Explanation: The longest harmonious subsequence is [3,2,2,2,3]. |
| 14 | + |
| 15 | + Example 2: |
| 16 | + Input: nums = [1,2,3,4] |
| 17 | + Output: 2 |
| 18 | + |
| 19 | + Example 3: |
| 20 | + Input: nums = [1,1,1,1] |
| 21 | + Output: 0 |
| 22 | + |
| 23 | + |
| 24 | + Constraints: |
| 25 | + - 1 <= nums.length <= 2 * 10^4 |
| 26 | + - -10^9 <= nums[i] <= 10^9 |
| 27 | + */ |
| 28 | +class Solution { |
| 29 | + func findLHS(_ nums: [Int]) -> Int { |
| 30 | + if nums.count == 1 { return 0 } |
| 31 | + let sortedNums = nums.sorted(by: <) |
| 32 | + print(sortedNums) |
| 33 | + var result = [Int]() |
| 34 | + var maxCount = 0 |
| 35 | + var flag = false |
| 36 | + var i = 0 |
| 37 | + while i < sortedNums.count { |
| 38 | + if i < sortedNums.count - 1 { |
| 39 | + if sortedNums[i + 1] - sortedNums[i] == 0 { |
| 40 | + result.append(sortedNums[i]) |
| 41 | + } else if sortedNums[i + 1] - sortedNums[i] == 1 { |
| 42 | + result.append(sortedNums[i]) |
| 43 | + flag = true |
| 44 | + if result.count > maxCount { |
| 45 | + maxCount = result.count |
| 46 | + } |
| 47 | + if !result.isEmpty { |
| 48 | + var newResult = [Int]() |
| 49 | + for e in result { |
| 50 | + if e >= sortedNums[i] { |
| 51 | + newResult.append(e) |
| 52 | + } |
| 53 | + } |
| 54 | + result = newResult |
| 55 | + } |
| 56 | + } else { |
| 57 | + if i > 0 { |
| 58 | + if sortedNums[i] - sortedNums[i - 1] <= 1 { |
| 59 | + result.append(sortedNums[i]) |
| 60 | + } |
| 61 | + } |
| 62 | + if flag == true { |
| 63 | + if result.count > maxCount { |
| 64 | + maxCount = result.count |
| 65 | + } |
| 66 | + } |
| 67 | + result.removeAll() |
| 68 | + result.append(sortedNums[i]) |
| 69 | + flag = false |
| 70 | + } |
| 71 | + } else { |
| 72 | + if sortedNums[i] - sortedNums[i - 1] == 0 { |
| 73 | + result.append(sortedNums[i]) |
| 74 | + } else if sortedNums[i] - sortedNums[i - 1] == 1 { |
| 75 | + result.append(sortedNums[i]) |
| 76 | + flag = true |
| 77 | + if result.count > maxCount { |
| 78 | + maxCount = result.count |
| 79 | + } |
| 80 | + if !result.isEmpty { |
| 81 | + var newResult = [Int]() |
| 82 | + for e in result { |
| 83 | + if e >= sortedNums[i - 1] { |
| 84 | + newResult.append(e) |
| 85 | + } |
| 86 | + } |
| 87 | + result = newResult |
| 88 | + } |
| 89 | + |
| 90 | + } else { |
| 91 | + if sortedNums[i] - sortedNums[i - 1] <= 1 { |
| 92 | + result.append(sortedNums[i]) |
| 93 | + } |
| 94 | + if flag == true { |
| 95 | + if result.count > maxCount { |
| 96 | + maxCount = result.count |
| 97 | + } |
| 98 | + } |
| 99 | + result.removeAll() |
| 100 | + result.append(sortedNums[i]) |
| 101 | + flag = false |
| 102 | + } |
| 103 | + } |
| 104 | + i += 1 |
| 105 | + } |
| 106 | + if result.last != result.first && result.count > maxCount { |
| 107 | + maxCount = result.count |
| 108 | + } |
| 109 | + return maxCount |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +let s = Solution() |
| 114 | +let r = s.findLHS([1,1,1,1]) |
| 115 | +print(r) |
0 commit comments