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

g0001_0100.s0056_merge_intervals.complexity.md Maven / Gradle / Ivy

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

1. The program first sorts the `intervals` array based on the start values of each interval. Sorting takes O(n log n) time, where 'n' is the number of intervals in the array.

2. After sorting, it iterates through the sorted intervals exactly once.

3. In the loop, it performs constant time operations for each interval, such as comparisons and updates.

4. Therefore, the overall time complexity is dominated by the sorting step and is O(n log n), where 'n' is the number of intervals in the input array `intervals`.

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

1. The space complexity of the program is determined by the additional data structures used.

2. It uses a `List` called `list` to store the merged intervals. In the worst case, where there are no overlapping intervals, this list could contain all 'n' intervals. Therefore, the space complexity of this list is O(n).

3. The `current` array of size 2 is used to keep track of the current merged interval. This array requires constant space.

4. The `int[][]` array returned at the end of the program is created based on the size of the `list`. In the worst case, it would have 'n' subarrays, each with two elements.

5. Therefore, the overall space complexity is dominated by the `list` and is O(n).

In summary, the time complexity of the provided program is O(n log n) due to the sorting step, and the space complexity is O(n) due to the `list` used to store merged intervals. The program efficiently merges overlapping intervals and returns a new array of merged intervals.




© 2015 - 2024 Weber Informatics LLC | Privacy Policy