g0001_0100.s0042_trapping_rain_water.complexity.md Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
**Time Complexity (Big O Time):**
1. The program uses two pointers, `l` (left) and `r` (right), that initially start at the leftmost and rightmost elements of the input `height` array. The pointers move towards each other until they meet.
2. 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]` and `height[r]`).
3. The loop runs until the `l` pointer is less than the `r` pointer, and in each iteration, it either increments `l` or decrements `r`. Since the pointers move towards each other and never move backward, the loop executes at most `n` times, where `n` is the number of elements in the input `height` array.
4. 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.
5. Therefore, the time complexity of the program is O(n), where `n` is the number of elements in the input array `height`.
**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`.