Let's say I have an array A = {a1, a2, ..., an}
Suppose A is divided in blocks of size b
What I'm trying to do is:
Let MinUntilHere and MinStartingHere be 2 arrays of size n, where:
- MinUntilHere[i] = minimum from the beginning of i's block until i
- MinStartingHere[i] = minimum from i until the end of i's block
I want to fill this 2 arrays in a single transversal in the array... I was trying the following pseudocode:
int fill(int pos = 0){
if(pos >= n) return INF
if(pos is the start index of a block){ //i % b == 0
MinUntilHere[pos] = A[pos]
MinStartingHere[pos] = min(A[pos], fill(i + 1))
}
else{
MinUntilHere[pos] = min(A[pos], A[pos - 1])
MinStartingHere[pos] = min(A[pos], fill(i + 1))
return MinStartingHere[pos]
}
}
but as you can see, it's not complete, since it can't detect when each block is over and some returns are missing. How can I make a function that go through the array only once and compute my answers?
Example:
Let A = {2,1,3,6,5,4} and b = 2, the arrays must be filled like this:
MinUntilHere = {2,1,3,3,5,4}
MinStartingHere = {1,1,3,6,4,4}
MinUntilHere[0] = 2 cause from the beginning of 0's block until index 0, the minimum is 2.
MinUntilHere[1] = 1 cause from the beginning of 1's block until index 1, the minimum is min(1,2) = 1
End of Example