Skip to content

Commit 36317d8

Browse files
authored
Added complexity tags 169-226
1 parent 03ecc39 commit 36317d8

File tree

22 files changed

+252
-12
lines changed

22 files changed

+252
-12
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ repositories {
1313

1414
dependencies {
1515
testImplementation 'org.junit.jupiter:junit-jupiter:[5.10.0,)'
16-
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
1716
testImplementation 'org.hamcrest:hamcrest-core:[2.2,)'
1817
testImplementation 'org.zapodot:embedded-db-junit-jupiter:[2.1.1,)'
18+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
1919
}
2020

2121
test {

src.save/main/java/g0101_0200/s0169_majority_element/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Sorting #Counting
44
// #Divide_and_Conquer #Data_Structure_II_Day_1_Array #Udemy_Famous_Algorithm
5-
// #2022_06_25_Time_1_ms_(100.00%)_Space_45.5_MB_(97.51%)
5+
// #Big_O_Time_O(n)_Space_O(1) #2022_06_25_Time_1_ms_(100.00%)_Space_45.5_MB_(97.51%)
66

77
public class Solution {
88
public int majorityElement(int[] arr) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The time complexity of this program is O(n), where n is the length of the input array `arr`. Here's why:
4+
5+
1. The first loop iterates through the entire array once (`for (int i = 1; i < arr.length; i++)`).
6+
- Inside this loop, there are constant-time operations (assignments and comparisons) for each element of the array.
7+
- Therefore, this part has a time complexity of O(n).
8+
9+
2. The second loop also iterates through the entire array once (`for (int j : arr)`).
10+
- Inside this loop, there are constant-time operations (comparisons) for each element of the array.
11+
- Therefore, this part also has a time complexity of O(n).
12+
13+
In summary, both loops together contribute to the linear time complexity O(n).
14+
15+
**Space Complexity (Big O Space):**
16+
17+
The space complexity of this program is O(1), which means it uses a constant amount of extra space. Regardless of the size of the input array, the program only uses a fixed number of variables (`count` and `majority`) to keep track of the majority element.
18+
19+
In summary, the time complexity is O(n), where n is the length of the input array, and the space complexity is O(1), indicating a constant amount of additional memory usage.

src.save/main/java/g0101_0200/s0189_rotate_array/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package g0101_0200.s0189_rotate_array;
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Math #Two_Pointers
4-
// #Algorithm_I_Day_2_Two_Pointers #Udemy_Arrays
4+
// #Algorithm_I_Day_2_Two_Pointers #Udemy_Arrays #Big_O_Time_O(n)_Space_O(1)
55
// #2022_06_27_Time_0_ms_(100.00%)_Space_58_MB_(96.22%)
66

77
public class Solution {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The time complexity of this program is O(n), where n is the length of the input array `nums`. Here's why:
4+
5+
1. The `reverse` method has a while loop that iterates from `l` to `r`, where `l` and `r` are indices within the array. The number of iterations in this loop is proportional to the size of the subarray being reversed.
6+
7+
2. In the `rotate` method, there are three calls to the `reverse` method:
8+
- The first `reverse` call reverses the subarray from index 0 to `t - 1`, where `t` is calculated as `n - (k % n)`.
9+
- The second `reverse` call reverses the subarray from index `t` to `n - 1`.
10+
- The third `reverse` call reverses the entire array from index 0 to `n - 1`.
11+
12+
All three `reverse` calls have time complexity proportional to the size of the subarray being reversed, and they operate on non-overlapping subarrays. Therefore, the overall time complexity is O(n).
13+
14+
**Space Complexity (Big O Space):**
15+
16+
The space complexity of this program is O(1), indicating that it uses a constant amount of additional memory regardless of the size of the input array. The program performs the rotation in-place, and the space used for variables and computations remains constant.
17+
18+
In summary, the time complexity is O(n), where n is the length of the input array, and the space complexity is O(1), indicating constant space usage.

src.save/main/java/g0101_0200/s0198_house_robber/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
44
// #Algorithm_I_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_3
5-
// #Level_2_Day_12_Dynamic_Programming #Udemy_Dynamic_Programming
5+
// #Level_2_Day_12_Dynamic_Programming #Udemy_Dynamic_Programming #Big_O_Time_O(n)_Space_O(n)
66
// #2022_06_28_Time_0_ms_(100.00%)_Space_39.9_MB_(85.30%)
77

88
public class Solution {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The time complexity of this program is O(n), where n is the length of the input array `nums`. Here's why:
4+
5+
1. The program iterates through the `nums` array once in a loop that runs from 2 to `n - 1`, where n is the length of the array. This loop computes the maximum profit that can be obtained up to each house.
6+
7+
2. In each iteration of the loop, the program calculates `profit[i]` based on the previous two values: `profit[i - 1]` and `profit[i - 2]`. These calculations are done in constant time for each house.
8+
9+
3. Therefore, the total number of operations in the loop is proportional to the length of the `nums` array, resulting in a time complexity of O(n).
10+
11+
**Space Complexity (Big O Space):**
12+
13+
The space complexity of this program is O(n), where n is the length of the input array `nums`. Here's why:
14+
15+
1. The program creates an integer array `profit` of the same length as `nums` to store the maximum profit at each house. Therefore, the space required for the `profit` array is O(n).
16+
17+
2. In addition to the `profit` array, the program uses a constant amount of space for other variables and calculations. These additional space requirements are independent of the size of the input and can be considered O(1).
18+
19+
3. Overall, the dominant factor in space complexity is the `profit` array, resulting in a space complexity of O(n).
20+
21+
In summary, the time complexity of the program is O(n) because it iterates through the input array once, and the space complexity is also O(n) due to the `profit` array used to store intermediate results.

src.save/main/java/g0101_0200/s0200_number_of_islands/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// #Breadth_First_Search #Matrix #Union_Find
55
// #Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search
66
// #Graph_Theory_I_Day_1_Matrix_Related_Problems #Level_1_Day_9_Graph/BFS/DFS #Udemy_Graph
7-
// #2022_06_28_Time_3_ms_(97.76%)_Space_57.5_MB_(41.23%)
7+
// #Big_O_Time_O(M*N)_Space_O(M*N) #2022_06_28_Time_3_ms_(97.76%)_Space_57.5_MB_(41.23%)
88

99
public class Solution {
1010
public int numIslands(char[][] grid) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The time complexity of this program is O(M * N), where M is the number of rows and N is the number of columns in the grid. Here's why:
4+
5+
1. The program uses a nested loop to iterate through each cell of the 2D grid, where the outer loop runs from 0 to the number of rows (M) and the inner loop runs from 0 to the number of columns (N). This results in a total of M * N iterations.
6+
7+
2. For each cell, the program performs a depth-first search (DFS) operation, which may recursively visit adjacent cells if they are part of the same island. The DFS operation has constant time complexity for each cell since it visits each cell once.
8+
9+
3. Therefore, the overall time complexity is O(M * N) since the program performs DFS for each cell in the grid.
10+
11+
**Space Complexity (Big O Space):**
12+
13+
The space complexity of this program is O(1) in terms of auxiliary space (additional data structures) and O(M * N) in terms of stack space due to recursive function calls. Here's why:
14+
15+
1. The program uses a constant amount of auxiliary space for variables like `islands`, `x`, and `y`. These variables do not depend on the size of the input grid and can be considered O(1).
16+
17+
2. The recursive DFS calls result in stack space usage proportional to the depth of the recursion, which is determined by the size of the island being explored. In the worst case, if the entire grid is one large island, the stack space can be as large as M * N.
18+
19+
3. Overall, the space complexity is dominated by the stack space used during DFS, and it can be represented as O(M * N) in the worst case.
20+
21+
In summary, the time complexity is O(M * N) because the program iterates through each cell in the grid, and the space complexity is O(1) for auxiliary space and O(M * N) for stack space used during recursive DFS calls.

src.save/main/java/g0201_0300/s0206_reverse_linked_list/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Linked_List #Recursion
44
// #Data_Structure_I_Day_8_Linked_List #Algorithm_I_Day_10_Recursion_Backtracking
5-
// #Level_1_Day_3_Linked_List #Udemy_Linked_List
5+
// #Level_1_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(1)
66
// #2022_06_28_Time_0_ms_(100.00%)_Space_43.9_MB_(7.98%)
77

88
import com_github_leetcode.ListNode;

0 commit comments

Comments
 (0)