Skip to content

Commit 48749f7

Browse files
authored
Updated java tasks
1 parent 023a278 commit 48749f7

File tree

7 files changed

+371
-328
lines changed

7 files changed

+371
-328
lines changed

README.md

Lines changed: 307 additions & 307 deletions
Large diffs are not rendered by default.

src/main/java/g0001_0100/s0001_two_sum/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
44
// #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
5-
// #2024_01_04_Time_2_ms_(85.97%)_Space_44.8_MB_(15.45%)
5+
// #AI_can_be_used_to_solve_the_task #2024_01_04_Time_2_ms_(85.97%)_Space_44.8_MB_(15.45%)
66

77
import java.util.HashMap;
88
import java.util.Map;

src/main/java/g0001_0100/s0002_add_two_numbers/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 #Top_Interview_Questions #Math #Linked_List #Recursion
44
// #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
5-
// #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #2024_01_04_Time_1_ms_(100.00%)_Space_44.4_MB_(16.63%)
5+
// #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #AI_can_be_used_to_solve_the_task
6+
// #2024_01_04_Time_1_ms_(100.00%)_Space_44.4_MB_(16.63%)
67

78
import com_github_leetcode.ListNode;
89

src/main/java/g0001_0100/s0002_add_two_numbers/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public class Solution {
122122
solution.printList(result2);
123123

124124
ListNode l5 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9)))))));
125-
ListNode l6 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9)))));
125+
ListNode l6 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9))));
126126
ListNode result3 = solution.addTwoNumbers(l5, l6);
127127
System.out.print("Example 3 Output: ");
128128
solution.printList(result3);

src/main/java/g0101_0200/s0152_maximum_product_subarray/Solution.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,23 @@
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-
// #Big_O_Time_O(N)_Space_O(1) #2022_06_25_Time_0_ms_(100.00%)_Space_42.7_MB_(82.46%)
5+
// #Big_O_Time_O(N)_Space_O(1) #2024_07_03_Time_1_ms_(92.31%)_Space_44.6_MB_(75.65%)
66

77
public class Solution {
8-
public int maxProduct(int[] arr) {
9-
int ans = Integer.MIN_VALUE;
10-
int cprod = 1;
11-
for (int j : arr) {
12-
cprod = cprod * j;
13-
ans = Math.max(ans, cprod);
14-
if (cprod == 0) {
15-
cprod = 1;
8+
public int maxProduct(int[] nums) {
9+
int currentMaxProd = nums[0];
10+
int currentMinProd = nums[0];
11+
int overAllMaxProd = nums[0];
12+
for (int i = 1; i < nums.length; i++) {
13+
if (nums[i] < 0) {
14+
int temp = currentMaxProd;
15+
currentMaxProd = currentMinProd;
16+
currentMinProd = temp;
1617
}
18+
currentMaxProd = Math.max(nums[i], nums[i] * currentMaxProd);
19+
currentMinProd = Math.min(nums[i], nums[i] * currentMinProd);
20+
overAllMaxProd = Math.max(overAllMaxProd, currentMaxProd);
1721
}
18-
cprod = 1;
19-
for (int i = arr.length - 1; i >= 0; i--) {
20-
cprod = cprod * arr[i];
21-
ans = Math.max(ans, cprod);
22-
if (cprod == 0) {
23-
cprod = 1;
24-
}
25-
}
26-
return ans;
22+
return overAllMaxProd;
2723
}
2824
}

src/main/java/g0201_0300/s0208_implement_trie_prefix_tree/Trie.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,11 @@ public boolean startsWith(String prefix) {
6666
return startWith;
6767
}
6868
}
69+
70+
/*
71+
* Your Trie object will be instantiated and called as such:
72+
* Trie obj = new Trie();
73+
* obj.insert(word);
74+
* boolean param_2 = obj.search(word);
75+
* boolean param_3 = obj.startsWith(prefix);
76+
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
**Time Complexity (Big O Time):**
2+
3+
1. **Insertion (insert):**
4+
- In the `insert` method, the program iterates through the characters of the input word, and for each character, it follows the corresponding child node in the Trie.
5+
- The number of iterations depends on the length of the word, which is O(word.length()).
6+
- Since each character is processed once, the insertion of a single word takes O(word.length()) time.
7+
8+
2. **Search (search):**
9+
- In the `search` method, the program iterates through the characters of the input word, following the corresponding child nodes in the Trie.
10+
- The number of iterations depends on the length of the word, which is O(word.length()).
11+
- In the worst case, when the Trie contains a large number of words with the same prefix, searching for a word could take O(word.length()) time.
12+
13+
3. **StartsWith (startsWith):**
14+
- The `startsWith` method calls the `search` method to find whether any word starts with the given prefix.
15+
- This is similar to the search operation and also takes O(prefix.length()) time.
16+
17+
Overall, the time complexity for insertion, search, and startsWith operations in the Trie is O(word.length()) or O(prefix.length()), depending on the length of the word or prefix being processed.
18+
19+
**Space Complexity (Big O Space):**
20+
21+
1. **TrieNode Array (children):**
22+
- The Trie is represented using a tree structure where each node (TrieNode) has an array of children (of size 26 for lowercase English letters).
23+
- In the worst case, where all words are distinct and there are no common prefixes, the Trie would have a node for each character in all words.
24+
- The space complexity for the TrieNode array is O(N), where N is the total number of characters in all inserted words.
25+
26+
2. **Recursive Call Stack:**
27+
- During insertion and search operations, the program uses recursion, which results in a function call stack.
28+
- The depth of the call stack is bounded by the length of the word or prefix being processed.
29+
- In the worst case, this depth can be O(word.length()) or O(prefix.length()).
30+
31+
3. **Other Variables:**
32+
- The `root` variable is a constant space requirement.
33+
- The `startWith` variable is also constant space.
34+
35+
The dominant factor in the space complexity is typically the TrieNode array, which is O(N), where N is the total number of characters in all inserted words.
36+
37+
In summary, the space complexity of this Trie implementation is O(N), and the time complexity for insertion, search, and startsWith operations is O(word.length()) or O(prefix.length()), depending on the length of the word or prefix being processed.
38+
8

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy