Skip to content

Commit 62d21a5

Browse files
committed
Longest harmonious subsequence.
1 parent 04ef754 commit 62d21a5

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/594.Longest Harmonious Subsequence.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
109. [Array Partition](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/561.Array%20Partition.playground/Contents.swift)
114114
110. [Reshape the Matrix](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/566.Reshape%20the%20Matrix.playground/Contents.swift)
115115
111. [Distribute Candies](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/575.Distribute%20Candies.playground/Contents.swift)
116+
112. [Longest Harmonious Subsequence](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/594.Longest%20Harmonious%20Subsequence.playground/Contents.swift)
116117

117118
#### Medium
118119

0 commit comments

Comments
 (0)