|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: 中等 |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3247.Number%20of%20Subsequences%20with%20Odd%20Sum/README.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3247. Number of Subsequences with Odd Sum 🔒](https://leetcode.cn/problems/number-of-subsequences-with-odd-sum) |
| 10 | + |
| 11 | +[English Version](/solution/3200-3299/3247.Number%20of%20Subsequences%20with%20Odd%20Sum/README_EN.md) |
| 12 | + |
| 13 | +## 题目描述 |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>Given an array <code>nums</code>, return the number of <span data-keyword="subsequence-array">subsequences</span> with an odd sum of elements.</p> |
| 18 | + |
| 19 | +<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> |
| 20 | + |
| 21 | +<p> </p> |
| 22 | +<p><strong class="example">Example 1:</strong></p> |
| 23 | + |
| 24 | +<div class="example-block"> |
| 25 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1]</span></p> |
| 26 | + |
| 27 | +<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 28 | + |
| 29 | +<p><strong>Explanation:</strong></p> |
| 30 | + |
| 31 | +<p>The odd-sum subsequences are: <code>[<u><strong>1</strong></u>, 1, 1]</code>, <code>[1, <u><strong>1</strong></u>, 1],</code> <code>[1, 1, <u><strong>1</strong></u>]</code>, <code>[<u><strong>1, 1, 1</strong></u>]</code>.</p> |
| 32 | +</div> |
| 33 | + |
| 34 | +<p><strong class="example">Example 2:</strong></p> |
| 35 | + |
| 36 | +<div class="example-block"> |
| 37 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,2,2]</span></p> |
| 38 | + |
| 39 | +<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 40 | + |
| 41 | +<p><strong>Explanation:</strong></p> |
| 42 | + |
| 43 | +<p>The odd-sum subsequences are: <code>[<u><strong>1</strong></u>, 2, 2]</code>, <code>[<u><strong>1, 2</strong></u>, 2],</code> <code>[<u><strong>1</strong></u>, 2, <b><u>2</u></b>]</code>, <code>[<u><strong>1, 2, 2</strong></u>]</code>.</p> |
| 44 | +</div> |
| 45 | + |
| 46 | +<p> </p> |
| 47 | +<p><strong>Constraints:</strong></p> |
| 48 | + |
| 49 | +<ul> |
| 50 | + <li><code>1 <= nums.lnegth <= 10<sup>5</sup></code></li> |
| 51 | + <li><code>1 <= nums[i] <= 10<sup>9</sup></code></li> |
| 52 | +</ul> |
| 53 | + |
| 54 | +<!-- description:end --> |
| 55 | + |
| 56 | +## 解法 |
| 57 | + |
| 58 | +<!-- solution:start --> |
| 59 | + |
| 60 | +### 方法一:动态规划 |
| 61 | + |
| 62 | +我们定义 $f[0]$ 表示目前为止的子序列中,和为偶数的子序列个数,而 $f[1]$ 表示目前为止的子序列中,和为奇数的子序列个数。初始时 $f[0] = 0$, $f[1] = 0$。 |
| 63 | + |
| 64 | +遍历数组 $\textit{nums}$,对于每个数 $x$: |
| 65 | + |
| 66 | +如果 $x$ 为奇数,那么 $f[0]$ 和 $f[1]$ 的更新方式为: |
| 67 | + |
| 68 | +$$ |
| 69 | +\begin{aligned} |
| 70 | +f[0] & = (f[0] + f[1]) \bmod 10^9 + 7, \\ |
| 71 | +f[1] & = (f[0] + f[1] + 1) \bmod 10^9 + 7. |
| 72 | +\end{aligned} |
| 73 | +$$ |
| 74 | + |
| 75 | +即,当前的和为偶数的子序列个数等于之前的和为偶数的子序列个数,加上之前的和为奇数的子序列拼上当前数 $x$ 的子序列个数;当前的和为奇数的子序列个数等于之前的和为偶数的子序列拼上当前数 $x$ 的子序列个数,加上之前的和为奇数的子序列个数,再加上一个只包含当前数 $x$ 的子序列。 |
| 76 | + |
| 77 | +如果 $x$ 为偶数,那么 $f[0]$ 和 $f[1]$ 的更新方式为: |
| 78 | + |
| 79 | +$$ |
| 80 | +\begin{aligned} |
| 81 | +f[0] & = (f[0] + f[0] + 1) \bmod 10^9 + 7, \\ |
| 82 | +f[1] & = (f[1] + f[1]) \bmod 10^9 + 7. |
| 83 | +\end{aligned} |
| 84 | +$$ |
| 85 | + |
| 86 | +即,当前的和为偶数的子序列个数等于之前的和为偶数的子序列个数,加上之前的和为偶数的子序列拼上当前数 $x$ 的子序列个数,再加上一个只包含当前数 $x$ 的子序列;当前的和为奇数的子序列个数等于之前的和为奇数的子序列拼上当前数 $x$ 的子序列个数,加上之前的和为奇数的子序列个数。 |
| 87 | + |
| 88 | +最终,返回 $f[1]$ 即可。 |
| 89 | + |
| 90 | +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 |
| 91 | + |
| 92 | +<!-- tabs:start --> |
| 93 | + |
| 94 | +#### Python3 |
| 95 | + |
| 96 | +```python |
| 97 | +class Solution: |
| 98 | + def subsequenceCount(self, nums: List[int]) -> int: |
| 99 | + mod = 10**9 + 7 |
| 100 | + f = [0] * 2 |
| 101 | + for x in nums: |
| 102 | + if x % 2: |
| 103 | + f[0], f[1] = (f[0] + f[1]) % mod, (f[0] + f[1] + 1) % mod |
| 104 | + else: |
| 105 | + f[0], f[1] = (f[0] + f[0] + 1) % mod, (f[1] + f[1]) % mod |
| 106 | + return f[1] |
| 107 | +``` |
| 108 | + |
| 109 | +#### Java |
| 110 | + |
| 111 | +```java |
| 112 | +class Solution { |
| 113 | + public int subsequenceCount(int[] nums) { |
| 114 | + final int mod = (int) 1e9 + 7; |
| 115 | + int[] f = new int[2]; |
| 116 | + for (int x : nums) { |
| 117 | + int[] g = new int[2]; |
| 118 | + if (x % 2 == 1) { |
| 119 | + g[0] = (f[0] + f[1]) % mod; |
| 120 | + g[1] = (f[0] + f[1] + 1) % mod; |
| 121 | + } else { |
| 122 | + g[0] = (f[0] + f[0] + 1) % mod; |
| 123 | + g[1] = (f[1] + f[1]) % mod; |
| 124 | + } |
| 125 | + f = g; |
| 126 | + } |
| 127 | + return f[1]; |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +#### C++ |
| 133 | + |
| 134 | +```cpp |
| 135 | +class Solution { |
| 136 | +public: |
| 137 | + int subsequenceCount(vector<int>& nums) { |
| 138 | + const int mod = 1e9 + 7; |
| 139 | + vector<int> f(2); |
| 140 | + for (int x : nums) { |
| 141 | + vector<int> g(2); |
| 142 | + if (x % 2 == 1) { |
| 143 | + g[0] = (f[0] + f[1]) % mod; |
| 144 | + g[1] = (f[0] + f[1] + 1) % mod; |
| 145 | + } else { |
| 146 | + g[0] = (f[0] + f[0] + 1) % mod; |
| 147 | + g[1] = (f[1] + f[1]) % mod; |
| 148 | + } |
| 149 | + f = g; |
| 150 | + } |
| 151 | + return f[1]; |
| 152 | + } |
| 153 | +}; |
| 154 | +``` |
| 155 | +
|
| 156 | +#### Go |
| 157 | +
|
| 158 | +```go |
| 159 | +func subsequenceCount(nums []int) int { |
| 160 | + mod := int(1e9 + 7) |
| 161 | + f := [2]int{} |
| 162 | + for _, x := range nums { |
| 163 | + g := [2]int{} |
| 164 | + if x%2 == 1 { |
| 165 | + g[0] = (f[0] + f[1]) % mod |
| 166 | + g[1] = (f[0] + f[1] + 1) % mod |
| 167 | + } else { |
| 168 | + g[0] = (f[0] + f[0] + 1) % mod |
| 169 | + g[1] = (f[1] + f[1]) % mod |
| 170 | + } |
| 171 | + f = g |
| 172 | + } |
| 173 | + return f[1] |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +#### TypeScript |
| 178 | + |
| 179 | +```ts |
| 180 | +function subsequenceCount(nums: number[]): number { |
| 181 | + const mod = 1e9 + 7; |
| 182 | + let f = [0, 0]; |
| 183 | + for (const x of nums) { |
| 184 | + const g = [0, 0]; |
| 185 | + if (x % 2 === 1) { |
| 186 | + g[0] = (f[0] + f[1]) % mod; |
| 187 | + g[1] = (f[0] + f[1] + 1) % mod; |
| 188 | + } else { |
| 189 | + g[0] = (f[0] + f[0] + 1) % mod; |
| 190 | + g[1] = (f[1] + f[1]) % mod; |
| 191 | + } |
| 192 | + f = g; |
| 193 | + } |
| 194 | + return f[1]; |
| 195 | +} |
| 196 | +``` |
| 197 | + |
| 198 | +<!-- tabs:end --> |
| 199 | + |
| 200 | +<!-- solution:end --> |
| 201 | + |
| 202 | +<!-- problem:end --> |
0 commit comments