Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8ad626c

Browse files
authored
Added complexity tags 138-160
1 parentf433c16 commit8ad626c

File tree

20 files changed

+193
-10
lines changed

20 files changed

+193
-10
lines changed

‎src.save/main/java/g0101_0200/s0138_copy_list_with_random_pointer/Solution.java

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

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Linked_List
4-
// #Programming_Skills_II_Day_14 #Udemy_Linked_List
4+
// #Programming_Skills_II_Day_14 #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(N)
55
// #2022_06_24_Time_0_ms_(100.00%)_Space_45.5_MB_(56.49%)
66

77
importcom_github_leetcode.random.Node;
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+
1.**First Pass**: In the first pass, the program iterates through the original linked list and creates a cloned node for each node in the original list. This operation takes O(N) time, where N is the number of nodes in the original list, as it processes each node once.
4+
5+
2.**Second Pass**: In the second pass, the program updates the random pointers of the cloned nodes based on the random pointers of the original nodes. This operation also takes O(N) time since it processes each node once.
6+
7+
3.**Third Pass**: In the third pass, the program separates the cloned linked list from the original linked list and restores the original linked list's next pointers. This final pass also takes O(N) time, as it processes each node once.
8+
9+
Overall, the time complexity of the program is O(N), where N is the number of nodes in the linked list. All three passes contribute linearly to the time complexity.
10+
11+
**Space Complexity (Big O Space):**
12+
13+
The space complexity of the program is O(N), where N is the number of nodes in the linked list. Here's how the space is allocated:
14+
15+
1.**Cloned Nodes**: During the first pass, the program creates cloned nodes for each node in the original linked list. This results in N additional nodes, each taking constant space. Therefore, the space used for the cloned nodes is O(N).
16+
17+
2.**Random Pointers**: No additional space is used for random pointers since the program manipulates the existing nodes in the original linked list to connect to their corresponding cloned nodes.
18+
19+
3.**New Head Node**: A single variable`newHead` is used to store the head of the cloned linked list. This variable takes constant space.
20+
21+
4.**Other Variables**: The program uses a few additional variables (`curr`,`clonedNode`) to traverse and manipulate the linked list. These variables take constant space.
22+
23+
Overall, the space complexity is O(N) due to the cloned nodes created during the first pass, which directly corresponds to the number of nodes in the linked list.

‎src.save/main/java/g0101_0200/s0139_word_break/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
44
// #Dynamic_Programming #Trie #Memoization #Algorithm_II_Day_15_Dynamic_Programming
5-
// #Dynamic_Programming_I_Day_9 #Udemy_Dynamic_Programming
5+
// #Dynamic_Programming_I_Day_9 #Udemy_Dynamic_Programming #Big_O_Time_O(M+max*N)_Space_O(M+N+max)
66
// #2022_06_24_Time_2_ms_(97.08%)_Space_42.1_MB_(90.92%)
77

88
importjava.util.HashSet;
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+
-**HashSet Initialization**: Initializing the`HashSet` with the words from the`wordDict` takes O(M) time, where M is the total length of all words in the dictionary.
4+
5+
-**Maximum Length Calculation**: In the first loop, the program calculates the maximum word length in the`wordDict`. This takes O(M) time, as it iterates through all the words.
6+
7+
-**DFS Function Calls**: The main work is done in the`dfs` function. The function is called multiple times with different`end` values (from 1 to the maximum word length). The number of calls depends on the maximum word length, which we denoted as "max." Therefore, the time complexity of the DFS portion is O(max * N), where N is the length of the input string`s`.
8+
9+
-**Flag Array**: The program uses a`flag` array to memoize subproblem results to avoid redundant work. Initializing this array takes O(N) time since it has a length equal to the length of the input string`s`.
10+
11+
Combining these steps, the overall time complexity of the program is O(M + max * N), where M is the total length of all words in the dictionary, N is the length of the input string`s`, and max is the maximum word length in the dictionary.
12+
13+
**Space Complexity (Big O Space):**
14+
15+
-**HashSet Space**: The`HashSet``set` is used to store the words from the dictionary. It consumes O(M) space, where M is the total length of all words in the dictionary.
16+
17+
-**Flag Array**: The`flag` array is used for memoization, and it has a length equal to the length of the input string`s`, so it consumes O(N) space.
18+
19+
-**DFS Recursive Calls**: The depth of the recursive calls in the`dfs` function can go up to "max," where "max" is the maximum word length in the dictionary. This contributes to the call stack space. Therefore, in the worst case, the space complexity due to recursive calls is O(max).
20+
21+
Combining these space requirements, the overall space complexity of the program is O(M + N + max).
22+
23+
In summary, the time complexity is O(M + max * N), and the space complexity is O(M + N + max), where M is the total length of all words in the dictionary, N is the length of the input string`s`, and max is the maximum word length in the dictionary.

