Skip to content

Commit 156d315

Browse files
authored
Merge pull request #846 from 0xff-dev/594
Add solution and test-cases for problem 594
2 parents ff998b5 + 8987924 commit 156d315

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

leetcode/501-600/0594.Longest-Harmonious-Subsequence/README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [594.Longest Harmonious Subsequence][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
We define a harmonious array as an array where the difference between its maximum value and its minimum value is **exactly** `1`.
5+
6+
Given an integer array `nums`, return the length of its longest harmonious subsequence among all its possible subsequences.
7+
8+
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.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [1,3,2,2,5,2,3,7]
14+
Output: 5
15+
Explanation: The longest harmonious subsequence is [3,2,2,2,3].
1316
```
1417

15-
## 题意
16-
> ...
17-
18-
## 题解
18+
**Example 2:**
1919

20-
### 思路1
21-
> ...
22-
Longest Harmonious Subsequence
23-
```go
2420
```
21+
Input: nums = [1,2,3,4]
22+
Output: 2
23+
```
24+
25+
**Example 3:**
2526

27+
```
28+
Input: nums = [1,1,1,1]
29+
Output: 0
30+
```
2631

2732
## 结语
2833

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int) int {
6+
keys := make(map[int]int)
7+
a := make([]int, 0)
8+
for _, n := range nums {
9+
if _, ok := keys[n]; !ok {
10+
a = append(a, n)
11+
}
12+
keys[n]++
13+
}
14+
sort.Ints(a)
15+
ans := 0
16+
for i := 1; i < len(a); i++ {
17+
if a[i]-a[i-1] == 1 && keys[a[i]]+keys[a[i-1]] > ans {
18+
ans = keys[a[i]] + keys[a[i-1]]
19+
}
20+
}
21+
return ans
522
}

leetcode/501-600/0594.Longest-Harmonious-Subsequence/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 3, 2, 2, 5, 2, 3, 7}, 5},
17+
{"TestCase2", []int{1, 2, 3, 4}, 2},
18+
{"TestCase3", []int{1, 1, 1, 1}, 0},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)