-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution and test-cases for problem 594
- Loading branch information
Showing
3 changed files
with
44 additions
and
22 deletions.
There are no files selected for viewing
31 changes: 18 additions & 13 deletions
31
leetcode/501-600/0594.Longest-Harmonious-Subsequence/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 19 additions & 2 deletions
21
leetcode/501-600/0594.Longest-Harmonious-Subsequence/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
import "sort" | ||
|
||
func Solution(nums []int) int { | ||
keys := make(map[int]int) | ||
a := make([]int, 0) | ||
for _, n := range nums { | ||
if _, ok := keys[n]; !ok { | ||
a = append(a, n) | ||
} | ||
keys[n]++ | ||
} | ||
sort.Ints(a) | ||
ans := 0 | ||
for i := 1; i < len(a); i++ { | ||
if a[i]-a[i-1] == 1 && keys[a[i]]+keys[a[i-1]] > ans { | ||
ans = keys[a[i]] + keys[a[i-1]] | ||
} | ||
} | ||
return ans | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters