Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4a139a7

Browse files
add 594
1 parent f31070c commit 4a139a7

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Your ideas/fixes/algorithms are more than welcome!
2020

2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
23+
|594|[Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence/)|[Solution](../master/src/main/java/com/stevesun/solutions/_594.java) | O(n) |O(n) | Easy | Array, HashMap
2324
|583|[Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/)|[Solution](../master/src/main/java/com/stevesun/solutions/_583.java) | O(m*n) |O(m*n) could be optimized to O(n) | Medium | DP
2425
|582|[Kill Process](https://leetcode.com/problems/kill-process/)|[Solution](../master/src/main/java/com/stevesun/solutions/_582.java) | O(n) |O(h) | Medium | Stack
2526
|581|[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/)|[Solution](../master/src/main/java/com/stevesun/solutions/_581.java) | O(n) |O(1) | Easy | Array, Sort
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.stevesun.solutions;
2+
3+
import java.util.*;
4+
5+
/**
6+
* 594. Longest Harmonious Subsequence
7+
*
8+
* We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1.
9+
10+
Now, given an integer array, you need to find the length of its longest harmonious subsequence among all its possible subsequences.
11+
12+
Example 1:
13+
Input: [1,3,2,2,5,2,3,7]
14+
Output: 5
15+
16+
Explanation: The longest harmonious subsequence is [3,2,2,2,3].
17+
Note: The length of the input array will not exceed 20,000.
18+
19+
*/
20+
public class _594 {
21+
public int findLHS(int[] nums) {
22+
Map<Integer, Integer> map = new HashMap<>();
23+
for (int i : nums) {
24+
map.put(i, map.getOrDefault(i, 0) + 1);
25+
}
26+
int max = 0;
27+
for (int i = 0; i < nums.length; i++) {
28+
if (map.containsKey(nums[i]+1)) {
29+
max = Math.max(max, map.get(nums[i]) + map.get(nums[i] + 1));
30+
}
31+
}
32+
return max;
33+
}
34+
35+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions._48;
4+
import com.stevesun.solutions._594;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
/**
11+
* Created by stevesun on 5/20/17.
12+
*/
13+
public class _594Test {
14+
private static _594 test;
15+
private static int[] nums;
16+
17+
@BeforeClass
18+
public static void setup(){
19+
test = new _594();
20+
}
21+
22+
@Test
23+
public void test1(){
24+
nums = new int[]{1,3,2,2,5,2,3,7};
25+
assertEquals(5, test.findLHS(nums));
26+
}
27+
}

0 commit comments

Comments
 (0)