g0001_0100.s0084_largest_rectangle_in_histogram.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):**
The program uses a divide-and-conquer approach to calculate the largest rectangle area. Here's the analysis of the time complexity:
1. The `largestRectangleArea` function is called initially with the entire array of heights, so the initial function call has a time complexity of O(n), where 'n' is the length of the `heights` array.
2. The `largestArea` function is called recursively to calculate the largest rectangle area. In each recursive call, it divides the problem into smaller subproblems. The subproblem is divided into two parts or processed linearly based on whether the array is sorted.
3. In the worst case, the `largestArea` function may make recursive calls on both the left and right halves of the input array, effectively dividing it in half each time. This results in a recursive call tree with a height of log₂(n).
4. Within each recursive call, there are loops and conditional checks. The loop that finds the minimum value in a subarray has a time complexity of O(n), where 'n' is the size of the subarray. In the worst case, this loop can run for each level of recursion.
5. Overall, the time complexity can be represented as O(n log n) in the worst case. This is because at each level of recursion, you perform a linear scan of the array, and there are log₂(n) levels of recursion.
**Space Complexity (Big O Space):**
The space complexity of the program is primarily determined by the call stack due to recursion and some additional variables. Here's the analysis of space complexity:
1. The primary space usage is due to the recursive call stack. In the worst case, the maximum depth of the call stack can be log₂(n), where 'n' is the length of the `heights` array. Therefore, the space complexity due to the call stack is O(log n).
2. The program uses a few additional variables for loop indices and temporary values, but their space usage is constant and does not depend on the size of the input. Therefore, their contribution to space complexity is negligible.
3. Overall, the dominant factor in space complexity is the recursive call stack, so the space complexity is O(log n) in the worst case.
In summary, the time complexity of the program is O(n log n) in the worst case, and the space complexity is O(log n) due to the recursive call stack.