‎src.save/main/java/g0101_0200/s0141_linked_list_cycle/Solution.java

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

33
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Two_Pointers #Linked_List
4-
// #Data_Structure_I_Day_7_Linked_List #Udemy_Linked_List
4+
// #Data_Structure_I_Day_7_Linked_List #Udemy_Linked_List #Big_O_Time_O(N)_Space_O(1)
55
// #2022_06_24_Time_0_ms_(100.00%)_Space_45.5_MB_(68.52%)
66

77
importcom_github_leetcode.ListNode;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
**Time Complexity (Big O Time):**
2+
3+
- The program uses two pointers,`fast` and`slow`, to traverse the linked list.
4+
- The`fast` pointer moves twice as fast as the`slow` pointer in each iteration.
5+
6+
The key insight here is that if there is a cycle in the linked list, the`fast` and`slow` pointers will eventually meet (i.e.,`fast == slow`). If there is no cycle, the`fast` pointer will reach the end of the list (i.e.,`fast == null` or`fast.next == null`) before the`slow` pointer.
7+
8+
Therefore, the time complexity of this algorithm is O(N), where N is the number of nodes in the linked list. This is because in the worst case, when there is no cycle, both pointers will traverse the entire linked list once.
9+
10+
**Space Complexity (Big O Space):**
11+
12+
The space complexity of this algorithm is O(1), which means it uses a constant amount of extra space regardless of the size of the input linked list. This is because it only uses two additional pointers (`fast` and`slow`) to traverse the list and does not rely on additional data structures or recursion that would consume additional memory.
13+
14+
In summary, the time complexity of this program is O(N), and the space complexity is O(1), where N is the number of nodes in the linked list.

‎src.save/main/java/g0101_0200/s0142_linked_list_cycle_ii/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 #Hash_Table #Two_Pointers #Linked_List
44
// #Data_Structure_II_Day_10_Linked_List #Level_1_Day_4_Linked_List #Udemy_Linked_List
5-
// #2022_06_24_Time_0_ms_(100.00%)_Space_42.3_MB_(98.70%)
5+
// #Big_O_Time_O(N)_Space_O(1) #2022_06_24_Time_0_ms_(100.00%)_Space_42.3_MB_(98.70%)
66

77
importcom_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+
3+
- The program uses two pointers,`fast` and`slow`, to traverse the linked list.
4+
- The first while loop is used to determine if there is a cycle and find the meeting point of`fast` and`slow`. This loop runs until`fast` and`slow` meet inside the loop or reach the end of the list.
5+
6+
If there is no cycle, both pointers will reach the end of the list in O(N) time, where N is the number of nodes in the linked list.
7+
8+
If there is a cycle, the`fast` and`slow` pointers will meet inside the loop. The maximum number of iterations needed to detect the cycle and find the meeting point is less than or equal to N. Therefore, the time complexity remains O(N).
9+
10+
The second while loop is used to find the starting node of the cycle. It starts from the head of the list and moves`slow` and`fast` pointers at the same speed until they meet at the starting node of the cycle. This loop runs in O(N) time.
11+
12+
Overall, the time complexity of this algorithm is O(N).
13+
14+
**Space Complexity (Big O Space):**
15+
16+
The space complexity of this algorithm is O(1), which means it uses a constant amount of extra space regardless of the size of the input linked list. It only uses a few additional pointers (`fast` and`slow`) to traverse the list and does not rely on additional data structures or recursion that would consume additional memory.
17+
18+
In summary, the time complexity of this program is O(N), and the space complexity is O(1), where N is the number of nodes in the linked list.

‎src.save/main/java/g0101_0200/s0146_lru_cache/LRUCache.java

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

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Design #Linked_List
4-
// #Doubly_Linked_List #Udemy_Linked_List #2022_06_24_Time_87_ms_(50.80%)_Space_125.2_MB_(58.73%)
4+
// #Doubly_Linked_List #Udemy_Linked_List #Big_O_Time_O(1)_Space_O(capacity)
5+
// #2022_06_24_Time_87_ms_(50.80%)_Space_125.2_MB_(58.73%)
56

