All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0201_0300.s0239_sliding_window_maximum.complexity.md Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
**Time Complexity (Big O Time):**

The time complexity of this program is primarily determined by the two nested loops:

1. The outer `while` loop runs from `j = 0` to `j = n - 1`, where `n` is the length of the input array `nums`. Therefore, it has a time complexity of O(n).

2. Inside the outer loop, there is an inner `while` loop that removes elements from the back of the deque (`dq`). In the worst case, this inner loop can run up to `k` times, where `k` is the size of the sliding window. So, the inner `while` loop contributes O(k) to the time complexity.

Overall, the time complexity of the program is O(n * k) in the worst case, where `n` is the length of the input array, and `k` is the size of the sliding window. In practice, when `k` is much smaller than `n`, the algorithm approaches O(n).

**Space Complexity (Big O Space):**

1. The program uses an integer array `res` to store the results, which has a length of `n - k + 1`. Therefore, the space complexity of the `res` array is O(n).

2. The program uses a `LinkedList` named `dq` as a deque to store elements. In the worst case, the deque can store up to `k` elements at any given time. Therefore, the space complexity of the deque is O(k).

3. The program uses a few integer variables (`x`, `i`, `j`) that consume a constant amount of space regardless of the input size. These variables do not depend on the length of the input array.

Overall, the space complexity of the program is O(n + k), where `n` is the length of the input array, and `k` is the size of the sliding window. In practice, when `k` is much smaller than `n`, the space complexity approaches O(n).

In summary, the provided program has a time complexity of O(n * k) and a space complexity of O(n + k), but in practice, it can be more efficient when `k` is small compared to `n`.




© 2015 - 2024 Weber Informatics LLC | Privacy Policy