Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,21 @@ fun longestPalindrome(words: Array<String>): Int {

return len
}

/**
* 594. Longest Harmonious Subsequence
*/

fun findLHS(nums: IntArray): Int {
val freq = nums.toList().groupingBy { it }.eachCount()
var longest = 0

for ((key, value) in freq) {
val more = key + 1
if (freq.contains(more)) {
longest = maxOf(longest, freq.getOrDefault(more, 0) + value)
}
}

return longest
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ fun countVowelSubstringsProdVariantIII(word: String) = sequence {
}
}.sum()

/**
* 594. Longest Harmonious Subsequence
* Prod Variant
*/

fun findLHSProdVariant(nums: IntArray): Int =
nums.toList().groupingBy { it }.eachCount().let { map ->
map.maxOf { (num, value) ->
val more = num + 1
if (map.contains(more)) {
map.getOrDefault(num, 0) + value
} else 0
}
}