3251. Find the Count of Monotonic Pairs II LeetCode Solution

In this guide, you will get 3251. Find the Count of Monotonic Pairs II LeetCode Solution with the best time and space complexity. The solution to Find the Count of Monotonic Pairs II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find the Count of Monotonic Pairs II solution in C++
  4. Find the Count of Monotonic Pairs II solution in Java
  5. Find the Count of Monotonic Pairs II solution in Python
  6. Additional Resources
3251. Find the Count of Monotonic Pairs II LeetCode Solution image

Problem Statement of Find the Count of Monotonic Pairs II

You are given an array of positive integers nums of length n.
We call a pair of non-negative integer arrays (arr1, arr2) monotonic if:

The lengths of both arrays are n.
arr1 is monotonically non-decreasing, in other words, arr1[0] <= arr1[1] <= … = arr2[1] >= … >= arr2[n – 1].
arr1[i] + arr2[i] == nums[i] for all 0 <= i <= n – 1.

Return the count of monotonic pairs.
Since the answer may be very large, return it modulo 109 + 7.

Example 1:

Input: nums = [2,3,2]
Output: 4
Explanation:
The good pairs are:

([0, 1, 1], [2, 2, 1])
([0, 1, 2], [2, 2, 0])
([0, 2, 2], [2, 1, 0])
([1, 2, 2], [1, 1, 0])

Example 2:

Input: nums = [5,5,5,5]
Output: 126

Constraints:

1 <= n == nums.length <= 2000
1 <= nums[i] <= 1000

See also  443. String Compression LeetCode Solution

Complexity Analysis

  • Time Complexity: O(n \cdot \max(\texttt{nums}))
  • Space Complexity: O(n \cdot \max(\texttt{nums}))

3251. Find the Count of Monotonic Pairs II LeetCode Solution in C++

class Solution {
 public:
  // Same as 3250. Find the Count of Monotonic Pairs I
  int countOfPairs(vector<int>& nums) {
    constexpr int kMod = 1'000'000'007;
    constexpr int kMax = 1000;
    const int n = nums.size();
    int ans = 0;
    // dp[i][num] := the number of valid ways to fill the arrays up to index i
    // with arr1[i] = num
    vector<vector<int>> dp(n, vector<int>(kMax + 1));

    for (int num = 0; num <= nums[0]; ++num)
      dp[0][num] = 1;

    for (int i = 1; i < n; ++i) {
      int ways = 0;
      int prevNum = 0;
      // To satisfy arr1, prevNum <= num.
      // To satisfy arr2, nums[i - 1] - prevNum >= nums[i] - num.
      //               => prevNum <= min(num, num - (nums[i] - nums[i - 1])).
      // As we move from `num` to `num + 1`, the range of valid `prevNum` values
      // becomes prevNum <= min(num + 1, num + 1 - (nums[i] - nums[i - 1])).
      // Since the range of `prevNum` can only increase by at most 1, there's
      // no need to iterate through all possible values of `prevNum`. We can
      // simply increment `prevNum` by 1 if it meets the condition.
      for (int num = 0; num <= nums[i]; ++num) {
        if (prevNum <= min(num, num - (nums[i] - nums[i - 1]))) {
          ways = (ways + dp[i - 1][prevNum]) % kMod;
          ++prevNum;
        }
        dp[i][num] = ways;
      }
    }

    for (int i = 0; i <= kMax; ++i)
      ans = (ans + dp[n - 1][i]) % kMod;

    return ans;
  }
};
/* code provided by PROGIEZ */

3251. Find the Count of Monotonic Pairs II LeetCode Solution in Java

class Solution {
  // Same as 3250. Find the Count of Monotonic Pairs I
  public int countOfPairs(int[] nums) {
    final int kMod = 1_000_000_007;
    final int kMax = 1000;
    final int n = nums.length;
    int ans = 0;
    // dp[i][num] := the number of valid ways to fill the arrays up to index i
    // with arr1[i] = num
    int[][] dp = new int[n][kMax + 1];

    for (int num = 0; num <= nums[0]; ++num)
      dp[0][num] = 1;

    for (int i = 1; i < n; ++i) {
      int ways = 0;
      int prevNum = 0;
      // To satisfy arr1, prevNum <= num.
      // To satisfy arr2, nums[i - 1] - prevNum >= nums[i] - num.
      //               => prevNum <= min(num, num - (nums[i] - nums[i - 1])).
      // As we move from `num` to `num + 1`, the range of valid `prevNum` values
      // becomes prevNum <= min(num + 1, num + 1 - (nums[i] - nums[i - 1])).
      // Since the range of `prevNum` can only increase by at most 1, there's
      // no need to iterate through all possible values of `prevNum`. We can
      // simply increment `prevNum` by 1 if it meets the condition.
      for (int num = 0; num <= nums[i]; ++num) {
        if (prevNum <= Math.min(num, num - (nums[i] - nums[i - 1]))) {
          ways = (ways + dp[i - 1][prevNum]) % kMod;
          ++prevNum;
        }
        dp[i][num] = ways;
      }
    }

    for (int i = 0; i <= kMax; ++i)
      ans = (ans + dp[n - 1][i]) % kMod;

    return ans;
  }
}
// code provided by PROGIEZ

3251. Find the Count of Monotonic Pairs II LeetCode Solution in Python

class Solution:
  # Same as 3250. Find the Count of Monotonic Pairs I
  def countOfPairs(self, nums: list[int]) -> int:
    kMod = 1_000_000_007
    kMax = 1000
    n = len(nums)
    # dp[i][num] := the number of valid ways to fill the arrays up to index i
    # with arr1[i] = num
    dp = [[0] * (kMax + 1) for _ in range(n)]

    for num in range(nums[0] + 1):
      dp[0][num] = 1

    for i in range(1, n):
      ways = 0
      prevNum = 0
      # To satisfy arr1, prevNum <= num.
      # To satisfy arr2, nums[i - 1] - prevNum >= nums[i] - num.
      #               => prevNum <= min(num, num - (nums[i] - nums[i - 1])).
      # As we move from `num` to `num + 1`, the range of valid `prevNum` values
      # becomes prevNum <= min(num + 1, num + 1 - (nums[i] - nums[i - 1])).
      # Since the range of `prevNum` can only increase by at most 1, there's
      # no need to iterate through all possible values of `prevNum`. We can
      # simply increment `prevNum` by 1 if it meets the condition.
      for num in range(nums[i] + 1):
        if prevNum <= min(num, num - (nums[i] - nums[i - 1])):
          ways = (ways + dp[i - 1][prevNum]) % kMod
          prevNum += 1
        dp[i][num] = ways

    return sum(dp[n - 1]) % kMod
# code by PROGIEZ

Additional Resources

See also  577. Employee Bonus LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.