public class Solution {
public int FindLHS(int[] nums) {
Dictionary<int, int> dic = new Dictionary<int, int>();
foreach (var num in nums)
{
if (!dic.ContainsKey(num))
{
dic.Add(num, );
}
else
{
dic[num]++;
}
} var list = dic.OrderBy(x => x.Key).ToList();
if (list.Count == )
{
return ;
}
else
{
var lastkey = ;
var lastLen = ;
var max = ;
for (int i = ; i < list.Count; i++)
{
if (i == )
{
lastkey = list[i].Key;
lastLen = list[i].Value;
continue;
}
else
{
var curKey = list[i].Key;
var curLen = list[i].Value;
if (curKey - lastkey == )
{
var totalLen = lastLen + curLen;
if (max < totalLen)
{
max = totalLen;
}
}
lastkey = curKey;
lastLen = curLen;
}
}
return max;
}
}
}

https://leetcode.com/problems/longest-harmonious-subsequence/#/description

05-11 22:20