Skip to content

feat: add solutions to lc problem: No.3351 #3749

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
Nov 11, 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
119 changes: 116 additions & 3 deletions solution/3300-3399/3351.Sum of Good Subsequences/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,138 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3351.Su
#### Python3

```python

class Solution:
def sumOfGoodSubsequences(self, nums: List[int]) -> int:
mod = 10**9 + 7
f = defaultdict(int)
g = defaultdict(int)
for x in nums:
f[x] += x
g[x] += 1
f[x] += f[x - 1] + g[x - 1] * x
g[x] += g[x - 1]
f[x] += f[x + 1] + g[x + 1] * x
g[x] += g[x + 1]
return sum(f.values()) % mod
```

#### Java

```java

class Solution {
public int sumOfGoodSubsequences(int[] nums) {
final int mod = (int) 1e9 + 7;
int mx = 0;
for (int x : nums) {
mx = Math.max(mx, x);
}
long[] f = new long[mx + 1];
long[] g = new long[mx + 1];
for (int x : nums) {
f[x] += x;
g[x] += 1;
if (x > 0) {
f[x] = (f[x] + f[x - 1] + g[x - 1] * x % mod) % mod;
g[x] = (g[x] + g[x - 1]) % mod;
}
if (x + 1 <= mx) {
f[x] = (f[x] + f[x + 1] + g[x + 1] * x % mod) % mod;
g[x] = (g[x] + g[x + 1]) % mod;
}
}
long ans = 0;
for (long x : f) {
ans = (ans + x) % mod;
}
return (int) ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int sumOfGoodSubsequences(vector<int>& nums) {
const int mod = 1e9 + 7;
int mx = ranges::max(nums);

vector<long long> f(mx + 1), g(mx + 1);
for (int x : nums) {
f[x] += x;
g[x] += 1;

if (x > 0) {
f[x] = (f[x] + f[x - 1] + g[x - 1] * x % mod) % mod;
g[x] = (g[x] + g[x - 1]) % mod;
}

if (x + 1 <= mx) {
f[x] = (f[x] + f[x + 1] + g[x + 1] * x % mod) % mod;
g[x] = (g[x] + g[x + 1]) % mod;
}
}

return accumulate(f.begin(), f.end(), 0LL) % mod;
}
};
```

#### Go

```go
func sumOfGoodSubsequences(nums []int) (ans int) {
mod := int(1e9 + 7)
mx := slices.Max(nums)

f := make([]int, mx+1)
g := make([]int, mx+1)

for _, x := range nums {
f[x] += x
g[x] += 1

if x > 0 {
f[x] = (f[x] + f[x-1] + g[x-1]*x%mod) % mod
g[x] = (g[x] + g[x-1]) % mod
}

if x+1 <= mx {
f[x] = (f[x] + f[x+1] + g[x+1]*x%mod) % mod
g[x] = (g[x] + g[x+1]) % mod
}
}

for _, x := range f {
ans = (ans + x) % mod
}
return
}
```

#### TypeScript

```ts
function sumOfGoodSubsequences(nums: number[]): number {
const mod = 10 ** 9 + 7;
const mx = Math.max(...nums);
const f: number[] = Array(mx + 1).fill(0);
const g: number[] = Array(mx + 1).fill(0);
for (const x of nums) {
f[x] += x;
g[x] += 1;
if (x > 0) {
f[x] = (f[x] + f[x - 1] + ((g[x - 1] * x) % mod)) % mod;
g[x] = (g[x] + g[x - 1]) % mod;
}
if (x + 1 <= mx) {
f[x] = (f[x] + f[x + 1] + ((g[x + 1] * x) % mod)) % mod;
g[x] = (g[x] + g[x + 1]) % mod;
}
}
return f.reduce((acc, cur) => (acc + cur) % mod, 0);
}
```

<!-- tabs:end -->
Expand Down
119 changes: 116 additions & 3 deletions solution/3300-3399/3351.Sum of Good Subsequences/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,138 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3351.Su
#### Python3

```python

class Solution:
def sumOfGoodSubsequences(self, nums: List[int]) -> int:
mod = 10**9 + 7
f = defaultdict(int)
g = defaultdict(int)
for x in nums:
f[x] += x
g[x] += 1
f[x] += f[x - 1] + g[x - 1] * x
g[x] += g[x - 1]
f[x] += f[x + 1] + g[x + 1] * x
g[x] += g[x + 1]
return sum(f.values()) % mod
```

#### Java

```java

class Solution {
public int sumOfGoodSubsequences(int[] nums) {
final int mod = (int) 1e9 + 7;
int mx = 0;
for (int x : nums) {
mx = Math.max(mx, x);
}
long[] f = new long[mx + 1];
long[] g = new long[mx + 1];
for (int x : nums) {
f[x] += x;
g[x] += 1;
if (x > 0) {
f[x] = (f[x] + f[x - 1] + g[x - 1] * x % mod) % mod;
g[x] = (g[x] + g[x - 1]) % mod;
}
if (x + 1 <= mx) {
f[x] = (f[x] + f[x + 1] + g[x + 1] * x % mod) % mod;
g[x] = (g[x] + g[x + 1]) % mod;
}
}
long ans = 0;
for (long x : f) {
ans = (ans + x) % mod;
}
return (int) ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int sumOfGoodSubsequences(vector<int>& nums) {
const int mod = 1e9 + 7;
int mx = ranges::max(nums);

vector<long long> f(mx + 1), g(mx + 1);
for (int x : nums) {
f[x] += x;
g[x] += 1;

if (x > 0) {
f[x] = (f[x] + f[x - 1] + g[x - 1] * x % mod) % mod;
g[x] = (g[x] + g[x - 1]) % mod;
}

if (x + 1 <= mx) {
f[x] = (f[x] + f[x + 1] + g[x + 1] * x % mod) % mod;
g[x] = (g[x] + g[x + 1]) % mod;
}
}

return accumulate(f.begin(), f.end(), 0LL) % mod;
}
};
```

