Skip to content

Commit

Permalink
Updated java tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Oct 2, 2024
1 parent 023a278 commit 48749f7
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 328 deletions.
614 changes: 307 additions & 307 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main/java/g0001_0100/s0001_two_sum/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
// #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
// #2024_01_04_Time_2_ms_(85.97%)_Space_44.8_MB_(15.45%)
// #AI_can_be_used_to_solve_the_task #2024_01_04_Time_2_ms_(85.97%)_Space_44.8_MB_(15.45%)

import java.util.HashMap;
import java.util.Map;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/g0001_0100/s0002_add_two_numbers/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
// #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
// #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%)
// #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #AI_can_be_used_to_solve_the_task
// #2024_01_04_Time_1_ms_(100.00%)_Space_44.4_MB_(16.63%)

import com_github_leetcode.ListNode;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0001_0100/s0002_add_two_numbers/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public class Solution {
solution.printList(result2);

ListNode l5 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9)))))));
ListNode l6 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9)))));
ListNode l6 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9))));
ListNode result3 = solution.addTwoNumbers(l5, l6);
System.out.print("Example 3 Output: ");
solution.printList(result3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,23 @@

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
// #Dynamic_Programming_I_Day_6 #Level_2_Day_13_Dynamic_Programming #Udemy_Dynamic_Programming
// #Big_O_Time_O(N)_Space_O(1) #2022_06_25_Time_0_ms_(100.00%)_Space_42.7_MB_(82.46%)
// #Big_O_Time_O(N)_Space_O(1) #2024_07_03_Time_1_ms_(92.31%)_Space_44.6_MB_(75.65%)

public class Solution {
public int maxProduct(int[] arr) {
int ans = Integer.MIN_VALUE;
int cprod = 1;
for (int j : arr) {
cprod = cprod * j;
ans = Math.max(ans, cprod);
if (cprod == 0) {
cprod = 1;
public int maxProduct(int[] nums) {
int currentMaxProd = nums[0];
int currentMinProd = nums[0];
int overAllMaxProd = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] < 0) {
int temp = currentMaxProd;
currentMaxProd = currentMinProd;
currentMinProd = temp;
}
currentMaxProd = Math.max(nums[i], nums[i] * currentMaxProd);
currentMinProd = Math.min(nums[i], nums[i] * currentMinProd);
overAllMaxProd = Math.max(overAllMaxProd, currentMaxProd);
}
cprod = 1;
for (int i = arr.length - 1; i >= 0; i--) {
cprod = cprod * arr[i];
ans = Math.max(ans, cprod);
if (cprod == 0) {
cprod = 1;
}
}
return ans;
return overAllMaxProd;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,11 @@ public boolean startsWith(String prefix) {
return startWith;
}
}

/*
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
**Time Complexity (Big O Time):**

1. **Insertion (insert):**
- 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.
- The number of iterations depends on the length of the word, which is O(word.length()).
- Since each character is processed once, the insertion of a single word takes O(word.length()) time.

2. **Search (search):**
- In the `search` method, the program iterates through the characters of the input word, following the corresponding child nodes in the Trie.
- The number of iterations depends on the length of the word, which is O(word.length()).
- 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.

3. **StartsWith (startsWith):**
- The `startsWith` method calls the `search` method to find whether any word starts with the given prefix.
- This is similar to the search operation and also takes O(prefix.length()) time.

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.

**Space Complexity (Big O Space):**

1. **TrieNode Array (children):**
- The Trie is represented using a tree structure where each node (TrieNode) has an array of children (of size 26 for lowercase English letters).
- 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.
- The space complexity for the TrieNode array is O(N), where N is the total number of characters in all inserted words.

2. **Recursive Call Stack:**
- During insertion and search operations, the program uses recursion, which results in a function call stack.
- The depth of the call stack is bounded by the length of the word or prefix being processed.
- In the worst case, this depth can be O(word.length()) or O(prefix.length()).

3. **Other Variables:**
- The `root` variable is a constant space requirement.
- The `startWith` variable is also constant space.

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.

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.
8

0 comments on commit 48749f7

Please sign in to comment.