Skip to content

Commit ba673e7

Browse files
authored
Improved task 15
1 parent 5d3cd42 commit ba673e7

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.24'
174174
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
175175
|-|-|-|-|-|-
176176
| 0082 |[Remove Duplicates from Sorted List II](src/main/java/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/Solution.java)| Medium | Two_Pointers, Linked_List | 0 | 100.00
177-
| 0015 |[3Sum](src/main/java/g0001_0100/s0015_3sum/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n^2)_Space_O(1) | 27 | 97.93
177+
| 0015 |[3Sum](src/main/java/g0001_0100/s0015_3sum/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n*log(n))_Space_O(n^2) | 27 | 97.93
178178

179179
#### Day 4 Two Pointers
180180

@@ -1443,7 +1443,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.24'
14431443
| 0977 |[Squares of a Sorted Array](src/main/java/g0901_1000/s0977_squares_of_a_sorted_array/Solution.java)| Easy | Array, Sorting, Two_Pointers | 1 | 100.00
14441444
| 0026 |[Remove Duplicates from Sorted Array](src/main/java/g0001_0100/s0026_remove_duplicates_from_sorted_array/Solution.java)| Easy | Top_Interview_Questions, Array, Two_Pointers | 1 | 98.56
14451445
| 0042 |[Trapping Rain Water](src/main/java/g0001_0100/s0042_trapping_rain_water/Solution.java)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1446-
| 0015 |[3Sum](src/main/java/g0001_0100/s0015_3sum/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n^2)_Space_O(1) | 27 | 97.93
1446+
| 0015 |[3Sum](src/main/java/g0001_0100/s0015_3sum/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n*log(n))_Space_O(n^2) | 27 | 97.93
14471447

14481448
#### Udemy Famous Algorithm
14491449

@@ -1695,7 +1695,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.24'
16951695
|-|-|-|-|-|-
16961696
| 0136 |[Single Number](src/main/java/g0101_0200/s0136_single_number/Solution.java)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 1 | 99.97
16971697
| 0169 |[Majority Element](src/main/java/g0101_0200/s0169_majority_element/Solution.java)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 1 | 100.00
1698-
| 0015 |[3Sum](src/main/java/g0001_0100/s0015_3sum/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n^2)_Space_O(1) | 27 | 97.93
1698+
| 0015 |[3Sum](src/main/java/g0001_0100/s0015_3sum/Solution.java)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n*log(n))_Space_O(n^2) | 27 | 97.93
16991699

17001700
#### Day 2 Array
17011701

src/main/java/g0001_0100/s0015_3sum/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Sorting #Two_Pointers
44
// #Data_Structure_II_Day_1_Array #Algorithm_II_Day_3_Two_Pointers #Udemy_Two_Pointers
5-
// #Big_O_Time_O(n^2)_Space_O(1) #2023_08_09_Time_27_ms_(97.93%)_Space_51.7_MB_(23.15%)
5+
// #Big_O_Time_O(n*log(n))_Space_O(n^2) #2023_08_09_Time_27_ms_(97.93%)_Space_51.7_MB_(23.15%)
66

77
import java.util.ArrayList;
88
import java.util.Arrays;
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
**Time Complexity (Big O Time):**
22

3-
The time complexity of this program is O(n^2), where "n" represents the number of elements in the `nums` array. Here's the breakdown:
3+
1. Sorting: The program sorts the input array `nums`. Sorting typically takes O(n * log(n)) time complexity, where 'n' is the length of the array.
44

5-
1. The program starts by sorting the `nums` array, which has a time complexity of O(n * log(n)), where "n" is the length of the array.
5+
2. Main Loop: The program uses nested loops. The outer loop runs from 0 to `len - 2`, where `len` is the length of the sorted array `nums`. The inner while loop has two pointers (`l` and `r`) that move towards each other. In the worst case, the inner loop can go through the entire array, so its time complexity is O(n).
66

7-
2. After sorting, the program uses nested loops:
8-
- The outer loop runs for `i` from 0 to `len - 2`, where "len" is the length of the array. This loop iterates through each element of the array once, so it has a time complexity of O(n).
7+
3. The code inside the inner loop contains constant-time operations, such as addition, comparison, and list creation.
98

10-
- The inner loop uses two pointers (`l` and `r`) and runs until `r` is greater than `l`. Within the inner loop, constant-time operations are performed, such as calculating the sum of three elements and adjusting the pointers.
9+
Overall, the dominant time complexity is determined by the sorting step, which is O(n * log(n)). The loop inside the sorting step has a complexity of O(n), but it doesn't dominate the overall complexity.
1110

12-
3. Inside the inner loop, there are additional while loops that skip duplicate elements. These while loops also perform constant-time operations.
13-
14-
Since the sorting step has a time complexity of O(n * log(n)), and the nested loops contribute O(n) iterations, the overall time complexity is dominated by the sorting step, resulting in O(n * log(n)).
11+
So, the total time complexity of the program is O(n * log(n)) due to the sorting step.
1512

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

18-
The space complexity of this program is O(1) because it uses a constant amount of extra space. The program creates a few integer variables (`l`, `r`, `sum`, and `len`), but the space used by these variables is independent of the input size. Additionally, the `result` list stores the output, but its space is not considered part of the space complexity analysis, as it's required to store the program's output.
15+
The space complexity of the program is determined by the space used for the output list `result`. In the worst case, when there are many unique triplets that sum to zero, the size of `result` can be significant.
16+
17+
1. The input array `nums` and the variables `len`, `l`, and `r` all use constant space.
18+
19+
2. The `result` list stores the triplets, and its size can be at most O(n^2) when there are many unique triplets.
20+
21+
3. Other variables and temporary lists used inside the loops use constant space.
22+
23+
So, the space complexity of the program is O(n^2) for the `result` list when considering the worst case.
1924

20-
Therefore, the overall space complexity is O(1).
25+
In summary, the provided program has a time complexity of O(n * log(n)) due to sorting and a space complexity of O(n^2) for the `result` list when considering the worst-case scenario.

0 commit comments

Comments
 (0)