#### Go

```go
func sumOfGoodSubsequences(nums []int) (ans int) {
mod := int(1e9 + 7)
mx := slices.Max(nums)

f := make([]int, mx+1)
g := make([]int, mx+1)

for _, x := range nums {
f[x] += x
g[x] += 1

if x > 0 {
f[x] = (f[x] + f[x-1] + g[x-1]*x%mod) % mod
g[x] = (g[x] + g[x-1]) % mod
}

if x+1 <= mx {
f[x] = (f[x] + f[x+1] + g[x+1]*x%mod) % mod
g[x] = (g[x] + g[x+1]) % mod
}
}

for _, x := range f {
ans = (ans + x) % mod
}
return
}
```

#### TypeScript

```ts
function sumOfGoodSubsequences(nums: number[]): number {
const mod = 10 ** 9 + 7;
const mx = Math.max(...nums);
const f: number[] = Array(mx + 1).fill(0);
const g: number[] = Array(mx + 1).fill(0);
for (const x of nums) {
f[x] += x;
g[x] += 1;
if (x > 0) {
f[x] = (f[x] + f[x - 1] + ((g[x - 1] * x) % mod)) % mod;
g[x] = (g[x] + g[x - 1]) % mod;
}
if (x + 1 <= mx) {
f[x] = (f[x] + f[x + 1] + ((g[x + 1] * x) % mod)) % mod;
g[x] = (g[x] + g[x + 1]) % mod;
}
}
return f.reduce((acc, cur) => (acc + cur) % mod, 0);
}
```

<!-- tabs:end -->
Expand Down
25 changes: 25 additions & 0 deletions solution/3300-3399/3351.Sum of Good Subsequences/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
int sumOfGoodSubsequences(vector<int>& nums) {
const int mod = 1e9 + 7;
int mx = ranges::max(nums);

vector<long long> f(mx + 1), g(mx + 1);
for (int x : nums) {
f[x] += x;
g[x] += 1;

if (x > 0) {
f[x] = (f[x] + f[x - 1] + g[x - 1] * x % mod) % mod;
g[x] = (g[x] + g[x - 1]) % mod;
}

if (x + 1 <= mx) {
f[x] = (f[x] + f[x + 1] + g[x + 1] * x % mod) % mod;
g[x] = (g[x] + g[x + 1]) % mod;
}
}

return accumulate(f.begin(), f.end(), 0LL) % mod;
}
};
27 changes: 27 additions & 0 deletions solution/3300-3399/3351.Sum of Good Subsequences/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
func sumOfGoodSubsequences(nums []int) (ans int) {
mod := int(1e9 + 7)
mx := slices.Max(nums)

f := make([]int, mx+1)
g := make([]int, mx+1)

for _, x := range nums {
f[x] += x
g[x] += 1

if x > 0 {
f[x] = (f[x] + f[x-1] + g[x-1]*x%mod) % mod
g[x] = (g[x] + g[x-1]) % mod
}

if x+1 <= mx {
f[x] = (f[x] + f[x+1] + g[x+1]*x%mod) % mod
g[x] = (g[x] + g[x+1]) % mod
}
}

for _, x := range f {
ans = (ans + x) % mod
}
return
}
28 changes: 28 additions & 0 deletions solution/3300-3399/3351.Sum of Good Subsequences/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public int sumOfGoodSubsequences(int[] nums) {
final int mod = (int) 1e9 + 7;
int mx = 0;
for (int x : nums) {
mx = Math.max(mx, x);
}
long[] f = new long[mx + 1];
long[] g = new long[mx + 1];
for (int x : nums) {
f[x] += x;
g[x] += 1;
if (x > 0) {
f[x] = (f[x] + f[x - 1] + g[x - 1] * x % mod) % mod;
g[x] = (g[x] + g[x - 1]) % mod;
}
if (x + 1 <= mx) {
f[x] = (f[x] + f[x + 1] + g[x + 1] * x % mod) % mod;
g[x] = (g[x] + g[x + 1]) % mod;
}
}
long ans = 0;
for (long x : f) {
ans = (ans + x) % mod;
}
return (int) ans;
}
}
13 changes: 13 additions & 0 deletions solution/3300-3399/3351.Sum of Good Subsequences/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def sumOfGoodSubsequences(self, nums: List[int]) -> int:
mod = 10**9 + 7
f = defaultdict(int)
g = defaultdict(int)
for x in nums:
f[x] += x
g[x] += 1
f[x] += f[x - 1] + g[x - 1] * x
g[x] += g[x - 1]
f[x] += f[x + 1] + g[x + 1] * x
g[x] += g[x + 1]
return sum(f.values()) % mod
Loading
Loading