From d8856d885c1ef14fe7ab18e0b7cb09ccebc8c8a0 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 8 Aug 2024 09:13:48 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3247 No.3247.Number of Subsequences with Odd Sum --- .../README.md | 2 - .../README_EN.md | 2 - .../README.md | 202 ++++++++++++++++++ .../README_EN.md | 202 ++++++++++++++++++ .../Solution.cpp | 19 ++ .../Solution.go | 16 ++ .../Solution.java | 18 ++ .../Solution.py | 10 + .../Solution.ts | 16 ++ solution/README.md | 1 + solution/README_EN.md | 1 + solution/util.py | 10 +- 12 files changed, 491 insertions(+), 8 deletions(-) create mode 100644 solution/3200-3299/3247.Number of Subsequences with Odd Sum/README.md create mode 100644 solution/3200-3299/3247.Number of Subsequences with Odd Sum/README_EN.md create mode 100644 solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.cpp create mode 100644 solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.go create mode 100644 solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.java create mode 100644 solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.py create mode 100644 solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.ts diff --git a/solution/2700-2799/2753.Count Houses in a Circular Street II/README.md b/solution/2700-2799/2753.Count Houses in a Circular Street II/README.md index 6c88677ced4d4..e0835102ccfdb 100644 --- a/solution/2700-2799/2753.Count Houses in a Circular Street II/README.md +++ b/solution/2700-2799/2753.Count Houses in a Circular Street II/README.md @@ -2,8 +2,6 @@ comments: true difficulty: 困难 edit_url: https://github.com/doocs/leetcode/edit/main/solution/2700-2799/2753.Count%20Houses%20in%20a%20Circular%20Street%20II/README.md -tags: - - Algorithms --- diff --git a/solution/2700-2799/2753.Count Houses in a Circular Street II/README_EN.md b/solution/2700-2799/2753.Count Houses in a Circular Street II/README_EN.md index 6ce8ca023d22a..8b6747744dff2 100644 --- a/solution/2700-2799/2753.Count Houses in a Circular Street II/README_EN.md +++ b/solution/2700-2799/2753.Count Houses in a Circular Street II/README_EN.md @@ -2,8 +2,6 @@ comments: true difficulty: Hard edit_url: https://github.com/doocs/leetcode/edit/main/solution/2700-2799/2753.Count%20Houses%20in%20a%20Circular%20Street%20II/README_EN.md -tags: - - Algorithms --- diff --git a/solution/3200-3299/3247.Number of Subsequences with Odd Sum/README.md b/solution/3200-3299/3247.Number of Subsequences with Odd Sum/README.md new file mode 100644 index 0000000000000..b094d2203a0dc --- /dev/null +++ b/solution/3200-3299/3247.Number of Subsequences with Odd Sum/README.md @@ -0,0 +1,202 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3247.Number%20of%20Subsequences%20with%20Odd%20Sum/README.md +--- + + + +# [3247. Number of Subsequences with Odd Sum 🔒](https://leetcode.cn/problems/number-of-subsequences-with-odd-sum) + +[English Version](/solution/3200-3299/3247.Number%20of%20Subsequences%20with%20Odd%20Sum/README_EN.md) + +## 题目描述 + + + +

Given an array nums, return the number of subsequences with an odd sum of elements.

+ +

Since the answer may be very large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
+

Input: nums = [1,1,1]

+ +

Output: 4

+ +

Explanation:

+ +

The odd-sum subsequences are: [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1].

+
+ +

Example 2:

+ +
+

Input: nums = [1,2,2]

+ +

Output: 4

+ +

Explanation:

+ +

The odd-sum subsequences are: [1, 2, 2], [1, 2, 2], [1, 2, 2], [1, 2, 2].

+
+ +

 

+

Constraints:

