File tree 3 files changed +42
-1
lines changed
3 files changed +42
-1
lines changed Original file line number Diff line number Diff line change 155
155
| 581 | [ Shortest Unsorted Continuous Subarray] ( https://leetcode.com/problems/shortest-unsorted-continuous-subarray ) | Medium | [ ![ Java] ( assets/java.png )] ( src/ShortestUnsortedContinuousSubarray.java ) [ ![ Python] ( assets/python.png )] ( python/shortest_continuous_unsorted_subarray.py ) |
156
156
| 589 | [ N-Ary Tree Preorder Traversal] ( https://leetcode.com/problems/n-ary-tree-preorder-traversal ) | Easy | [ ![ Java] ( assets/java.png )] ( src/NArayTreePreOrderTraversal.java ) [ ![ Python] ( assets/python.png )] ( python/n_ary_tree_preorder_traversal.py ) |
157
157
| 590 | [ N-Ary Tree Postorder Traversal] ( https://leetcode.com/problems/n-ary-tree-postorder-traversal ) | Easy | [ ![ Java] ( assets/java.png )] ( src/NAryTreePostorderTraversal.java ) [ ![ Python] ( assets/python.png )] ( python/n_ary_tree_postorder_traversal.py ) |
158
- | 594 | [ Longest Harmonious Subsequence] ( https://leetcode.com/problems/longest-harmonious-subsequence ) | Easy | |
158
+ | 594 | [ Longest Harmonious Subsequence] ( https://leetcode.com/problems/longest-harmonious-subsequence ) | Easy | [ ![ Java ] ( assets/java.png )] ( src/LongestHarmoniousSubsequence.java ) [ ![ Python ] ( assets/python.png )] ( python/longest_harmonious_subequence.py ) |
159
159
| 598 | [ Range Addition II] ( https://leetcode.com/problems/range-addition-ii ) | Easy | |
160
160
| 599 | [ Minimum Index Sum of 2 Lists] ( https://leetcode.com/problems/minimum-index-sum-of-two-lists ) | Easy | |
161
161
| 604 | [ Design Compressed String Iterator] ( https://leetcode.com/problems/design-compressed-string-iterator ) | Easy | |
Original file line number Diff line number Diff line change
1
+ from collections import Counter
2
+ from typing import List
3
+
4
+
5
+ class Solution :
6
+ def findLHS (self , array : List [int ]) -> int :
7
+ frequencies = Counter (array )
8
+ result = 0
9
+ for value , frequency in frequencies .items ():
10
+ result = max (
11
+ result ,
12
+ frequencies .get (value - 1 ) + frequency if value - 1 in frequencies else 0 ,
13
+ frequencies .get (value + 1 ) + frequency if value + 1 in frequencies else 0
14
+ )
15
+ return result
Original file line number Diff line number Diff line change
1
+ import java .util .HashMap ;
2
+ import java .util .Map ;
3
+
4
+ public class LongestHarmoniousSubsequence {
5
+ public int findLHS (int [] array ) {
6
+ Map <Integer , Integer > frequencies = getFrequencies (array );
7
+ int value , frequency , result = 0 ;
8
+ for (Map .Entry <Integer , Integer > entry : frequencies .entrySet ()) {
9
+ value = entry .getKey ();
10
+ frequency = entry .getValue ();
11
+ result = Math .max (result , Math .max (
12
+ frequencies .containsKey (value - 1 ) ? frequency + frequencies .get (value - 1 ) : 0 ,
13
+ frequencies .containsKey (value + 1 ) ? frequency + frequencies .get (value + 1 ) : 0
14
+ ));
15
+ }
16
+ return result ;
17
+ }
18
+
19
+ private Map <Integer , Integer > getFrequencies (int [] array ) {
20
+ Map <Integer , Integer > frequencies = new HashMap <>();
21
+ for (int element : array ) {
22
+ frequencies .put (element , frequencies .getOrDefault (element , 0 ) + 1 );
23
+ }
24
+ return frequencies ;
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments