DoubleHelix SPOJ
Pattern: Two pointers
Walk two increasing sequences side by side. Between shared values, keep the better of the two stretches, and cross over at every shared value.
The problem
You are given two strictly increasing sequences of whole numbers. A path walks along one sequence from its start to its
end, adding the values it passes. Wherever both sequences contain the same value, the path may switch to the other
sequence at that value (the shared value is counted once). Return the largest possible total. For example,
[-5, 100, 1000, 1005] and [-12, 1000, 1001] give 2100: -5 + 100 + 1000 + 1005.
This is SPOJ's ANARC05B ("The Double HeLiX"). On SPOJ you write a whole program that reads several test cases until a line
with 0; this guide shows the core function maxPathSum, which you call once per test case. Use the link above for the
exact input format.
Think first
- Between two shared values, the path stays on one sequence. Which one should it choose?
- Do the choices made in one stretch affect the next stretch?
- How do you find the shared values in both sequences without comparing every pair?
Hints
Brute force, and why it fails
Try every combination of switch / don't switch at each shared value: 2^k paths for k shared values, which explodes
quickly. This guide uses that exhaustive search only to check its tests on small inputs.
The key insight
Shared values split the problem into independent stretches, and in each stretch the best choice is simply the larger sum. A single merge walk finds the stretches and adds up the best total.
Dry run
| sumA | 0 |
|---|---|
| sumB | 0 |
| total | 0 |
a = [-5, 100, 1000, 1005] and b = [-12, 1000, 1001]. Walk both, keeping the sum of each side since the last shared value.
The plan
i = j = 0, sumA = sumB = total = 0
while i < len(a) and j < len(b):
if a[i] < b[j]: sumA = sumA + a[i]; i = i + 1
else if a[i] > b[j]: sumB = sumB + b[j]; j = j + 1
else: # shared value
total = total + max(sumA, sumB) + a[i]
sumA = sumB = 0
i = i + 1; j = j + 1
add the rest of a to sumA and the rest of b to sumB
return total + max(sumA, sumB)
Complexity
- Time
O(n + m)- Space
O(1)
One merge walk over both sequences, with a few running sums.
Edge cases
- No shared values: the answer is the larger of the two whole-sequence sums.
- Identical sequences: every value is shared, and the answer is the sum of one sequence.
- Negative values: a stretch may be best on the side whose sum is less negative.
I've built my logic, show the code
Java
class Solution {
public long maxPathSum(int[] a, int[] b) {
int i = 0;
int j = 0;
long sumA = 0; // sum of a since the last shared value
long sumB = 0; // sum of b since the last shared value
long total = 0;
while (i < a.length && j < b.length) {
if (a[i] < b[j]) {
sumA += a[i++];
} else if (a[i] > b[j]) {
sumB += b[j++];
} else { // a shared value: keep the better stretch, then cross it
total += Math.max(sumA, sumB) + a[i];
sumA = 0;
sumB = 0;
i++;
j++;
}
}
while (i < a.length) {
sumA += a[i++];
}
while (j < b.length) {
sumB += b[j++];
}
return total + Math.max(sumA, sumB);
}
}C++
class Solution {
public:
long long maxPathSum(vector<int>& a, vector<int>& b) {
size_t i = 0;
size_t j = 0;
long long sumA = 0; // sum of a since the last shared value
long long sumB = 0; // sum of b since the last shared value
long long total = 0;
while (i < a.size() && j < b.size()) {
if (a[i] < b[j]) {
sumA += a[i++];
} else if (a[i] > b[j]) {
sumB += b[j++];
} else { // a shared value: keep the better stretch, then cross it
total += max(sumA, sumB) + a[i];
sumA = 0;
sumB = 0;
i++;
j++;
}
}
while (i < a.size()) {
sumA += a[i++];
}
while (j < b.size()) {
sumB += b[j++];
}
return total + max(sumA, sumB);
}
};Python
class Solution:
def maxPathSum(self, a, b):
i = j = 0
sum_a = sum_b = 0 # sums since the last shared value
total = 0
while i < len(a) and j < len(b):
if a[i] < b[j]:
sum_a += a[i]
i += 1
elif a[i] > b[j]:
sum_b += b[j]
j += 1
else: # a shared value: keep the better stretch, then cross it
total += max(sum_a, sum_b) + a[i]
sum_a = sum_b = 0
i += 1
j += 1
sum_a += sum(a[i:])
sum_b += sum(b[j:])
return total + max(sum_a, sum_b)Common mistakes
- Adding a shared value twice (once for each side).
- Forgetting the final stretch after the last shared value.
- Choosing a side value by value instead of stretch by stretch.