[LeetCode] Find the Number of Copy Arrays

3468. Find the Number of Copy Arrays

You are given an array original of length n and a 2D array bounds of length n x 2, where bounds[i] = [ui, vi].

You need to find the number of possible arrays copy of length n such that:

  1. (copy[i] - copy[i - 1]) == (original[i] - original[i - 1]) for 1 <= i <= n - 1.
  2. ui <= copy[i] <= vi for 0 <= i <= n - 1.

Return the number of such arrays.

c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int countArrays(vector<int>& original, vector<vector<int>>& bounds) {
long long l = bounds[0][0], r = bounds[0][1], n = original.size();
for(int i = 1; i < n and l <= r; i++) {
long long d = original[i] - original[i-1];
l += d, r += d;
long long le = bounds[i][0], ri = bounds[i][1];
l = max(l, le);
r = min(r, ri);
}
return max(0ll, r - l + 1);
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2025/03/02/PS/LeetCode/find-the-number-of-copy-arrays/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.

Related Issues not found

Please contact @SongHayoung to initialize the comment