Can anyone please explain how to solve this question. The link I mentioned already has a solution but the most efficient on takes O(N) time and O(N) space, I need a solution that takes O(N) time and O(1) space.
In this problem if we find the highest bar on left and right side of the current bar, than the amount of water current bar can hold will be ={min(left_max,right_max)-height[cur]}
@arpit728 The amount of water that will be on top of any index is determined by the left max and right max for that index. The left max and right max acts as container and the amount of water on top of any index = min(H[left_max], H[right_max]) - H[index].
Now lets divide the array in two parts using the max H… let it be at indMaxH. For all index < indMaxH … the left “wall” and for all index > indMaxh the right wall will determine the water. I am not posting any code. Think over it and you can easily write the code with O(n) time and O(1) space complexities.