+ + + + + +## 解法 + + + +### 方法一:动态规划 + +我们定义 $f[0]$ 表示目前为止的子序列中,和为偶数的子序列个数,而 $f[1]$ 表示目前为止的子序列中,和为奇数的子序列个数。初始时 $f[0] = 0$, $f[1] = 0$。 + +遍历数组 $\textit{nums}$,对于每个数 $x$: + +如果 $x$ 为奇数,那么 $f[0]$ 和 $f[1]$ 的更新方式为: + +$$ +\begin{aligned} +f[0] & = (f[0] + f[1]) \bmod 10^9 + 7, \\ +f[1] & = (f[0] + f[1] + 1) \bmod 10^9 + 7. +\end{aligned} +$$ + +即,当前的和为偶数的子序列个数等于之前的和为偶数的子序列个数,加上之前的和为奇数的子序列拼上当前数 $x$ 的子序列个数;当前的和为奇数的子序列个数等于之前的和为偶数的子序列拼上当前数 $x$ 的子序列个数,加上之前的和为奇数的子序列个数,再加上一个只包含当前数 $x$ 的子序列。 + +如果 $x$ 为偶数,那么 $f[0]$ 和 $f[1]$ 的更新方式为: + +$$ +\begin{aligned} +f[0] & = (f[0] + f[0] + 1) \bmod 10^9 + 7, \\ +f[1] & = (f[1] + f[1]) \bmod 10^9 + 7. +\end{aligned} +$$ + +即,当前的和为偶数的子序列个数等于之前的和为偶数的子序列个数,加上之前的和为偶数的子序列拼上当前数 $x$ 的子序列个数,再加上一个只包含当前数 $x$ 的子序列;当前的和为奇数的子序列个数等于之前的和为奇数的子序列拼上当前数 $x$ 的子序列个数,加上之前的和为奇数的子序列个数。 + +最终,返回 $f[1]$ 即可。 + +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 + + + +#### Python3 + +```python +class Solution: + def subsequenceCount(self, nums: List[int]) -> int: + mod = 10**9 + 7 + f = [0] * 2 + for x in nums: + if x % 2: + f[0], f[1] = (f[0] + f[1]) % mod, (f[0] + f[1] + 1) % mod + else: + f[0], f[1] = (f[0] + f[0] + 1) % mod, (f[1] + f[1]) % mod + return f[1] +``` + +#### Java + +```java +class Solution { + public int subsequenceCount(int[] nums) { + final int mod = (int) 1e9 + 7; + int[] f = new int[2]; + for (int x : nums) { + int[] g = new int[2]; + if (x % 2 == 1) { + g[0] = (f[0] + f[1]) % mod; + g[1] = (f[0] + f[1] + 1) % mod; + } else { + g[0] = (f[0] + f[0] + 1) % mod; + g[1] = (f[1] + f[1]) % mod; + } + f = g; + } + return f[1]; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int subsequenceCount(vector& nums) { + const int mod = 1e9 + 7; + vector f(2); + for (int x : nums) { + vector g(2); + if (x % 2 == 1) { + g[0] = (f[0] + f[1]) % mod; + g[1] = (f[0] + f[1] + 1) % mod; + } else { + g[0] = (f[0] + f[0] + 1) % mod; + g[1] = (f[1] + f[1]) % mod; + } + f = g; + } + return f[1]; + } +}; +``` + +#### Go + +```go +func subsequenceCount(nums []int) int { + mod := int(1e9 + 7) + f := [2]int{} + for _, x := range nums { + g := [2]int{} + if x%2 == 1 { + g[0] = (f[0] + f[1]) % mod + g[1] = (f[0] + f[1] + 1) % mod + } else { + g[0] = (f[0] + f[0] + 1) % mod + g[1] = (f[1] + f[1]) % mod + } + f = g + } + return f[1] +} +``` + +#### TypeScript + +```ts +function subsequenceCount(nums: number[]): number { + const mod = 1e9 + 7; + let f = [0, 0]; + for (const x of nums) { + const g = [0, 0]; + if (x % 2 === 1) { + g[0] = (f[0] + f[1]) % mod; + g[1] = (f[0] + f[1] + 1) % mod; + } else { + g[0] = (f[0] + f[0] + 1) % mod; + g[1] = (f[1] + f[1]) % mod; + } + f = g; + } + return f[1]; +} +``` + + + + + + diff --git a/solution/3200-3299/3247.Number of Subsequences with Odd Sum/README_EN.md b/solution/3200-3299/3247.Number of Subsequences with Odd Sum/README_EN.md new file mode 100644 index 0000000000000..79a4e49627384 --- /dev/null +++ b/solution/3200-3299/3247.Number of Subsequences with Odd Sum/README_EN.md @@ -0,0 +1,202 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3247.Number%20of%20Subsequences%20with%20Odd%20Sum/README_EN.md +--- + + + +# [3247. Number of Subsequences with Odd Sum 🔒](https://leetcode.com/problems/number-of-subsequences-with-odd-sum) + +[中文文档](/solution/3200-3299/3247.Number%20of%20Subsequences%20with%20Odd%20Sum/README.md) + +## Description + + + +

