From aafbd1754ff98bdd8183e47839496dd956d766f4 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Thu, 6 Feb 2025 11:32:26 -0800 Subject: [PATCH 1/8] time complexity answer done, need to work on rewrite --- src/Main.java | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Main.java b/src/Main.java index 036c766..0b00afd 100644 --- a/src/Main.java +++ b/src/Main.java @@ -5,7 +5,7 @@ public class Main { - // The time complexity is: + // The time complexity is: n^2 // YOUR ANSWER HERE public static void timesTable(int x) { for(int i = 1; i <= x; i++) { @@ -16,7 +16,7 @@ public static void timesTable(int x) { } } - // The time complexity is: + // The time complexity is: n // YOUR ANSWER HERE public static void printLetters(String word) { char[] letters = word.toCharArray(); @@ -26,7 +26,7 @@ public static void printLetters(String word) { } } - // The time complexity is: + // The time complexity is: n // YOUR ANSWER HERE public static boolean isBanned(String password) { String[] bannedPasswords = {"password", "hello", "qwerty"}; @@ -40,7 +40,7 @@ public static boolean isBanned(String password) { } - // The time complexity is: + // The time complexity is: n // YOUR ANSWER HERE public static int computeProduct(int[] nums) { int total = 1; @@ -50,7 +50,7 @@ public static int computeProduct(int[] nums) { return total; } - // The time complexity is: + // The time complexity is: n // YOUR ANSWER HERE public static void describeProduct(int[] nums) { System.out.println("About to compute the product of the array..."); @@ -59,7 +59,7 @@ public static void describeProduct(int[] nums) { } - // The time complexity is: + // The time complexity is: n // YOUR ANSWER HERE public static int computeFactorial(int n) { int result = 1; @@ -71,6 +71,7 @@ public static int computeFactorial(int n) { // Assume that the largest number is no bigger than the length // of the array + // The time complexity is: n^2 public static void computeAllFactorials(int[] nums) { for(int num : nums) { int result = computeFactorial(num); @@ -80,7 +81,7 @@ public static void computeAllFactorials(int[] nums) { // assume that each String is bounded by a constant length - // The time complexity is: + // The time complexity is: n // YOUR ANSWER HERE public static void checkIfContainedArrayList(ArrayList arr, String target) { if (arr.contains(target)) { @@ -93,7 +94,7 @@ public static void checkIfContainedArrayList(ArrayList arr, String targe // assume n = wordsA.length = wordsB.length // assume that each String is bounded by a constant length - // The time complexity is: + // The time complexity is: n^2 // YOUR ANSWER HERE public static boolean containsOverlap(String[] wordsA, String[] wordsB) { for(String wordA : wordsA) { @@ -107,7 +108,7 @@ public static boolean containsOverlap(String[] wordsA, String[] wordsB) { } // assume that each String is bounded by a constant length - // The time complexity is: + // The time complexity is: n // YOUR ANSWER HERE public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { Set wordsSet = new HashSet<>(); @@ -124,7 +125,7 @@ public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { return false; } - // The time complexity is: + // The time complexity is: n // YOUR ANSWER HERE public static void printCharacters(char[] chars) { for (int i = 0; i < chars.length; i++) { @@ -132,14 +133,14 @@ public static void printCharacters(char[] chars) { System.out.println("The character at index " + i + " is " + character); } } - // The time complexity is: + // The time complexity is: 1 // YOUR ANSWER HERE public static double computeAverage(double a, double b) { return (a + b) / 2.0; } // assume that each String is bounded by a constant length - // The time complexity is: + // The time complexity is: 1 // YOUR ANSWER HERE public static void checkIfContainedHashSet(HashSet set, String target) { @@ -157,6 +158,7 @@ public static void checkIfContainedHashSet(HashSet set, String target) // assume that each String is bounded by a constant length // What is the time complexity of this method? // YOUR ANSWER HERE + // The time complexity is: n public static String emailLookup(String[] names, String[] emails, String queryName) { for(int i = 0; i < names.length; i++) { if (names[i].equals(queryName)) { @@ -181,6 +183,7 @@ public static String emailLookupEfficient(HashMap namesToEmails, // assume that each String is bounded by a constant length // (assume the set and list have the same number of elements) // YOUR ANSWER HERE + // The time complexity is: n public static boolean hasCommon(HashSet wordSet, ArrayList wordList) { for(String word : wordSet) { if(wordList.contains(word)) { From 01321ac731a1dc7efac53bd6a40a89a2b08b9be8 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Thu, 6 Feb 2025 11:34:10 -0800 Subject: [PATCH 2/8] change the default return --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 0b00afd..386cca1 100644 --- a/src/Main.java +++ b/src/Main.java @@ -176,7 +176,7 @@ public static String emailLookup(String[] names, String[] emails, String queryNa // What is the time complexity of your solution? // YOUR ANSWER HERE public static String emailLookupEfficient(HashMap namesToEmails, String queryName) { - return null; + return "Person not found"; } // What is the time complexity of this method? From 1e139135cd82003f8e1e8252f3e151a517ca6c7e Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Thu, 6 Feb 2025 11:36:41 -0800 Subject: [PATCH 3/8] eLE logic done --- src/Main.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Main.java b/src/Main.java index 386cca1..ca72300 100644 --- a/src/Main.java +++ b/src/Main.java @@ -176,6 +176,9 @@ public static String emailLookup(String[] names, String[] emails, String queryNa // What is the time complexity of your solution? // YOUR ANSWER HERE public static String emailLookupEfficient(HashMap namesToEmails, String queryName) { + if(namesToEmails.containsKey(queryName)){ + return namesToEmails.get(queryName); + } return "Person not found"; } From 1c6f5a9b93a264bac6ab7b16895d07f8e801d6a4 Mon Sep 17 00:00:00 2001 From: xavierb <194048107+xavierb117@users.noreply.github.com> Date: Thu, 6 Feb 2025 11:38:14 -0800 Subject: [PATCH 4/8] Added time complexity to emailLookupEfficient --- src/Main.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Main.java b/src/Main.java index ca72300..8ed0a7a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -175,6 +175,7 @@ public static String emailLookup(String[] names, String[] emails, String queryNa // assume that each String is bounded by a constant length // What is the time complexity of your solution? // YOUR ANSWER HERE + // The time complexity is: O(1) public static String emailLookupEfficient(HashMap namesToEmails, String queryName) { if(namesToEmails.containsKey(queryName)){ return namesToEmails.get(queryName); From 884a402ac0f004443e7a2fa9b2389cd52bb9165e Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Thu, 6 Feb 2025 11:40:45 -0800 Subject: [PATCH 5/8] update answer --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 8ed0a7a..a6b7486 100644 --- a/src/Main.java +++ b/src/Main.java @@ -187,7 +187,7 @@ public static String emailLookupEfficient(HashMap namesToEmails, // assume that each String is bounded by a constant length // (assume the set and list have the same number of elements) // YOUR ANSWER HERE - // The time complexity is: n + // The time complexity is: n^2 public static boolean hasCommon(HashSet wordSet, ArrayList wordList) { for(String word : wordSet) { if(wordList.contains(word)) { From 40ef9efc03ab811081a95851f0b91181948789ef Mon Sep 17 00:00:00 2001 From: xavierb <194048107+xavierb117@users.noreply.github.com> Date: Thu, 6 Feb 2025 11:43:55 -0800 Subject: [PATCH 6/8] Solved hasCommonEfficient --- src/Main.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Main.java b/src/Main.java index 8ed0a7a..d8da193 100644 --- a/src/Main.java +++ b/src/Main.java @@ -201,7 +201,13 @@ public static boolean hasCommon(HashSet wordSet, ArrayList wordL // assume that each String is bounded by a constant length // What is the time complexity of your new solution? // YOUR ANSWER HERE + // The time complexity is: O(n) public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { + for (String word : wordList) { + if (wordSet.contains(word)) { + return true; + } + } return false; } From a17ebaeb400f50f27518a6b68ef2be2b63a97e91 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Thu, 6 Feb 2025 11:57:07 -0800 Subject: [PATCH 7/8] answer done --- src/Main.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Main.java b/src/Main.java index 6325a88..93d1e61 100644 --- a/src/Main.java +++ b/src/Main.java @@ -218,6 +218,8 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordSet, ArrayList wordSet, ArrayList Date: Thu, 6 Feb 2025 11:57:55 -0800 Subject: [PATCH 8/8] Fixed HashSet to HashMap --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 93d1e61..87e9d38 100644 --- a/src/Main.java +++ b/src/Main.java @@ -218,7 +218,7 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList