From c78908bc841e79e75f891f6b1461f6508e15c241 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 28 Jun 2024 23:10:10 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0594 No.0594.Longest Harmonious Subsequence --- .../README.md | 91 +++++++++---------- .../README_EN.md | 91 +++++++++---------- .../Solution.cpp | 12 +-- .../Solution.go | 17 ++-- .../Solution.java | 13 +-- .../Solution.py | 8 +- .../Solution.ts | 14 +++ .../Solution2.py | 7 -- 8 files changed, 125 insertions(+), 128 deletions(-) create mode 100644 solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.ts delete mode 100644 solution/0500-0599/0594.Longest Harmonious Subsequence/Solution2.py diff --git a/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md b/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md index 5c238cfba4ce9..f6cc7e8ee7e05 100644 --- a/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md +++ b/solution/0500-0599/0594.Longest Harmonious Subsequence/README.md @@ -65,7 +65,11 @@ tags: -### 方法一 +### 方法一:哈希表 + +我们可以用一个哈希表 $\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}$ 的长度。 @@ -74,12 +78,8 @@ 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 @@ -87,14 +87,15 @@ class Solution: ```java class Solution { public int findLHS(int[] nums) { - Map counter = new HashMap<>(); - for (int num : nums) { - counter.put(num, counter.getOrDefault(num, 0) + 1); + Map 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; @@ -108,14 +109,14 @@ class Solution { class Solution { public: int findLHS(vector& nums) { - unordered_map counter; - for (int num : nums) { - ++counter[num]; + unordered_map 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; @@ -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 } ``` - - - - - - -### 方法二 - - +#### 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 = {}; + 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; +} ``` diff --git a/solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md b/solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md index 25b7790c83187..d57222ade3e83 100644 --- a/solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md +++ b/solution/0500-0599/0594.Longest Harmonious Subsequence/README_EN.md @@ -78,7 +78,11 @@ tags: -### 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}$. @@ -87,12 +91,8 @@ 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 @@ -100,14 +100,15 @@ class Solution: ```java class Solution { public int findLHS(int[] nums) { - Map counter = new HashMap<>(); - for (int num : nums) { - counter.put(num, counter.getOrDefault(num, 0) + 1); + Map 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; @@ -121,14 +122,14 @@ class Solution { class Solution { public: int findLHS(vector& nums) { - unordered_map counter; - for (int num : nums) { - ++counter[num]; + unordered_map 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; @@ -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 } ``` - - - - - - -### Solution 2 - - +#### 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 = {}; + 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; +} ``` diff --git a/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.cpp b/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.cpp index c3abf6955641f..a8f44238fb7d7 100644 --- a/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.cpp +++ b/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.cpp @@ -1,14 +1,14 @@ class Solution { public: int findLHS(vector& nums) { - unordered_map counter; - for (int num : nums) { - ++counter[num]; + unordered_map 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; diff --git a/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.go b/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.go index 2abfa120ebf26..1e847e34c2c22 100644 --- a/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.go +++ b/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.go @@ -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 } \ No newline at end of file diff --git a/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.java b/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.java index 0e7e538a1179f..1b8bc3237beaa 100644 --- a/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.java +++ b/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.java @@ -1,13 +1,14 @@ class Solution { public int findLHS(int[] nums) { - Map counter = new HashMap<>(); - for (int num : nums) { - counter.put(num, counter.getOrDefault(num, 0) + 1); + Map 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; diff --git a/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.py b/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.py index 8cc8a4b7538ef..56d5414f170cd 100644 --- a/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.py +++ b/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.py @@ -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) diff --git a/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.ts b/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.ts new file mode 100644 index 0000000000000..f74b0a8a7a68d --- /dev/null +++ b/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution.ts @@ -0,0 +1,14 @@ +function findLHS(nums: number[]): number { + const cnt: Record = {}; + 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; +} diff --git a/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution2.py b/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution2.py deleted file mode 100644 index 1c4cf735b54cf..0000000000000 --- a/solution/0500-0599/0594.Longest Harmonious Subsequence/Solution2.py +++ /dev/null @@ -1,7 +0,0 @@ -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, - )