Skip to content

feat: add solutions to lc problem: No.0594 #3173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2024
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
91 changes: 44 additions & 47 deletions solution/0500-0599/0594.Longest Harmonious Subsequence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:哈希表

我们可以用一个哈希表 $\text{cnt}$ 记录数组 $\text{nums}$ 中每个元素出现的次数,然后遍历哈希表中的每个键值对 $(x, c)$,如果哈希表中存在键 $x + 1$,那么 $\text{nums}$ 中元素 $x$ 和 $x + 1$ 出现的次数之和 $c + \text{cnt}[x + 1]$ 就是一个和谐子序列,我们只需要在所有和谐子序列中找到最大的长度即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\text{nums}$ 的长度。

<!-- tabs:start -->

Expand All @@ -74,27 +78,24 @@ tags:
```python
class Solution:
def findLHS(self, nums: List[int]) -> int:
ans = 0
counter = Counter(nums)
for num in nums:
if num + 1 in counter:
ans = max(ans, counter[num] + counter[num + 1])
return ans
cnt = Counter(nums)
return max((c + cnt[x + 1] for x, c in cnt.items() if cnt[x + 1]), default=0)
```

#### Java

```java
class Solution {
public int findLHS(int[] nums) {
Map<Integer, Integer> counter = new HashMap<>();
for (int num : nums) {
counter.put(num, counter.getOrDefault(num, 0) + 1);
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : nums) {
cnt.merge(x, 1, Integer::sum);
}
int ans = 0;
for (int num : nums) {
if (counter.containsKey(num + 1)) {
ans = Math.max(ans, counter.get(num) + counter.get(num + 1));
for (var e : cnt.entrySet()) {
int x = e.getKey(), c = e.getValue();
if (cnt.containsKey(x + 1)) {
ans = Math.max(ans, c + cnt.get(x + 1));
}
}
return ans;
Expand All @@ -108,14 +109,14 @@ class Solution {
class Solution {
public:
int findLHS(vector<int>& nums) {
unordered_map<int, int> counter;
for (int num : nums) {
++counter[num];
unordered_map<int, int> cnt;
for (int x : nums) {
++cnt[x];
}
int ans = 0;
for (int num : nums) {
if (counter.count(num + 1)) {
ans = max(ans, counter[num] + counter[num + 1]);
for (auto& [x, c] : cnt) {
if (cnt.contains(x + 1)) {
ans = max(ans, c + cnt[x + 1]);
}
}
return ans;
Expand All @@ -126,41 +127,37 @@ public:
#### Go

```go
func findLHS(nums []int) int {
counter := make(map[int]int)
for _, num := range nums {
counter[num]++
func findLHS(nums []int) (ans int) {
cnt := map[int]int{}
for _, x := range nums {
cnt[x]++
}
ans := 0
for _, num := range nums {
if counter[num+1] > 0 {
ans = max(ans, counter[num]+counter[num+1])
for x, c := range cnt {
if c1, ok := cnt[x+1]; ok {
ans = max(ans, c+c1)
}
}
return ans
return
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->
#### TypeScript

#### Python3

```python
class Solution:
def findLHS(self, nums: List[int]) -> int:
counter = Counter(nums)
return max(
[counter[num] + counter[num + 1] for num in nums if num + 1 in counter],
default=0,
)
```ts
function findLHS(nums: number[]): number {
const cnt: Record<number, number> = {};
for (const x of nums) {
cnt[x] = (cnt[x] || 0) + 1;
}
let ans = 0;
for (const [x, c] of Object.entries(cnt)) {
const y = +x + 1;
if (cnt[y]) {
ans = Math.max(ans, c + cnt[y]);
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
91 changes: 44 additions & 47 deletions solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table

We can use a hash table $\text{cnt}$ to record the occurrence count of each element in the array $\text{nums}$. Then, we iterate through each key-value pair $(x, c)$ in the hash table. If the key $x + 1$ exists in the hash table, then the sum of occurrences of elements $x$ and $x + 1$, $c + \text{cnt}[x + 1]$, forms a harmonious subsequence. We just need to find the maximum length among all harmonious subsequences.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\text{nums}$.

<!-- tabs:start -->

Expand All @@ -87,27 +91,24 @@ tags:
```python
class Solution:
def findLHS(self, nums: List[int]) -> int:
ans = 0
counter = Counter(nums)
for num in nums:
if num + 1 in counter:
ans = max(ans, counter[num] + counter[num + 1])
return ans
cnt = Counter(nums)
return max((c + cnt[x + 1] for x, c in cnt.items() if cnt[x + 1]), default=0)
```

#### Java

```java
class Solution {
public int findLHS(int[] nums) {
Map<Integer, Integer> counter = new HashMap<>();
for (int num : nums) {
counter.put(num, counter.getOrDefault(num, 0) + 1);
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : nums) {
cnt.merge(x, 1, Integer::sum);
}
int ans = 0;
for (int num : nums) {
if (counter.containsKey(num + 1)) {
ans = Math.max(ans, counter.get(num) + counter.get(num + 1));
for (var e : cnt.entrySet()) {
int x = e.getKey(), c = e.getValue();
if (cnt.containsKey(x + 1)) {
ans = Math.max(ans, c + cnt.get(x + 1));
}
}
return ans;
Expand All @@ -121,14 +122,14 @@ class Solution {
class Solution {
public:
int findLHS(vector<int>& nums) {
unordered_map<int, int> counter;
for (int num : nums) {
++counter[num];
unordered_map<int, int> cnt;
for (int x : nums) {
++cnt[x];
}
int ans = 0;
for (int num : nums) {
if (counter.count(num + 1)) {
ans = max(ans, counter[num] + counter[num + 1]);
for (auto& [x, c] : cnt) {
if (cnt.contains(x + 1)) {
ans = max(ans, c + cnt[x + 1]);
}
}
return ans;
Expand All @@ -139,41 +140,37 @@ public:
#### Go

```go
func findLHS(nums []int) int {
counter := make(map[int]int)
for _, num := range nums {
counter[num]++
func findLHS(nums []int) (ans int) {
cnt := map[int]int{}
for _, x := range nums {
cnt[x]++
}
ans := 0
for _, num := range nums {
if counter[num+1] > 0 {
ans = max(ans, counter[num]+counter[num+1])
for x, c := range cnt {
if c1, ok := cnt[x+1]; ok {
ans = max(ans, c+c1)
}
}
return ans
return
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->
#### TypeScript

#### Python3

```python
class Solution:
def findLHS(self, nums: List[int]) -> int:
counter = Counter(nums)
return max(
[counter[num] + counter[num + 1] for num in nums if num + 1 in counter],
default=0,
)
```ts
function findLHS(nums: number[]): number {
const cnt: Record<number, number> = {};
for (const x of nums) {
cnt[x] = (cnt[x] || 0) + 1;
}
let ans = 0;
for (const [x, c] of Object.entries(cnt)) {
const y = +x + 1;
if (cnt[y]) {
ans = Math.max(ans, c + cnt[y]);
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class Solution {
public:
int findLHS(vector<int>& nums) {
unordered_map<int, int> counter;
for (int num : nums) {
++counter[num];
unordered_map<int, int> cnt;
for (int x : nums) {
++cnt[x];
}
int ans = 0;
for (int num : nums) {
if (counter.count(num + 1)) {
ans = max(ans, counter[num] + counter[num + 1]);
for (auto& [x, c] : cnt) {
if (cnt.contains(x + 1)) {
ans = max(ans, c + cnt[x + 1]);
}
}
return ans;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
func findLHS(nums []int) int {
counter := make(map[int]int)
for _, num := range nums {
counter[num]++
func findLHS(nums []int) (ans int) {
cnt := map[int]int{}
for _, x := range nums {
cnt[x]++
}
ans := 0
for _, num := range nums {
if counter[num+1] > 0 {
ans = max(ans, counter[num]+counter[num+1])
for x, c := range cnt {
if c1, ok := cnt[x+1]; ok {
ans = max(ans, c+c1)
}
}
return ans
return
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
class Solution {
public int findLHS(int[] nums) {
Map<Integer, Integer> counter = new HashMap<>();
for (int num : nums) {
counter.put(num, counter.getOrDefault(num, 0) + 1);
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : nums) {
cnt.merge(x, 1, Integer::sum);
}
int ans = 0;
for (int num : nums) {
if (counter.containsKey(num + 1)) {
ans = Math.max(ans, counter.get(num) + counter.get(num + 1));
for (var e : cnt.entrySet()) {
int x = e.getKey(), c = e.getValue();
if (cnt.containsKey(x + 1)) {
ans = Math.max(ans, c + cnt.get(x + 1));
}
}
return ans;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
class Solution:
def findLHS(self, nums: List[int]) -> int:
ans = 0
counter = Counter(nums)
for num in nums:
if num + 1 in counter:
ans = max(ans, counter[num] + counter[num + 1])
return ans
cnt = Counter(nums)
return max((c + cnt[x + 1] for x, c in cnt.items() if cnt[x + 1]), default=0)
14 changes: 14 additions & 0 deletions solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function findLHS(nums: number[]): number {
const cnt: Record<number, number> = {};
for (const x of nums) {
cnt[x] = (cnt[x] || 0) + 1;
}
let ans = 0;
for (const [x, c] of Object.entries(cnt)) {
const y = +x + 1;
if (cnt[y]) {
ans = Math.max(ans, c + cnt[y]);
}
}
return ans;
}

This file was deleted.