67
importjava.util.HashMap;
78
importjava.util.Map;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**Time Complexity (Big O Time):**
2+
3+
1.`get(int key)` Method:
4+
- Accessing an element from the`cacheMap` is done in constant time (O(1)).
5+
- Moving a node to the head in the`moveToHead` method also operates in constant time (O(1)).
6+
- Overall, the`get` method's time complexity is O(1).
7+
8+
2.`put(int key, int value)` Method:
9+
- Updating the value of an existing key in the`cacheMap` is done in constant time (O(1)).
10+
- Adding a new node to the cache is done in constant time if the cache is not full (O(1)).
11+
- If the cache is full, it removes the node at the tail, which is also done in constant time (O(1)).
12+
- Recursively calling the`put` method when the cache is full does not result in nested loops because the eviction operation happens once in each recursion.
13+
- Overall, the`put` method's time complexity is O(1) on average, but in the worst case (when eviction happens multiple times), it can be considered O(N), where N is the capacity of the cache.
14+
15+
3.`moveToHead(LruCacheNode node)` Method:
16+
- Moving a node to the head of the cache is done in constant time (O(1)) because it involves changing pointers.
17+
- The time complexity of this method is O(1).
18+
19+
Overall, the time complexity of most operations in the LRU cache is O(1), except for the`put` method in the worst-case scenario.
20+
21+
**Space Complexity (Big O Space):**
22+
23+
The space complexity of this LRU cache implementation is O(capacity), where "capacity" represents the maximum number of key-value pairs the cache can store. The primary space usage comes from the`cacheMap`, which stores the key-value pairs, and the linked list of nodes (`head` and`tail`) used to maintain the order of access. Additionally, each node in the linked list consumes space proportional to the key and value sizes.
24+
25+
In summary, the space complexity of the LRU cache is determined by its capacity, and the primary space-consuming data structure is the`cacheMap`, resulting in an overall space complexity of O(capacity).

‎src.save/main/java/g0101_0200/s0148_sort_list/Solution.java

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

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Sorting #Two_Pointers #Linked_List
4-
// #Divide_and_Conquer #Merge_Sort #Level_2_Day_4_Linked_List
4+
// #Divide_and_Conquer #Merge_Sort #Level_2_Day_4_Linked_List #Big_O_Time_O(log(N))_Space_O(log(N))
55
// #2022_06_24_Time_12_ms_(85.82%)_Space_76_MB_(43.84%)
66

77
importcom_github_leetcode.ListNode;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The time complexity of the`sortList` method in this program is O(N*log(N)), where N is the number of nodes in the linked list. Here's the breakdown:
4+
5+
- The recursive`sortList` function divides the linked list into two halves. This division step is performed in O(N/2) time, and it happens recursively, so it results in O(N*log(N)) time complexity.
6+
- The merging step, where the two sorted halves are merged together, takes O(N) time in each recursive call.
7+
8+
Therefore, the overall time complexity of the`sortList` method is O(N*log(N)).
9+
10+
**Space Complexity (Big O Space):**
11+
12+
The space complexity of the`sortList` method is O(log(N)) on average due to the recursive function calls. This is because the space required for the function call stack during the recursion grows logarithmically with the number of elements in the list. Each recursive call creates a new stack frame with its own local variables.
13+
14+
Additionally, the program uses a few extra variables for pointers and a dummy node (`res`). These variables consume a constant amount of space, so they don't significantly affect the overall space complexity.
15+
16+
In summary, the primary space consumption is due to the recursion stack, resulting in an average space complexity of O(log(N)) for this program.

‎src.save/main/java/g0101_0200/s0152_maximum_product_subarray/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
// #Dynamic_Programming_I_Day_6 #Level_2_Day_13_Dynamic_Programming #Udemy_Dynamic_Programming
5-
// #2022_06_25_Time_0_ms_(100.00%)_Space_42.7_MB_(82.46%)
5+
// #Big_O_Time_O(N)_Space_O(1) #2022_06_25_Time_0_ms_(100.00%)_Space_42.7_MB_(82.46%)
66

