Maximum Value of Concatenated Binary Segments Problem
Description
LeetCode Problem 3897.
You are given two integer arrays nums1 and nums0, each of size n.
nums1[i]represents the number of'1's in thei^thsegment.nums0[i]represents the number of'0's in thei^thsegment.
For each index i, construct a binary segment consisting of:
nums1[i]occurrences of'1'followed bynums0[i]occurrences of'0'.
You may rearrange the order of these segments in any way. After rearranging, concatenate all segments to form a single binary string.
Return the maximum possible integer value of the concatenated binary string.
Since the result can be very large, return the answer modulo 10^9 + 7.
Example 1:
Input: nums1 = [1,2], nums0 = [1,0]
Output: 14
Explanation:
- At index 0, nums1[0] = 1 and nums0[0] = 1, so the segment formed is "10".
- At index 1, nums1[1] = 2 and nums0[1] = 0, so the segment formed is "11".
- Reordering the segments as "11" followed by "10" produces the binary string "1110".
- The binary number "1110" has value 14 which is the maximum possible value.
Example 2:
Input: nums1 = [3,1], nums0 = [0,3]
Output: 120
Explanation:
- At index 0, nums1[0] = 3 and nums0[0] = 0, so the segment formed is "111".
- At index 1, nums1[1] = 1 and nums0[1] = 3, so the segment formed is "1000".
- Reordering the segments as "111" followed by "1000" produces the binary string "1111000".
- The binary number "1111000" has value 120 which is the maximum possible value.
Constraints:
1 <= n == nums1.length == nums0.length <= 10^50 <= nums1[i], nums0[i] <= 10^4nums1[i] + nums0[i] > 0- The total sum of all elements in
nums1andnums0does not exceed 2 * 10^5.
Solution
Approach: Sorting + Greedy
Each segment looks like 1^x 0^y, with x = nums1[i] and y = nums0[i]. All concatenated strings have the same length, so the larger binary value is simply the lexicographically larger string: we want 1s as early as possible.
Comparing two segments A = 1^a 0^b and B = 1^c 0^d by looking at AB against BA gives this order:
- Segments with
y = 0(only1s) go first, the longer ones before the shorter ones, since they never put a0too early. - Then segments with
x > 0andy > 0: more leading1s first (xdescending), and for equalx, fewer0s first (yascending). - Segments with
x = 0(only0s) go last.
We don't need to build the string. Let m be its total length, and precompute 2^0 ... 2^(m-1) modulo 10^9 + 7. Walk through the sorted segments from the highest bit down: every 1 adds the weight of the current bit to the answer, and every 0 just moves the position down by one.
Complexity:
- Time complexity:
O(n log n + m), wherenis the number of segments andmis the total length of the string (at most2 * 10^5). - Space complexity:
O(n + m).
class Solution:
def maxValue(self, nums1: List[int], nums0: List[int]) -> int:
MOD = 10**9 + 7
pairs = list(zip(nums1, nums0))
b = sum(x + y for x, y in pairs)
def key(p: Tuple[int, int]) -> Tuple[int, int, int]:
x, y = p
if y == 0:
return (0, -x, 0)
if x > 0:
return (1, -x, y)
return (2, y, 0)
pairs.sort(key=key)
ans = 0
p = [1] * b
for i in range(1, b):
p[i] = p[i - 1] * 2 % MOD
b -= 1
for cnt1, cnt0 in pairs:
while cnt1:
ans = (ans + p[b]) % MOD
b -= 1
cnt1 -= 1
b -= cnt0
return ans