g0001_0100.s0011_container_with_most_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-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
**Time Complexity (Big O Time):**
The time complexity of this program is O(n), where "n" represents the number of elements in the `height` array. Here's the breakdown:
1. The program uses a two-pointer approach with `left` and `right` pointers initialized at the beginning and end of the `height` array.
2. It iterates through the array using a `while` loop, and in each iteration, it performs constant-time operations:
- It calculates the area as the product of the heights at the current `left` and `right` pointers and the distance between them (constant time).
- It updates the `maxArea` variable by taking the maximum of the current `maxArea` and the calculated area (constant time).
- It moves either the `left` or `right` pointer toward the center of the array based on the condition `height[left] < height[right]` or `height[left] >= height[right]`.
Since the `while` loop runs until the `left` and `right` pointers meet in the middle, and each iteration performs constant-time operations, the overall time complexity is O(n), where "n" is the number of elements in the `height` array.
**Space Complexity (Big O Space):**
The space complexity of this program is O(1), which means it uses a constant amount of extra space that does not depend on the input size. The program uses only a few integer variables (`maxArea`, `left`, and `right`) to keep track of the maximum area and the positions of the two pointers. The space used by these variables remains constant regardless of the input size.
Therefore, the overall space complexity is O(1).
© 2015 - 2024 Weber Informatics LLC | Privacy Policy