g0001_0100.s0053_maximum_subarray.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 a single loop to iterate through the elements of the `nums` array. Let's assume there are 'n' elements in the array.
2. In each iteration of the loop, the program performs constant time operations such as addition, comparison, and calls to `Math.max`.
3. Therefore, the time complexity of the program is O(n), where 'n' is the number of elements in the input array `nums`.
**Space Complexity (Big O Space):**
1. 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.
2. The program uses a few integer variables (`maxi` and `sum`) to keep track of the maximum subarray sum and the current subarray sum, respectively. These variables require constant space.
3. The space complexity does not depend on the size of the input array `nums`.
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 `nums`. The program efficiently finds the maximum subarray sum using a single pass through the array.