Skip to content

Commit 2da4ba7

Browse files
authored
Added complexity tags1-20
1 parent 1736c86 commit 2da4ba7

File tree

22 files changed

+213
-11
lines changed

22 files changed

+213
-11
lines changed

src.save/main/java/g0001_0100/s0001_two_sum/Solution.java

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

33
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
4-
// #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays
4+
// #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
55
// #2023_08_09_Time_1_ms_(99.37%)_Space_43.5_MB_(94.36%)
66

77
import java.util.HashMap;
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+
The time complexity of this program is O(n), where "n" represents the number of elements in the `numbers` array. Here's the breakdown:
3+
4+
1. The program iterates through the `numbers` array once using a `for` loop, which runs for "n" iterations (n is the length of the array).
5+
2. Inside the loop, it performs constant-time operations:
6+
- It calculates the `requiredNum` (constant time).
7+
- It checks if `requiredNum` is present in the `indexMap` (constant time).
8+
- It puts `numbers[i]` and `i` into the `indexMap` (constant time).
9+
10+
Since all the operations inside the loop are constant time, and the loop itself runs for "n" iterations, the overall time complexity is O(n).
11+
12+
**Space Complexity (Big O Space):**
13+
The space complexity of this program is also O(n), where "n" represents the number of elements in the `numbers` array. Here's why:
14+
15+
1. The program uses a `Map<Integer, Integer>` called `indexMap` to store the numbers and their corresponding indices. In the worst case, it could store all "n" elements from the `numbers` array.
16+
2. The program returns an integer array of size 2 (constant space), regardless of the input size.
17+
18+
The dominant factor in terms of space complexity is the `indexMap`, which can potentially store "n" key-value pairs. Therefore, the space complexity is O(n).

src.save/main/java/g0001_0100/s0002_add_two_numbers/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 #Math #Linked_List #Recursion
44
// #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
5-
// #2023_08_09_Time_1_ms_(100.00%)_Space_43.1_MB_(78.48%)
5+
// #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #2023_08_09_Time_1_ms_(100.00%)_Space_43.1_MB_(78.48%)
66

77
import com_github_leetcode.ListNode;
88

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+
The time complexity of this program is O(max(N, M)), where "N" and "M" represent the lengths of the input linked lists `l1` and `l2`, respectively. Here's the breakdown:
3+
4+
1. The program iterates through both linked lists `l1` and `l2` simultaneously using a `while` loop. The loop runs as long as either of the two lists has elements to process.
5+
2. Inside the loop, it performs constant-time operations for each node:
6+
- It calculates the sum of three integer values (constant time).
7+
- It creates a new node (constant time).
8+
- It updates pointers and variables (constant time).
9+
10+
Since the loop runs until the end of the longer of the two input linked lists, the overall time complexity is O(max(N, M)), where "N" and "M" are the lengths of `l1` and `l2`, respectively.
11+
12+
**Space Complexity (Big O Space):**
13+
The space complexity of this program is O(max(N, M)), which is primarily determined by the space used for the output linked list. Here's why:
14+
15+
1. The program creates a new linked list to store the result, and the length of this linked list can be at most max(N, M) + 1 (where +1 is for the potential carry at the end).
16+
2. Other than the output linked list, the program uses a constant amount of space for variables like `dummyHead`, `p`, `q`, `curr`, and `carry`.
17+
18+
Therefore, the space complexity is O(max(N, M)).

src.save/main/java/g0001_0100/s0003_longest_substring_without_repeating_characters/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 #String #Hash_Table #Sliding_Window
44
// #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings
5-
// #2023_08_09_Time_2_ms_(99.88%)_Space_43.7_MB_(58.61%)
5+
// #Big_O_Time_O(n)_Space_O(1) #2023_08_09_Time_2_ms_(99.88%)_Space_43.7_MB_(58.61%)
66

