Skip to content

3250. Find the Count of Monotonic Pairs I 👍

  • Time: $O(n \cdot \max(\texttt{nums}))$
  • Space: $O(n \cdot \max(\texttt{nums}))$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
 public:
  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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
  public int countOfPairs(int[] nums) {
    final int MOD = 1_000_000_007;
    final int MAX = 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][MAX + 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]) % MOD;
          ++prevNum;
        }
        dp[i][num] = ways;
      }
    }

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

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution:
  def countOfPairs(self, nums: list[int]) -> int:
    MOD = 1_000_000_007
    MAX = 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] * (MAX + 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]) % MOD
          prevNum += 1
        dp[i][num] = ways

    return sum(dp[n - 1]) % MOD