Given an array nums, return the number of subsequences with an odd sum of elements.

+ +

Since the answer may be very large, return it modulo 109 + 7.

+ +

 

+

Example 1:

+ +
+

Input: nums = [1,1,1]

+ +

Output: 4

+ +

Explanation:

+ +

The odd-sum subsequences are: [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1].

+
+ +

Example 2:

+ +
+

Input: nums = [1,2,2]

+ +

Output: 4

+ +

Explanation:

+ +

The odd-sum subsequences are: [1, 2, 2], [1, 2, 2], [1, 2, 2], [1, 2, 2].

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.lnegth <= 105
  • +
  • 1 <= nums[i] <= 109
  • +
+ + + +## Solutions + + + +### Solution 1: Dynamic Programming + +We define $f[0]$ to represent the number of subsequences with an even sum so far, and $f[1]$ to represent the number of subsequences with an odd sum so far. Initially, $f[0] = 0$ and $f[1] = 0$. + +Traverse the array $\textit{nums}$, for each number $x$: + +If $x$ is odd, the update rules for $f[0]$ and $f[1]$ are: + +$$ +\begin{aligned} +f[0] & = (f[0] + f[1]) \bmod 10^9 + 7, \\ +f[1] & = (f[0] + f[1] + 1) \bmod 10^9 + 7. +\end{aligned} +$$ + +That is, the current number of subsequences with an even sum is equal to the previous number of subsequences with an even sum plus the number of subsequences with an odd sum concatenated with the current number $x$; the current number of subsequences with an odd sum is equal to the previous number of subsequences with an even sum concatenated with the current number $x$ plus the previous number of subsequences with an odd sum, plus one subsequence containing only the current number $x$. + +If $x$ is even, the update rules for $f[0]$ and $f[1]$ are: + +$$ +\begin{aligned} +f[0] & = (f[0] + f[0] + 1) \bmod 10^9 + 7, \\ +f[1] & = (f[1] + f[1]) \bmod 10^9 + 7. +\end{aligned} +$$ + +That is, the current number of subsequences with an even sum is equal to the previous number of subsequences with an even sum plus the number of subsequences with an even sum concatenated with the current number $x$, plus one subsequence containing only the current number $x$; the current number of subsequences with an odd sum is equal to the previous number of subsequences with an odd sum concatenated with the current number $x$ plus the previous number of subsequences with an odd sum. + +Finally, return $f[1]$. + +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. + + + +#### Python3 + +```python +class Solution: + def subsequenceCount(self, nums: List[int]) -> int: + mod = 10**9 + 7 + f = [0] * 2 + for x in nums: + if x % 2: + f[0], f[1] = (f[0] + f[1]) % mod, (f[0] + f[1] + 1) % mod + else: + f[0], f[1] = (f[0] + f[0] + 1) % mod, (f[1] + f[1]) % mod + return f[1] +``` + +#### Java + +```java +class Solution { + public int subsequenceCount(int[] nums) { + final int mod = (int) 1e9 + 7; + int[] f = new int[2]; + for (int x : nums) { + int[] g = new int[2]; + if (x % 2 == 1) { + g[0] = (f[0] + f[1]) % mod; + g[1] = (f[0] + f[1] + 1) % mod; + } else { + g[0] = (f[0] + f[0] + 1) % mod; + g[1] = (f[1] + f[1]) % mod; + } + f = g; + } + return f[1]; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int subsequenceCount(vector& nums) { + const int mod = 1e9 + 7; + vector f(2); + for (int x : nums) { + vector g(2); + if (x % 2 == 1) { + g[0] = (f[0] + f[1]) % mod; + g[1] = (f[0] + f[1] + 1) % mod; + } else { + g[0] = (f[0] + f[0] + 1) % mod; + g[1] = (f[1] + f[1]) % mod; + } + f = g; + } + return f[1]; + } +}; +``` + +#### Go + +```go +func subsequenceCount(nums []int) int { + mod := int(1e9 + 7) + f := [2]int{} + for _, x := range nums { + g := [2]int{} + if x%2 == 1 { + g[0] = (f[0] + f[1]) % mod + g[1] = (f[0] + f[1] + 1) % mod + } else { + g[0] = (f[0] + f[0] + 1) % mod + g[1] = (f[1] + f[1]) % mod + } + f = g + } + return f[1] +} +``` + +#### TypeScript + +```ts +function subsequenceCount(nums: number[]): number { + const mod = 1e9 + 7; + let f = [0, 0]; + for (const x of nums) { + const g = [0, 0]; + if (x % 2 === 1) { + g[0] = (f[0] + f[1]) % mod; + g[1] = (f[0] + f[1] + 1) % mod; + } else { + g[0] = (f[0] + f[0] + 1) % mod; + g[1] = (f[1] + f[1]) % mod; + } + f = g; + } + return f[1]; +} +``` + + + + + + diff --git a/solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.cpp b/solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.cpp new file mode 100644 index 0000000000000..c2a88c814b994 --- /dev/null +++ b/solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int subsequenceCount(vector& nums) { + const int mod = 1e9 + 7; + vector f(2); + for (int x : nums) { + vector g(2); + if (x % 2 == 1) { + g[0] = (f[0] + f[1]) % mod; + g[1] = (f[0] + f[1] + 1) % mod; + } else { + g[0] = (f[0] + f[0] + 1) % mod; + g[1] = (f[1] + f[1]) % mod; + } + f = g; + } + return f[1]; + } +}; diff --git a/solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.go b/solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.go new file mode 100644 index 0000000000000..0ddb57c6aca83 --- /dev/null +++ b/solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.go @@ -0,0 +1,16 @@ +func subsequenceCount(nums []int) int { + mod := int(1e9 + 7) + f := [2]int{} + for _, x := range nums { + g := [2]int{} + if x%2 == 1 { + g[0] = (f[0] + f[1]) % mod + g[1] = (f[0] + f[1] + 1) % mod + } else { + g[0] = (f[0] + f[0] + 1) % mod + g[1] = (f[1] + f[1]) % mod + } + f = g + } + return f[1] +} diff --git a/solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.java b/solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.java new file mode 100644 index 0000000000000..f9312a8a8267a --- /dev/null +++ b/solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.java @@ -0,0 +1,18 @@ +class Solution { + public int subsequenceCount(int[] nums) { + final int mod = (int) 1e9 + 7; + int[] f = new int[2]; + for (int x : nums) { + int[] g = new int[2]; + if (x % 2 == 1) { + g[0] = (f[0] + f[1]) % mod; + g[1] = (f[0] + f[1] + 1) % mod; + } else { + g[0] = (f[0] + f[0] + 1) % mod; + g[1] = (f[1] + f[1]) % mod; + } + f = g; + } + return f[1]; + } +} diff --git a/solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.py b/solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.py new file mode 100644 index 0000000000000..421f8d00986ba --- /dev/null +++ b/solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.py @@ -0,0 +1,10 @@ +class Solution: + def subsequenceCount(self, nums: List[int]) -> int: + mod = 10**9 + 7 + f = [0] * 2 + for x in nums: + if x % 2: + f[0], f[1] = (f[0] + f[1]) % mod, (f[0] + f[1] + 1) % mod + else: + f[0], f[1] = (f[0] + f[0] + 1) % mod, (f[1] + f[1]) % mod + return f[1] diff --git a/solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.ts b/solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.ts new file mode 100644 index 0000000000000..3aa3745777cf5 --- /dev/null +++ b/solution/3200-3299/3247.Number of Subsequences with Odd Sum/Solution.ts @@ -0,0 +1,16 @@ +function subsequenceCount(nums: number[]): number { + const mod = 1e9 + 7; + let f = [0, 0]; + for (const x of nums) { + const g = [0, 0]; + if (x % 2 === 1) { + g[0] = (f[0] + f[1]) % mod; + g[1] = (f[0] + f[1] + 1) % mod; + } else { + g[0] = (f[0] + f[0] + 1) % mod; + g[1] = (f[1] + f[1]) % mod; + } + f = g; + } + return f[1]; +} diff --git a/solution/README.md b/solution/README.md index b4f17b9babd98..f9d5d6be164c8 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3257,6 +3257,7 @@ | 3244 | [新增道路查询后的最短距离 II](/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/README.md) | `贪心`,`图`,`数组`,`有序集合` | 困难 | 第 409 场周赛 | | 3245 | [交替组 III](/solution/3200-3299/3245.Alternating%20Groups%20III/README.md) | `树状数组`,`数组` | 困难 | 第 409 场周赛 | | 3246 | [英超积分榜排名](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README.md) | `数据库` | 简单 | 🔒 | +| 3247 | [Number of Subsequences with Odd Sum](/solution/3200-3299/3247.Number%20of%20Subsequences%20with%20Odd%20Sum/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 626678a57b8ee..f3c6e75ef54ba 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3255,6 +3255,7 @@ Press Control + F(or Command + F on | 3244 | [Shortest Distance After Road Addition Queries II](/solution/3200-3299/3244.Shortest%20Distance%20After%20Road%20Addition%20Queries%20II/README_EN.md) | `Greedy`,`Graph`,`Array`,`Ordered Set` | Hard | Weekly Contest 409 | | 3245 | [Alternating Groups III](/solution/3200-3299/3245.Alternating%20Groups%20III/README_EN.md) | `Binary Indexed Tree`,`Array` | Hard | Weekly Contest 409 | | 3246 | [Premier League Table Ranking](/solution/3200-3299/3246.Premier%20League%20Table%20Ranking/README_EN.md) | `Database` | Easy | 🔒 | +| 3247 | [Number of Subsequences with Odd Sum](/solution/3200-3299/3247.Number%20of%20Subsequences%20with%20Odd%20Sum/README_EN.md) | | Medium | 🔒 | ## Copyright diff --git a/solution/util.py b/solution/util.py index ad124cb1069d5..1187e07f648c2 100644 --- a/solution/util.py +++ b/solution/util.py @@ -203,7 +203,7 @@ def generate_question_readme(result): "edit_url": f'https://github.com/doocs/leetcode/edit/main{item["relative_path_cn"]}', "source": source, } - if not item["tags_cn"]: + if not item["tags_cn"] or metadata["tags"] == ["Algorithms"]: metadata.pop("tags") if not rating: metadata.pop("rating") @@ -242,7 +242,7 @@ def generate_question_readme(result): "edit_url": f'https://github.com/doocs/leetcode/edit/main{item["relative_path_en"]}', "source": source, } - if not item["tags_cn"]: + if not item["tags_cn"] or metadata["tags"] == ["Algorithms"]: metadata.pop("tags") if not rating: metadata.pop("rating") @@ -382,7 +382,7 @@ def refresh(result): "source": source, } - if not question["tags_cn"] and not cat: + if (not question["tags_cn"] and not cat) or metadata["tags"] == ["Algorithms"]: metadata.pop("tags") if not rating: metadata.pop("rating") @@ -420,7 +420,9 @@ def refresh(result): "edit_url": f'https://github.com/doocs/leetcode/edit/main{question["relative_path_en"]}', "source": source, } - if not question["tags_en"] and not [category]: + if (not question["tags_en"] and not [category]) or metadata["tags"] == [ + "Algorithms" + ]: metadata.pop("tags") if not rating: metadata.pop("rating")