Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8153c9b

Browse files
feat: add solutions to lc problem: No.3351 (#3749)
No.3351.Sum of Good Subsequences
1 parent c599399 commit 8153c9b

File tree

7 files changed

+344
-6
lines changed

7 files changed

+344
-6
lines changed

‎solution/3300-3399/3351.Sum of Good Subsequences/README.md‎

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,138 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3351.Su
7979
#### Python3
8080

8181
```python
82-
82+
class Solution:
83+
def sumOfGoodSubsequences(self, nums: List[int]) -> int:
84+
mod = 10**9 + 7
85+
f = defaultdict(int)
86+
g = defaultdict(int)
87+
for x in nums:
88+
f[x] += x
89+
g[x] += 1
90+
f[x] += f[x - 1] + g[x - 1] * x
91+
g[x] += g[x - 1]
92+
f[x] += f[x + 1] + g[x + 1] * x
93+
g[x] += g[x + 1]
94+
return sum(f.values()) % mod
8395
```
8496

8597
#### Java
8698

8799
```java
88-
100+
class Solution {
101+
public int sumOfGoodSubsequences(int[] nums) {
102+
final int mod = (int) 1e9 + 7;
103+
int mx = 0;
104+
for (int x : nums) {
105+
mx = Math.max(mx, x);
106+
}
107+
long[] f = new long[mx + 1];
108+
long[] g = new long[mx + 1];
109+
for (int x : nums) {
110+
f[x] += x;
111+
g[x] += 1;
112+
if (x > 0) {
113+
f[x] = (f[x] + f[x - 1] + g[x - 1] * x % mod) % mod;
114+
g[x] = (g[x] + g[x - 1]) % mod;
115+
}
116+
if (x + 1 <= mx) {
117+
f[x] = (f[x] + f[x + 1] + g[x + 1] * x % mod) % mod;
118+
g[x] = (g[x] + g[x + 1]) % mod;
119+
}
120+
}
121+
long ans = 0;
122+
for (long x : f) {
123+
ans = (ans + x) % mod;
124+
}
125+
return (int) ans;
126+
}
127+
}
89128
```
90129

91130
#### C++
92131

93132
```cpp
94-
133+
class Solution {
134+
public:
135+
int sumOfGoodSubsequences(vector<int>& nums) {
136+
const int mod = 1e9 + 7;
137+
int mx = ranges::max(nums);
138+
139+
vector<long long> f(mx + 1), g(mx + 1);
140+
for (int x : nums) {
141+
f[x] += x;
142+
g[x] += 1;
143+
144+
if (x > 0) {
145+
f[x] = (f[x] + f[x - 1] + g[x - 1] * x % mod) % mod;
146+
g[x] = (g[x] + g[x - 1]) % mod;
147+
}
148+
149+
if (x + 1 <= mx) {
150+
f[x] = (f[x] + f[x + 1] + g[x + 1] * x % mod) % mod;
151+
g[x] = (g[x] + g[x + 1]) % mod;
152+
}
153+
}
154+
155+
return accumulate(f.begin(), f.end(), 0LL) % mod;
156+
}
157+
};
95158
```
96159

97160
#### Go
98161

99162
```go
163+
func sumOfGoodSubsequences(nums []int) (ans int) {
164+
mod := int(1e9 + 7)
165+
mx := slices.Max(nums)
166+
167+
f := make([]int, mx+1)
168+
g := make([]int, mx+1)
169+
170+
for _, x := range nums {
171+
f[x] += x
172+
g[x] += 1
173+
174+
if x > 0 {
175+
f[x] = (f[x] + f[x-1] + g[x-1]*x%mod) % mod
176+
g[x] = (g[x] + g[x-1]) % mod
177+
}
178+
179+
if x+1 <= mx {
180+
f[x] = (f[x] + f[x+1] + g[x+1]*x%mod) % mod
181+
g[x] = (g[x] + g[x+1]) % mod
182+
}
183+
}
184+
185+
for _, x := range f {
186+
ans = (ans + x) % mod
187+
}
188+
return
189+
}
190+
```
100191

192+
#### TypeScript
193+
194+
```ts
195+
function sumOfGoodSubsequences(nums: number[]): number {
196+
const mod = 10 ** 9 + 7;
197+
const mx = Math.max(...nums);
198+
const f: number[] = Array(mx + 1).fill(0);
199+
const g: number[] = Array(mx + 1).fill(0);
200+
for (const x of nums) {
201+
f[x] += x;
202+
g[x] += 1;
203+
if (x > 0) {
204+
f[x] = (f[x] + f[x - 1] + ((g[x - 1] * x) % mod)) % mod;
205+
g[x] = (g[x] + g[x - 1]) % mod;
206+
}
207+
if (x + 1 <= mx) {
208+
f[x] = (f[x] + f[x + 1] + ((g[x + 1] * x) % mod)) % mod;
209+
g[x] = (g[x] + g[x + 1]) % mod;
210+
}
211+
}
212+
return f.reduce((acc, cur) => (acc + cur) % mod, 0);
213+
}
101214
```
102215

103216
<!-- tabs:end -->

‎solution/3300-3399/3351.Sum of Good Subsequences/README_EN.md‎

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,138 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3351.Su
7474
#### Python3
7575

7676
```python
77-
77+
class Solution:
78+
def sumOfGoodSubsequences(self, nums: List[int]) -> int:
79+
mod = 10**9 + 7
80+
f = defaultdict(int)
81+
g = defaultdict(int)
82+
for x in nums:
83+
f[x] += x
84+
g[x] += 1
85+
f[x] += f[x - 1] + g[x - 1] * x
86+
g[x] += g[x - 1]
87+
f[x] += f[x + 1] + g[x + 1] * x
88+
g[x] += g[x + 1]
89+
return sum(f.values()) % mod
7890
```
7991

8092
#### Java
8193

8294
```java
83-
95+
class Solution {
96+
public int sumOfGoodSubsequences(int[] nums) {
97+
final int mod = (int) 1e9 + 7;
98+
int mx = 0;
99+
for (int x : nums) {
100+
mx = Math.max(mx, x);
101+
}
102+
long[] f = new long[mx + 1];
103+
long[] g = new long[mx + 1];
104+
for (int x : nums) {
105+
f[x] += x;
106+
g[x] += 1;
107+
if (x > 0) {
108+
f[x] = (f[x] + f[x - 1] + g[x - 1] * x % mod) % mod;
109+
g[x] = (g[x] + g[x - 1]) % mod;
110+
}
111+
if (x + 1 <= mx) {
112+
f[x] = (f[x] + f[x + 1] + g[x + 1] * x % mod) % mod;
113+
g[x] = (g[x] + g[x + 1]) % mod;
114+
}
115+
}
116+
long ans = 0;
117+
for (long x : f) {
118+
ans = (ans + x) % mod;
119+
}
120+
return (int) ans;
121+
}
122+
}
84123
```
85124

86125
#### C++
87126

88127
```cpp
89-
128+
class Solution {
129+
public:
130+
int sumOfGoodSubsequences(vector<int>& nums) {
131+
const int mod = 1e9 + 7;
132+
int mx = ranges::max(nums);
133+
134+
vector<long long> f(mx + 1), g(mx + 1);
135+
for (int x : nums) {
136+
f[x] += x;
137+
g[x] += 1;
138+
139+
if (x > 0) {
140+
f[x] = (f[x] + f[x - 1] + g[x - 1] * x % mod) % mod;
141+
g[x] = (g[x] + g[x - 1]) % mod;
142+
}
143+
144+
if (x + 1 <= mx) {
145+
f[x] = (f[x] + f[x + 1] + g[x + 1] * x % mod) % mod;
146+
g[x] = (g[x] + g[x + 1]) % mod;
147+
}
148+
}
149+
150+
return accumulate(f.begin(), f.end(), 0LL) % mod;
151+
}
152+
};
90153
```
91154

92155
#### Go
93156

94157
```go
158+
func sumOfGoodSubsequences(nums []int) (ans int) {
159+
mod := int(1e9 + 7)
160+
mx := slices.Max(nums)
161+
162+
f := make([]int, mx+1)
163+
g := make([]int, mx+1)
164+
165+
for _, x := range nums {
166+
f[x] += x
167+
g[x] += 1
168+
169+
if x > 0 {
170+
f[x] = (f[x] + f[x-1] + g[x-1]*x%mod) % mod
171+
g[x] = (g[x] + g[x-1]) % mod
172+
}
173+
174+
if x+1 <= mx {
175+
f[x] = (f[x] + f[x+1] + g[x+1]*x%mod) % mod
176+
g[x] = (g[x] + g[x+1]) % mod
177+
}
178+
}
179+
180+
for _, x := range f {
181+
ans = (ans + x) % mod
182+
}
183+
return
184+
}
185+
```
95186

187+
#### TypeScript
188+
189+
```ts
190+
function sumOfGoodSubsequences(nums: number[]): number {
191+
const mod = 10 ** 9 + 7;
192+
const mx = Math.max(...nums);
193+
const f: number[] = Array(mx + 1).fill(0);
194+
const g: number[] = Array(mx + 1).fill(0);
195+
for (const x of nums) {
196+
f[x] += x;
197+
g[x] += 1;
198+
if (x > 0) {
199+
f[x] = (f[x] + f[x - 1] + ((g[x - 1] * x) % mod)) % mod;
200+
g[x] = (g[x] + g[x - 1]) % mod;
201+
}
202+
if (x + 1 <= mx) {
203+
f[x] = (f[x] + f[x + 1] + ((g[x + 1] * x) % mod)) % mod;
204+
g[x] = (g[x] + g[x + 1]) % mod;
205+
}
206+
}
207+
return f.reduce((acc, cur) => (acc + cur) % mod, 0);
208+
}
96209
```
97210

98211
<!-- tabs:end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int sumOfGoodSubsequences(vector<int>& nums) {
4+
const int mod = 1e9 + 7;
5+
int mx = ranges::max(nums);
6+
7+
vector<long long> f(mx + 1), g(mx + 1);
8+
for (int x : nums) {
9+
f[x] += x;
10+
g[x] += 1;
11+
12+
if (x > 0) {
13+
f[x] = (f[x] + f[x - 1] + g[x - 1] * x % mod) % mod;
14+
g[x] = (g[x] + g[x - 1]) % mod;
15+
}
16+
17+
if (x + 1 <= mx) {
18+
f[x] = (f[x] + f[x + 1] + g[x + 1] * x % mod) % mod;
19+
g[x] = (g[x] + g[x + 1]) % mod;
20+
}
21+
}
22+
23+
return accumulate(f.begin(), f.end(), 0LL) % mod;
24+
}
25+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
func sumOfGoodSubsequences(nums []int) (ans int) {
2+
mod := int(1e9 + 7)
3+
mx := slices.Max(nums)
4+
5+
f := make([]int, mx+1)
6+
g := make([]int, mx+1)
7+
8+
for _, x := range nums {
9+
f[x] += x
10+
g[x] += 1
11+
12+
if x > 0 {
13+
f[x] = (f[x] + f[x-1] + g[x-1]*x%mod) % mod
14+
g[x] = (g[x] + g[x-1]) % mod
15+
}
16+
17+
if x+1 <= mx {
18+
f[x] = (f[x] + f[x+1] + g[x+1]*x%mod) % mod
19+
g[x] = (g[x] + g[x+1]) % mod
20+
}
21+
}
22+
23+
for _, x := range f {
24+
ans = (ans + x) % mod
25+
}
26+
return
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int sumOfGoodSubsequences(int[] nums) {
3+
final int mod = (int) 1e9 + 7;
4+
int mx = 0;
5+
for (int x : nums) {
6+
mx = Math.max(mx, x);
7+
}
8+
long[] f = new long[mx + 1];
9+
long[] g = new long[mx + 1];
10+
for (int x : nums) {
11+
f[x] += x;
12+
g[x] += 1;
13+
if (x > 0) {
14+
f[x] = (f[x] + f[x - 1] + g[x - 1] * x % mod) % mod;
15+
g[x] = (g[x] + g[x - 1]) % mod;
16+
}
17+
if (x + 1 <= mx) {
18+
f[x] = (f[x] + f[x + 1] + g[x + 1] * x % mod) % mod;
19+
g[x] = (g[x] + g[x + 1]) % mod;
20+
}
21+
}
22+
long ans = 0;
23+
for (long x : f) {
24+
ans = (ans + x) % mod;
25+
}
26+
return (int) ans;
27+
}
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def sumOfGoodSubsequences(self, nums: List[int]) -> int:
3+
mod = 10**9 + 7
4+
f = defaultdict(int)
5+
g = defaultdict(int)
6+
for x in nums:
7+
f[x] += x
8+
g[x] += 1
9+
f[x] += f[x - 1] + g[x - 1] * x
10+
g[x] += g[x - 1]
11+
f[x] += f[x + 1] + g[x + 1] * x
12+
g[x] += g[x + 1]
13+
return sum(f.values()) % mod

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /