返回介绍

solution / 1900-1999 / 1955.Count Number of Special Subsequences / README_EN

发布于 2024-06-17 01:03:12 字数 9525 浏览 0 评论 0 收藏 0

1955. Count Number of Special Subsequences

中文文档

Description

A sequence is special if it consists of a positive number of 0s, followed by a positive number of 1s, then a positive number of 2s.

  • For example, [0,1,2] and [0,0,1,1,1,2] are special.
  • In contrast, [2,1,0], [1], and [0,1,2,0] are not special.

Given an array nums (consisting of only integers 0, 1, and 2), return_ the number of different subsequences that are special_. Since the answer may be very large, return it modulo 109 + 7.

A subsequence of an array is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements. Two subsequences are different if the set of indices chosen are different.

 

Example 1:

Input: nums = [0,1,2,2]
Output: 3
Explanation: The special subsequences are bolded [0,1,2,2], [0,1,2,2], and [0,1,2,2].

Example 2:

Input: nums = [2,2,0,0]
Output: 0
Explanation: There are no special subsequences in [2,2,0,0].

Example 3:

Input: nums = [0,1,2,0,1,2]
Output: 7
Explanation: The special subsequences are bolded:
- [0,1,2,0,1,2]
- [0,1,2,0,1,2]
- [0,1,2,0,1,2]
- [0,1,2,0,1,2]
- [0,1,2,0,1,2]
- [0,1,2,0,1,2]
- [0,1,2,0,1,2]

 

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 2

Solutions

Solution 1

class Solution:
  def countSpecialSubsequences(self, nums: List[int]) -> int:
    mod = 10**9 + 7
    n = len(nums)
    f = [[0] * 3 for _ in range(n)]
    f[0][0] = nums[0] == 0
    for i in range(1, n):
      if nums[i] == 0:
        f[i][0] = (2 * f[i - 1][0] + 1) % mod
        f[i][1] = f[i - 1][1]
        f[i][2] = f[i - 1][2]
      elif nums[i] == 1:
        f[i][0] = f[i - 1][0]
        f[i][1] = (f[i - 1][0] + 2 * f[i - 1][1]) % mod
        f[i][2] = f[i - 1][2]
      else:
        f[i][0] = f[i - 1][0]
        f[i][1] = f[i - 1][1]
        f[i][2] = (f[i - 1][1] + 2 * f[i - 1][2]) % mod
    return f[n - 1][2]
class Solution {
  public int countSpecialSubsequences(int[] nums) {
    final int mod = (int) 1e9 + 7;
    int n = nums.length;
    int[][] f = new int[n][3];
    f[0][0] = nums[0] == 0 ? 1 : 0;
    for (int i = 1; i < n; ++i) {
      if (nums[i] == 0) {
        f[i][0] = (2 * f[i - 1][0] % mod + 1) % mod;
        f[i][1] = f[i - 1][1];
        f[i][2] = f[i - 1][2];
      } else if (nums[i] == 1) {
        f[i][0] = f[i - 1][0];
        f[i][1] = (f[i - 1][0] + 2 * f[i - 1][1] % mod) % mod;
        f[i][2] = f[i - 1][2];
      } else {
        f[i][0] = f[i - 1][0];
        f[i][1] = f[i - 1][1];
        f[i][2] = (f[i - 1][1] + 2 * f[i - 1][2] % mod) % mod;
      }
    }
    return f[n - 1][2];
  }
}
class Solution {
public:
  int countSpecialSubsequences(vector<int>& nums) {
    const int mod = 1e9 + 7;
    int n = nums.size();
    int f[n][3];
    memset(f, 0, sizeof(f));
    f[0][0] = nums[0] == 0;
    for (int i = 1; i < n; ++i) {
      if (nums[i] == 0) {
        f[i][0] = (2 * f[i - 1][0] % mod + 1) % mod;
        f[i][1] = f[i - 1][1];
        f[i][2] = f[i - 1][2];
      } else if (nums[i] == 1) {
        f[i][0] = f[i - 1][0];
        f[i][1] = (f[i - 1][0] + 2 * f[i - 1][1] % mod) % mod;
        f[i][2] = f[i - 1][2];
      } else {
        f[i][0] = f[i - 1][0];
        f[i][1] = f[i - 1][1];
        f[i][2] = (f[i - 1][1] + 2 * f[i - 1][2] % mod) % mod;
      }
    }
    return f[n - 1][2];
  }
};
func countSpecialSubsequences(nums []int) int {
  const mod = 1e9 + 7
  n := len(nums)
  f := make([][3]int, n)
  if nums[0] == 0 {
    f[0][0] = 1
  }
  for i := 1; i < n; i++ {
    if nums[i] == 0 {
      f[i][0] = (2*f[i-1][0] + 1) % mod
      f[i][1] = f[i-1][1]
      f[i][2] = f[i-1][2]
    } else if nums[i] == 1 {
      f[i][0] = f[i-1][0]
      f[i][1] = (f[i-1][0] + 2*f[i-1][1]) % mod
      f[i][2] = f[i-1][2]
    } else {
      f[i][0] = f[i-1][0]
      f[i][1] = f[i-1][1]
      f[i][2] = (f[i-1][1] + 2*f[i-1][2]) % mod
    }
  }
  return f[n-1][2]
}
function countSpecialSubsequences(nums: number[]): number {
  const mod = 1e9 + 7;
  const n = nums.length;
  const f: number[][] = Array(n)
    .fill(0)
    .map(() => Array(3).fill(0));
  f[0][0] = nums[0] === 0 ? 1 : 0;
  for (let i = 1; i < n; ++i) {
    if (nums[i] === 0) {
      f[i][0] = (((2 * f[i - 1][0]) % mod) + 1) % mod;
      f[i][1] = f[i - 1][1];
      f[i][2] = f[i - 1][2];
    } else if (nums[i] === 1) {
      f[i][0] = f[i - 1][0];
      f[i][1] = (f[i - 1][0] + ((2 * f[i - 1][1]) % mod)) % mod;
      f[i][2] = f[i - 1][2];
    } else {
      f[i][0] = f[i - 1][0];
      f[i][1] = f[i - 1][1];
      f[i][2] = (f[i - 1][1] + ((2 * f[i - 1][2]) % mod)) % mod;
    }
  }
  return f[n - 1][2];
}

Solution 2

class Solution:
  def countSpecialSubsequences(self, nums: List[int]) -> int:
    mod = 10**9 + 7
    n = len(nums)
    f = [0] * 3
    f[0] = nums[0] == 0
    for i in range(1, n):
      if nums[i] == 0:
        f[0] = (2 * f[0] + 1) % mod
      elif nums[i] == 1:
        f[1] = (f[0] + 2 * f[1]) % mod
      else:
        f[2] = (f[1] + 2 * f[2]) % mod
    return f[2]
class Solution {
  public int countSpecialSubsequences(int[] nums) {
    final int mod = (int) 1e9 + 7;
    int n = nums.length;
    int[] f = new int[3];
    f[0] = nums[0] == 0 ? 1 : 0;
    for (int i = 1; i < n; ++i) {
      if (nums[i] == 0) {
        f[0] = (2 * f[0] % mod + 1) % mod;
      } else if (nums[i] == 1) {
        f[1] = (f[0] + 2 * f[1] % mod) % mod;
      } else {
        f[2] = (f[1] + 2 * f[2] % mod) % mod;
      }
    }
    return f[2];
  }
}
class Solution {
public:
  int countSpecialSubsequences(vector<int>& nums) {
    const int mod = 1e9 + 7;
    int n = nums.size();
    int f[3]{0};
    f[0] = nums[0] == 0;
    for (int i = 1; i < n; ++i) {
      if (nums[i] == 0) {
        f[0] = (2 * f[0] % mod + 1) % mod;
      } else if (nums[i] == 1) {
        f[1] = (f[0] + 2 * f[1] % mod) % mod;
      } else {
        f[2] = (f[1] + 2 * f[2] % mod) % mod;
      }
    }
    return f[2];
  }
};
func countSpecialSubsequences(nums []int) int {
  const mod = 1e9 + 7
  n := len(nums)
  f := [3]int{}
  if nums[0] == 0 {
    f[0] = 1
  }
  for i := 1; i < n; i++ {
    if nums[i] == 0 {
      f[0] = (2*f[0] + 1) % mod
    } else if nums[i] == 1 {
      f[1] = (f[0] + 2*f[1]) % mod
    } else {
      f[2] = (f[1] + 2*f[2]) % mod
    }
  }
  return f[2]
}
function countSpecialSubsequences(nums: number[]): number {
  const mod = 1e9 + 7;
  const n = nums.length;
  const f: number[] = [0, 0, 0];
  f[0] = nums[0] === 0 ? 1 : 0;
  for (let i = 1; i < n; ++i) {
    if (nums[i] === 0) {
      f[0] = (((2 * f[0]) % mod) + 1) % mod;
      f[1] = f[1];
      f[2] = f[2];
    } else if (nums[i] === 1) {
      f[0] = f[0];
      f[1] = (f[0] + ((2 * f[1]) % mod)) % mod;
      f[2] = f[2];
    } else {
      f[0] = f[0];
      f[1] = f[1];
      f[2] = (f[1] + ((2 * f[2]) % mod)) % mod;
    }
  }
  return f[2];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文