Number of Ways to Paint Sheets Problem
Description
LeetCode Problem 3802.
You are given an integer n representing the number of sheets.
You are also given an integer array limit of size m, where limit[i] is the maximum number of sheets that can be painted using color i.
You must paint all n sheets under the following conditions:
- Exactly two distinct colors are used.
- Each color must cover a single contiguous segment of sheets.
- The number of sheets painted with color
icannot exceedlimit[i].
Return an integer denoting the number of distinct ways to paint all sheets. Since the answer may be large, return it modulo 10^9 + 7.
Note: Two ways differ if at least one sheet is painted with a different color.
Example 1:
Input: n = 4, limit = [3,1,2]
Output: 6
Explanation:
For each ordered pair (i, j), where color i is used for the first segment and color j for the second segment (i != j), a split of x and 4 - x is valid if 1 <= x <= limit[i] and 1 <= 4 - x <= limit[j].
Valid pairs and counts are:
- (0, 1): x = 3
- (0, 2): x = 2, 3
- (1, 0): x = 1
- (2, 0): x = 1, 2
Therefore, there are 6 valid ways in total.
Example 2:
Input: n = 3, limit = [1,2]
Output: 2
Explanation:
For each ordered pair (i, j), where color i is used for the first segment and color j for the second segment (i != j), a split of x and 3 - x is valid if 1 <= x <= limit[i] and 1 <= 3 - x <= limit[j].
Valid pairs and counts are:
- (0, 1): x = 1
- (1, 0): x = 2
Hence, there are 2 valid ways in total.
Example 3:
Input: n = 3, limit = [2,2]
Output: 4
Explanation:
For each ordered pair (i, j), where color i is used for the first segment and color j for the second segment (i != j), a split of x and 3 - x is valid if 1 <= x <= limit[i] and 1 <= 3 - x <= limit[j].
Valid pairs and counts are:
- (0, 1): x = 1, 2
- (1, 0): x = 1, 2
Therefore, there are 4 valid ways in total.
Constraints:
2 <= n <= 10^92 <= m == limit.length <= 10^51 <= limit[i] <= 10^9
Solution
Approach: Sorting + Binary Search
Say the first segment uses color i with x sheets and the second uses color j with n - x sheets. We need 1 <= x <= limit[i] and 1 <= n - x <= limit[j].
Cap each limit at n - 1 and call the result a. The valid x run from max(1, n - a[j]) to a[i], and since a[j] <= n - 1 the lower end is n - a[j]. So an ordered pair (i, j) gives a[i] + a[j] - n + 1 ways when this is positive, and none otherwise.
Sort a and build suffix sums. For each i, binary search the first index whose value is at least n - a[i]. Every color from there on forms a valid pair with i, and their total is the suffix sum plus count * (a[i] - n + 1). If i pairs with itself (2 * a[i] >= n), subtract that one term. Add everything up modulo 10^9 + 7.
Complexity: Time O(m log m), space O(m), where m is the number of colors.
from bisect import bisect_left
class Solution:
def numberOfWays(self, n: int, limit: List[int]) -> int:
mod = 10**9 + 7
a = sorted(min(x, n - 1) for x in limit)
m = len(a)
suf = [0] * (m + 1)
for i in range(m - 1, -1, -1):
suf[i] = suf[i + 1] + a[i]
ans = 0
for x in a:
# partners y with x + y >= n; each pair contributes x + y - n + 1 splits
k = bisect_left(a, n - x)
t = suf[k] + (m - k) * (x - n + 1)
if 2 * x >= n:
t -= 2 * x - n + 1
ans += t
return ans % mod