Skip to content

Commit a616418

Browse files
authored
Added complexity tags 416-1143
1 parent ab78db0 commit a616418

File tree

20 files changed

+234
-9
lines changed

20 files changed

+234
-9
lines changed

src/main/java/g0401_0500/s0416_partition_equal_subset_sum/Solution.java

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

33
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Level_2_Day_13_Dynamic_Programming
4-
// #2022_12_29_Time_27_ms_(94.53%)_Space_41.8_MB_(95.29%)
4+
// #Big_O_Time_O(n*sums)_Space_O(n*sums) #2022_12_29_Time_27_ms_(94.53%)_Space_41.8_MB_(95.29%)
55

66
public class Solution {
77
public boolean canPartition(int[] nums) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The time complexity of the program depends on two main factors:
4+
5+
1. The sum of all elements in the input array: The program iterates through the array to calculate the sum of all elements. This step takes O(n) time, where 'n' is the number of elements in the array.
6+
7+
2. Dynamic Programming (DP) Loop: The program uses a dynamic programming approach to determine if a subset sum (less than or equal to half of the total sum) can be achieved. It uses a 2D boolean array 'dp' with dimensions (n+1) x (sums/2+1), where 'n' is the number of elements in the array, and 'sums' is the sum of all elements. The nested loops that iterate through 'num' and 'sum' take O(n * sums) time.
8+
9+
Combining these two factors, the overall time complexity of the program is O(n * sums), where 'n' is the number of elements in the input array, and 'sums' is the sum of all elements.
10+
11+
**Space Complexity (Big O Space):**
12+
13+
The space complexity of the program is determined by the additional data structures used:
14+
15+
1. Integer variable 'sums': This variable stores the sum of all elements in the input array. It requires O(1) space.
16+
17+
2. Boolean array 'dp': This 2D array has dimensions (n+1) x (sums/2+1). In the worst case, the size of 'dp' is proportional to 'n' and the sum of all elements in the input array. Therefore, the space complexity due to 'dp' is O(n * sums).
18+
19+
3. Various integer variables used for iteration and intermediate calculations: These variables require constant space, O(1).
20+
21+
Combining these factors, the dominant factor in the space complexity is the 'dp' array, which can grow to O(n * sums) in size in the worst case. Therefore, the space complexity of the program is O(n * sums).
22+
23+
In summary, the provided program has a time complexity of O(n * sums) and a space complexity of O(n * sums), where 'n' is the number of elements in the input array, and 'sums' is the sum of all elements.

src/main/java/g0401_0500/s0437_path_sum_iii/Solution.java

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

33
// #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree #Level_2_Day_7_Tree
4-
// #2022_07_16_Time_18_ms_(45.66%)_Space_42_MB_(88.96%)
4+
// #Big_O_Time_O(n)_Space_O(n) #2022_07_16_Time_18_ms_(45.66%)_Space_42_MB_(88.96%)
55

66
import com_github_leetcode.TreeNode;
77

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 the program primarily depends on two recursive functions:
4+
5+
1. `pathSum(TreeNode root, int targetSum)`: This function is the main driver of the calculation. It traverses the entire tree and calls the `helper` function for each node. In the worst case, it visits all nodes of the binary tree once. Therefore, its time complexity is O(n), where 'n' is the number of nodes in the tree.
6+
7+
2. `helper(TreeNode node, int targetSum, long currSum)`: This function recursively explores all possible paths from a given node downward to its descendants. In the worst case, it traverses all paths from the root to leaf nodes. Since each node is visited once, the time complexity of this function is also O(n).
8+
9+
Combining both functions, the overall time complexity of the program is O(n), where 'n' is the number of nodes in the binary tree.
10+
11+
**Space Complexity (Big O Space):**
12+
13+
The space complexity of the program is primarily determined by the function call stack during recursion and the constant space used for variables:
14+
15+
1. Function Call Stack: The maximum depth of the call stack is the height of the binary tree. In the worst case, when the tree is skewed (completely unbalanced), the height can be 'n,' making the space complexity due to the call stack O(n).
16+
17+
2. Various integer variables used for iteration and intermediate calculations: These variables require constant space, O(1).
18+
19+
Combining these factors, the dominant factor in the space complexity is the call stack, which can grow up to O(n) in the worst case.
20+
21+
In summary, the provided program has a time complexity of O(n) and a space complexity of O(n), where 'n' is the number of nodes in the binary tree.

src/main/java/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.java

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

33
// #Medium #Top_100_Liked_Questions #String #Hash_Table #Sliding_Window
44
// #Algorithm_II_Day_5_Sliding_Window #Programming_Skills_II_Day_12
5-
// #Level_1_Day_12_Sliding_Window/Two_Pointer #2022_07_16_Time_6_ms_(99.03%)_Space_47.9_MB_(50.50%)
5+
// #Level_1_Day_12_Sliding_Window/Two_Pointer #Big_O_Time_O(n+m)_Space_O(1)
6+
// #2022_07_16_Time_6_ms_(99.03%)_Space_47.9_MB_(50.50%)
67

78
import java.util.ArrayList;
89
import java.util.List;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The time complexity of the program is primarily determined by the two loops that iterate through the characters of strings `s` and `p`:
4+
5+
1. The first loop iterates through the characters of string `s`, where `i` goes from 0 to the length of `s`. This loop has a time complexity of O(n), where 'n' is the length of string `s`.
6+
7+
2. The second loop iterates through the characters of string `p`, where `i` goes from 0 to the length of `p`. This loop has a time complexity of O(m), where 'm' is the length of string `p`.
8+
9+
3. Inside the first loop, there's a constant-time operation that updates the `map` array based on characters in string `p`. This operation takes O(1) time.
10+
11+
4. Additionally, there is a loop that iterates through the `map` array, which has a constant length of 26 (since it represents lowercase English letters). This loop takes constant time, O(26) or simply O(1).
12+
13+
Combining these factors, the overall time complexity of the program is O(n + m), where 'n' is the length of string `s`, and 'm' is the length of string `p`. In the worst case, when both strings are of similar lengths, it can be approximated as O(n).
14+
15+
**Space Complexity (Big O Space):**
16+
17+
The space complexity of the program is primarily determined by the following data structures and variables:
18+
19+
1. `map` array of size 26: This array is used to keep track of character frequencies in string `p`. It requires constant space, O(26) or simply O(1).
20+
21+
2. `res` list: The space required for the result list depends on the number of anagrams found, but it's not included in the space complexity analysis.
22+
23+
3. Integer variables `i`, `j`, and `idx`, which require constant space, O(1).
24+
25+
4. Boolean variable `finish`, which requires constant space, O(1).
26+
27+
Therefore, the dominant factor in the space complexity is the `map` array, which takes O(1) space.
28+
29+
In summary, the provided program has a time complexity of O(n + m) and a space complexity of O(1), where 'n' is the length of string `s`, and 'm' is the length of string `p`.

src/main/java/g0401_0500/s0494_target_sum/Solution.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package g0401_0500.s0494_target_sum;
22

33
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Backtracking
4+
// #Big_O_Time_O(n*(sum+s))_Space_O(n*(sum+s))
45
// #2022_07_21_Time_9_ms_(79.99%)_Space_45.2_MB_(32.79%)
56

67
public class Solution {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The time complexity of the program is determined by the following major components:
4+
5+
1. Calculating the total sum of elements in the 'nums' array: This requires iterating through all the elements of 'nums', so it has a time complexity of O(n), where 'n' is the length of the 'nums' array.
6+
7+
2. Checking for invalid 's': The conditionals that check if 's' is invalid and returning 0 have constant time complexity, O(1).
8+
9+
3. Initializing the dynamic programming (DP) table 'dp': The initialization of the 'dp' table involves iterating through the 'nums' array once. This step has a time complexity of O(n), where 'n' is the length of the 'nums' array.
10+
11+
4. Filling in the DP table: The nested loops that fill in the 'dp' table have a time complexity of O(n * (sum + s)), where 'n' is the length of the 'nums' array, 'sum' is the sum of elements in 'nums', and 's' is the target sum.
12+
13+
Overall, the dominant factor in the time complexity is the nested loops that fill in the 'dp' table, and their time complexity is O(n * (sum + s)). In the worst case, 'sum' and 's' could both be large, leading to a time complexity that grows with the product of 'n', 'sum', and 's'.
14+
15+
**Space Complexity (Big O Space):**
16+
17+
The space complexity of the program is determined by the following major components:
18+
19+
1. Integer variables 'sum' and 's': These variables require constant space, O(1).
20+
21+
2. The DP table 'dp': The 'dp' table is a two-dimensional array of size [(sum + s) / 2 + 1][n + 1], where 'n' is the length of the 'nums' array. Therefore, the space complexity of the 'dp' table is O(n * (sum + s)).
22+
23+
3. Integer variables used in loops: These variables require constant space, O(1).
24+
25+
Overall, the dominant factor in the space complexity is the 'dp' table, and its space complexity is O(n * (sum + s)).
26+
27+
In summary, the provided program has a time complexity of O(n * (sum + s)) and a space complexity of O(n * (sum + s)), where 'n' is the length of the 'nums' array, 'sum' is the sum of elements in 'nums', and 's' is the target sum. The time complexity grows with the product of 'n', 'sum', and 's', making it potentially high for large inputs.

src/main/java/g0501_0600/s0543_diameter_of_binary_tree/Solution.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package g0501_0600.s0543_diameter_of_binary_tree;
22

33
// #Easy #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree #Level_2_Day_7_Tree
4-
// #Udemy_Tree_Stack_Queue #2022_08_02_Time_1_ms_(65.86%)_Space_43.5_MB_(33.52%)
4+
// #Udemy_Tree_Stack_Queue #Big_O_Time_O(n)_Space_O(n)
5+
// #2022_08_02_Time_1_ms_(65.86%)_Space_43.5_MB_(33.52%)
56

67
import com_github_leetcode.TreeNode;
78

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 the program is determined by the depth-first traversal of the binary tree. Specifically, the time complexity is governed by the `diameterOfBinaryTreeUtil` function, which recursively traverses the tree.
4+
5+
In each call to `diameterOfBinaryTreeUtil`, the program visits each node once. Therefore, the time complexity of visiting all nodes in the binary tree is O(n), where 'n' is the number of nodes in the tree.
6+
7+
Hence, the overall time complexity of the program is O(n), where 'n' is the number of nodes in the binary tree.
8+
9+
**Space Complexity (Big O Space):**
10+
11+
The space complexity of the program is determined by the function call stack during the recursive traversal of the binary tree.
12+
13+
1. The space used by the call stack is proportional to the height of the binary tree. In the worst case, when the tree is completely unbalanced (a skewed tree), the height of the tree can be 'n' (where 'n' is the number of nodes), resulting in a space complexity of O(n).
14+
15+
2. Additionally, the program uses a single integer variable `diameter` to keep track of the maximum diameter found during the traversal. This variable requires constant space, O(1).
16+
17+
Therefore, the overall space complexity of the program is O(n), where 'n' is the number of nodes in the binary tree. The dominant factor in the space complexity is the call stack space used during the recursion.
18+
19+
In summary, the provided program has a time complexity of O(n) and a space complexity of O(n), where 'n' is the number of nodes in the binary tree. It efficiently finds the diameter of the binary tree by performing a depth-first traversal.

0 commit comments

Comments
 (0)