77
publicclassSolution {
88
publicintmaxProduct(int[]arr) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The time complexity of this program is O(N), where N is the length of the`arr` input array. The program makes two passes over the array:
4+
5+
1.**Forward Pass:** In the first pass, it iterates through the array from left to right, calculating the cumulative product of elements in the`cprod` variable. It also keeps track of the maximum product seen so far in the`ans` variable. This pass takes O(N) time.
6+
7+
2.**Backward Pass:** In the second pass, it iterates through the array from right to left, again calculating the cumulative product in`cprod` and updating the maximum product in`ans`. This pass also takes O(N) time.
8+
9+
Both passes run in linear time, and there are no nested loops or recursive calls, so the overall time complexity is O(N).
10+
11+
**Space Complexity (Big O Space):**
12+
13+
The space complexity of this program is O(1), meaning it uses a constant amount of additional space that does not depend on the size of the input array`arr`. The program uses a fixed number of variables (`ans`,`cprod`,`i`,`j`) and doesn't create any data structures or data collections with a size proportional to`N`.
14+
15+
In summary, this program has a time complexity of O(N) and a space complexity of O(1).

‎src.save/main/java/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution.java

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

33
// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Algorithm_II_Day_2_Binary_Search
4-
// #Binary_Search_I_Day_12 #Udemy_Binary_Search
4+
// #Binary_Search_I_Day_12 #Udemy_Binary_Search #Big_O_Time_O(log_N)_Space_O(log_N)
55
// #2022_06_25_Time_0_ms_(100.00%)_Space_43.3_MB_(6.36%)
66

77
publicclassSolution {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The time complexity of this program is O(log N), where N is the number of elements in the`nums` array. The reason for this is that the program performs a binary search to find the minimum element in the rotated sorted array.
4+
5+
In each recursive call to`findMinUtil`, the program divides the search range in half by calculating the midpoint (`mid`) and then recursively searches either the left half or the right half based on certain conditions. This binary search process reduces the search range by half in each step. Therefore, the time complexity is logarithmic, specifically O(log N).
6+
7+
**Space Complexity (Big O Space):**
8+
9+
The space complexity of this program is O(log N) as well. This is because the program uses the call stack to keep track of recursive function calls. In the worst case, when the array is divided in half in each recursive call, the maximum depth of the call stack will be logarithmic in terms of the number of elements in the`nums` array.
10+
11+
So, both the time and space complexity of this program are O(log N), where N is the number of elements in the`nums` array.

‎src.save/main/java/g0101_0200/s0155_min_stack/MinStack.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 #Stack #Design
44
// #Data_Structure_II_Day_14_Stack_Queue #Programming_Skills_II_Day_18 #Level_2_Day_16_Design
5-
// #Udemy_Design #2022_06_25_Time_3_ms_(100.00%)_Space_44.3_MB_(85.39%)
5+
// #Udemy_Design #Big_O_Time_O(1)_Space_O(N) #2022_06_25_Time_3_ms_(100.00%)_Space_44.3_MB_(85.39%)
66

77
publicclassMinStack {
88
privatestaticclassNode {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
**Time Complexity (Big O Time):**
2+
3+
1.**push(int val):** The`push` operation has a time complexity of O(1). It involves creating a new node and updating pointers, both of which are constant time operations.
4+
5+
2.**pop():** The`pop` operation also has a time complexity of O(1). It simply involves moving the`currentNode` pointer to the previous node.
6+
7+
3.**top():** The`top` operation has a time complexity of O(1) because it directly accesses the`data` field of the current node.
8+
9+
4.**getMin():** The`getMin` operation has a time complexity of O(1) because it directly accesses the`min` field of the current node.
10+
11+
Overall, all the stack operations (push, pop, top, getMin) have a constant time complexity of O(1).
12+
13+
**Space Complexity (Big O Space):**
14+
15+
The space complexity of this MinStack implementation is O(N), where N is the number of elements pushed onto the stack.
16+
17+
- Each element pushed onto the stack consumes space for a new node, which contains integer values (`min`,`data`), a reference to the previous node (`previousNode`), and a reference to the next node (`nextNode`).
18+
- As you push more elements onto the stack, the memory usage increases linearly with the number of elements.
19+
20+
It's important to note that the space complexity is related to the number of elements in the stack (N), not the total number of operations performed. Each element added to the stack consumes additional memory.
21+
22+
In summary, the time complexity of all stack operations is O(1), and the space complexity is O(N), where N is the number of elements pushed onto the stack.

‎src.save/main/java/g0101_0200/s0160_intersection_of_two_linked_lists/Solution.java

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

33
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Two_Pointers #Linked_List
4-
// #Data_Structure_II_Day_11_Linked_List #Udemy_Linked_List
4+
// #Data_Structure_II_Day_11_Linked_List #Udemy_Linked_List #Big_O_Time_O(M+N)_Space_O(1)
55
// #2022_06_25_Time_1_ms_(99.68%)_Space_55.1_MB_(63.42%)
66

77
importcom_github_leetcode.ListNode;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
**Time Complexity (Big O Time):**
2+
3+
The time complexity of this program is O(M + N), where M is the length of the first linked list (headA) and N is the length of the second linked list (headB). Here's why:
4+
5+
- The two pointers`node1` and`node2` traverse their respective linked lists until they either meet at the intersection or reach the end of their lists.
6+
- In the worst case, where there is no intersection, both`node1` and`node2` will traverse both linked lists completely.
7+
- Therefore, each of the two pointers will perform a total of M + N operations (M for the first list and N for the second list).
8+
9+
The program terminates as soon as`node1` and`node2` meet or both become null.
10+
11+
**Space Complexity (Big O Space):**
12+
13+
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 linked lists. The program only uses a fixed number of pointers (`node1`,`node2`) and variables to keep track of the intersection point.
14+
15+
In summary, the time complexity is O(M + N) where M and N are the lengths of the two linked lists, and the space complexity is O(1), indicating a constant amount of additional memory usage.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp