Time Complexity (Big O Time):
-
The program uses two pointers,
l
(left) andr
(right), that initially start at the leftmost and rightmost elements of the inputheight
array. The pointers move towards each other until they meet. -
In each step of the loop, the program computes the trapped rainwater at the current position based on the lower wall (the smaller of the heights at
height[l]
andheight[r]
). -
The loop runs until the
l
pointer is less than ther
pointer, and in each iteration, it either incrementsl
or decrementsr
. Since the pointers move towards each other and never move backward, the loop executes at mostn
times, wheren
is the number of elements in the inputheight
array. -
The operations within the loop, including comparisons, arithmetic, and mathematical operations (e.g.,
Math.max
), are all constant time operations, so they do not significantly impact the overall time complexity. -
Therefore, the time complexity of the program is O(n), where
n
is the number of elements in the input arrayheight
.
Space Complexity (Big O Space):
The space complexity of the program is O(1), which means it uses a constant amount of additional space regardless of the size of the input array height
. The program only uses a few integer variables (l
, r
, res
, lowerWall
, lVal
, and rVal
) and does not use any additional data structures or memory that scales with the input size.
In summary, the time complexity of the provided program is O(n), and the space complexity is O(1), where n
is the number of elements in the input array height
.