77
public class Solution {
88
public int lengthOfLongestSubstring(String s) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
**Time Complexity (Big O Time):**
2+
The time complexity of this program is O(n), where "n" represents the length of the input string `s`. Here's the breakdown:
3+
4+
1. The program iterates through the characters of the input string `s` using a `for` loop, and this loop runs for "n" iterations, where "n" is the length of `s`.
5+
2. Inside the loop, it performs constant-time operations:
6+
- Initializing the `lastIndices` array (constant time).
7+
- Updating values in the `lastIndices` array (constant time).
8+
- Updating variables like `maxLen`, `curLen`, `start`, and `lastIndices[cur]` (constant time).
9+
10+
Since the loop runs for "n" iterations, and all operations inside the loop are constant time, the overall time complexity is O(n).
11+
12+
**Space Complexity (Big O Space):**
13+
The space complexity of this program is O(256) = O(1), as it uses a fixed-size array `lastIndices` of length 256 to store the last indices of characters. This array's size does not depend on the input size and remains constant.
14+
15+
Additionally, the program uses a few integer variables and constant space to store the result. The space used for these variables does not depend on the input size and is also considered constant.
16+
17+
Therefore, the space complexity is O(1), or more precisely, O(256), which is still considered constant because it doesn't grow with the input size.

src.save/main/java/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.java

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

33
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
4-
// #2023_08_09_Time_1_ms_(100.00%)_Space_44.4_MB_(96.42%)
4+
// #Big_O_Time_O(log(min(N,M)))_Space_O(1) #2023_08_09_Time_1_ms_(100.00%)_Space_44.4_MB_(96.42%)
55

66
@SuppressWarnings("java:S2234")
77
public class Solution {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
**Time Complexity (Big O Time):**
2+
The time complexity of this program is O(log(min(N, M))), where "N" and "M" represent the lengths of the input arrays `nums1` and `nums2`, respectively. Here's the breakdown:
3+
4+
1. The program uses a binary search algorithm to find the median of the two sorted arrays.
5+
2. The binary search operates on the smaller of the two input arrays. This choice ensures that the search is performed on the shorter array, which reduces the number of iterations.
6+
3. In each iteration of the binary search, the program performs constant-time operations. These operations include arithmetic calculations, comparisons, and array access.
7+
8+
Since the binary search reduces the search space by half in each iteration, the time complexity of this algorithm is O(log(min(N, M))), where "N" and "M" are the lengths of the input arrays `nums1` and `nums2`, respectively.
9+
10+
**Space Complexity (Big O Space):**
11+
The space complexity of this program is O(1), which means it uses a constant amount of extra space that does not depend on the input size. The program uses a few integer variables (`cut1`, `cut2`, `n1`, `n2`, `low`, `high`, `l1`, `l2`, `r1`, and `r2`) to keep track of indices and values during the binary search. These variables take up a constant amount of space, regardless of the input size.
12+
13+
Therefore, the space complexity is O(1).

src.save/main/java/g0001_0100/s0005_longest_palindromic_substring/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 #String #Dynamic_Programming
44
// #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming
5-
// #Dynamic_Programming_I_Day_17 #Udemy_Strings
5+
// #Dynamic_Programming_I_Day_17 #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
66
// #2023_08_09_Time_7_ms_(97.94%)_Space_41.9_MB_(56.27%)
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+
The time complexity of this program is O(n), where "n" represents the length of the input string `s`. Here's the breakdown:
3+
4+
1. The program first creates a new string `newStr` by inserting '#' characters between each character in the original string `s`. This operation has a time complexity of O(n) because it processes each character of `s`.
5+
6+
2. After creating `newStr`, the program uses the Manacher's Algorithm to find the longest palindromic substring. This algorithm is known to have a linear time complexity of O(n).
7+
8+
3. The loop that implements Manacher's Algorithm iterates through the characters of `newStr`. In each iteration, it performs constant-time operations like comparisons and arithmetic calculations. The key operation within this loop is the palindrome expansion, which extends the palindrome around the current center. The loop iterates through `newStr` only once, and each expansion step performs constant work.
9+
10+
Therefore, the overall time complexity of the program is dominated by the linear-time Manacher's Algorithm, making it O(n).
11+
12+
**Space Complexity (Big O Space):**
13+
The space complexity of this program is O(n), where "n" represents the length of the input string `s`. Here's why:
14+
15+
1. The program creates a new character array `newStr` with a length of `s.length() * 2 + 1`. This array stores a modified version of the input string `s` with '#' characters inserted. The space used by `newStr` is proportional to the length of `s`, so it has a space complexity of O(n).
16+
17+
2. The program uses an integer array `dp` of the same length as `newStr` to store information about the lengths of palindromes centered at different positions. The space complexity of `dp` is also O(n).
18+
19+
3. The program uses several integer variables (`friendCenter`, `friendRadius`, `lpsCenter`, and `lpsRadius`) to keep track of information during the algorithm. These variables take up constant space.
20+
21+
Therefore, the overall space complexity is dominated by the space used for `newStr` and `dp`, both of which are O(n).

0 commit comments

Comments
 (0)