From 63fe2df4d2bbfa79df68882ad24b6cd45d6b1e22 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Oct 2024 21:15:07 -0700 Subject: [PATCH 01/14] Answered timesTables --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 1ca5e5b..9953891 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,7 +6,7 @@ public class Main { // The time complexity is: - // YOUR ANSWER HERE + // O(n^2) public static void timesTable(int x) { for(int i = 1; i <= x; i++) { for(int j = 1; j <= x; j++) { From 6663c8e7db9d137af8b54a598e995d6a1a243bcd Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Oct 2024 21:18:23 -0700 Subject: [PATCH 02/14] Answered printLetters --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 9953891..028f8ca 100644 --- a/src/Main.java +++ b/src/Main.java @@ -17,7 +17,7 @@ public static void timesTable(int x) { } // The time complexity is: - // YOUR ANSWER HERE + // O(n) public static void printLetters(String word) { char[] letters = word.toCharArray(); From cf237e42c839570cf3b8b116907a5b27b8ff1fd9 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Oct 2024 21:24:07 -0700 Subject: [PATCH 03/14] Answered isBanned --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 028f8ca..277cb07 100644 --- a/src/Main.java +++ b/src/Main.java @@ -27,7 +27,7 @@ public static void printLetters(String word) { } // The time complexity is: - // YOUR ANSWER HERE + // O(n) public static boolean isBanned(String password) { String[] bannedPasswords = {"password", "hello", "qwerty"}; boolean banned = false; From 7f9b124e2fe1bd90c6f63be0602c31343799b182 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Oct 2024 21:30:13 -0700 Subject: [PATCH 04/14] Answered computeProduct, describeProduct, computeFactorial --- src/Main.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Main.java b/src/Main.java index 277cb07..2a74720 100644 --- a/src/Main.java +++ b/src/Main.java @@ -41,7 +41,7 @@ public static boolean isBanned(String password) { // The time complexity is: - // YOUR ANSWER HERE + // O(n) public static int computeProduct(int[] nums) { int total = 1; for(int num : nums) { @@ -51,7 +51,7 @@ public static int computeProduct(int[] nums) { } // The time complexity is: - // YOUR ANSWER HERE + // O(n) public static void describeProduct(int[] nums) { System.out.println("About to compute the product of the array..."); int product = computeProduct(nums); @@ -60,7 +60,7 @@ public static void describeProduct(int[] nums) { // The time complexity is: - // YOUR ANSWER HERE + // O(n) public static int computeFactorial(int n) { int result = 1; for(int i = 1; i <= n; i++) { From 1948ca86885ff2e053595ce858af9800eea99bff Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Oct 2024 21:33:59 -0700 Subject: [PATCH 05/14] Answered computeAllFactorials, checkIfContainedArrayList --- src/Main.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 2a74720..c05f6ce 100644 --- a/src/Main.java +++ b/src/Main.java @@ -71,6 +71,7 @@ public static int computeFactorial(int n) { // Assume that the largest number is no bigger than the length // of the array + //O(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) { // The time complexity is: - // YOUR ANSWER HERE + // O(n) public static void checkIfContainedArrayList(ArrayList arr, String target) { if (arr.contains(target)) { System.out.println(target + " is present in the list"); From adcc23475c4fa6e046d7a2ab03fddbf1c73979e6 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Oct 2024 21:39:57 -0700 Subject: [PATCH 06/14] Answered checkIfContainedArrayList, containsOverlap, containedOverlap2, printCharacters --- src/Main.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Main.java b/src/Main.java index c05f6ce..1a22354 100644 --- a/src/Main.java +++ b/src/Main.java @@ -93,7 +93,7 @@ public static void checkIfContainedArrayList(ArrayList arr, String targe // assume n = wordsA.length = wordsB.length // The time complexity is: - // YOUR ANSWER HERE + // O(n^2) public static boolean containsOverlap(String[] wordsA, String[] wordsB) { for(String wordA : wordsA) { for(String wordB : wordsB) { @@ -106,7 +106,7 @@ public static boolean containsOverlap(String[] wordsA, String[] wordsB) { } // The time complexity is: - // YOUR ANSWER HERE + // O(n^2) public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { Set wordsSet = new HashSet<>(); for(String word : wordsA) { @@ -123,13 +123,14 @@ public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { } // The time complexity is: - // YOUR ANSWER HERE + // O(n) public static void printCharacters(char[] chars) { for (int i = 0; i < chars.length; i++) { char character = chars[i]; System.out.println("The character at index " + i + " is " + character); } } + // The time complexity is: // YOUR ANSWER HERE public static double computeAverage(double a, double b) { From 4f081fc5981302bac2560325bd20ff3573fdee97 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Oct 2024 21:49:25 -0700 Subject: [PATCH 07/14] Answered computeAverage --- src/Main.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 1a22354..9483514 100644 --- a/src/Main.java +++ b/src/Main.java @@ -132,10 +132,11 @@ public static void printCharacters(char[] chars) { } // The time complexity is: - // YOUR ANSWER HERE + // O(1) public static double computeAverage(double a, double b) { return (a + b) / 2.0; } + // The time complexity is: // YOUR ANSWER HERE public static void checkIfContainedHashSet(HashSet set, String target) From 4047555641ceda2dbbfa2889357698eb513d1a76 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Oct 2024 21:55:18 -0700 Subject: [PATCH 08/14] Answered checkIfContainedHashSet --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 9483514..273f48c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -138,7 +138,7 @@ public static double computeAverage(double a, double b) { } // The time complexity is: - // YOUR ANSWER HERE + // O(1) public static void checkIfContainedHashSet(HashSet set, String target) { if (set.contains(target)) { From d2b4c95754af9f860751b001da3db50c7b62e07f Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Oct 2024 22:03:40 -0700 Subject: [PATCH 09/14] Answered emailLookup --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 273f48c..8b14b74 100644 --- a/src/Main.java +++ b/src/Main.java @@ -153,7 +153,7 @@ public static void checkIfContainedHashSet(HashSet set, String target) // A queryName is given, and this method returns the corresponding email if it is found // Otherwise, it returns "Person not found" // What is the time complexity of this method? - // YOUR ANSWER HERE + // O(n) public static String emailLookup(String[] names, String[] emails, String queryName) { for(int i = 0; i < names.length; i++) { if (names[i].equals(queryName)) { From 757ad8ff00166f787f3cf864f9bb655eedd402ae Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Oct 2024 22:14:54 -0700 Subject: [PATCH 10/14] Implemented method in emailLookupEfficient --- src/Main.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 8b14b74..688b703 100644 --- a/src/Main.java +++ b/src/Main.java @@ -170,7 +170,11 @@ 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; + if (namesToEmails.containsKey(queryName)) { + return namesToEmails.get(queryName); + } else { + return "Person not found"; + } } // What is the time complexity of this method? From 7ff415d905d2d14044e5de265cf1f8aae103cec6 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Oct 2024 22:17:28 -0700 Subject: [PATCH 11/14] Added time complexity answer --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 688b703..beeeba2 100644 --- a/src/Main.java +++ b/src/Main.java @@ -168,7 +168,7 @@ public static String emailLookup(String[] names, String[] emails, String queryNa // keys are names and the values are emails. // Write this method to efficiently return the corresponding email or "Person not found" if appropriate // What is the time complexity of your solution? - // YOUR ANSWER HERE + // O(1) public static String emailLookupEfficient(HashMap namesToEmails, String queryName) { if (namesToEmails.containsKey(queryName)) { return namesToEmails.get(queryName); From 56095f2fba4eb5bbd607f8137f23a980b39529fd Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Oct 2024 23:12:53 -0700 Subject: [PATCH 12/14] Added HashSet in hasCommonEfficient --- src/Main.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index beeeba2..ac659c6 100644 --- a/src/Main.java +++ b/src/Main.java @@ -179,7 +179,7 @@ public static String emailLookupEfficient(HashMap namesToEmails, // What is the time complexity of this method? // (assume the set and list have the same number of elements) - // YOUR ANSWER HERE + // O(n) public static boolean hasCommon(HashSet wordSet, ArrayList wordList) { for(String word : wordSet) { if(wordList.contains(word)) { @@ -188,11 +188,25 @@ public static boolean hasCommon(HashSet wordSet, ArrayList wordL } return false; } + // Rewrite hasCommon so it does the same thing as hasCommon, but with a better time complexity. // Do not change the datatype of wordSet or wordList. // What is the time complexity of your new solution? // YOUR ANSWER HERE public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { + + HashSet wordListSet = new HashSet<>(wordList); + + + for( String word : wordSet) { + + + if(wordListSet.contains(word)) { + + return true; + } + } + return false; } From 576896f22ffcf1b741658409ac96aff9f1963ce0 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Oct 2024 23:18:08 -0700 Subject: [PATCH 13/14] Added comments to hasCommonEfficient --- src/Main.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Main.java b/src/Main.java index ac659c6..3745066 100644 --- a/src/Main.java +++ b/src/Main.java @@ -194,19 +194,19 @@ public static boolean hasCommon(HashSet wordSet, ArrayList wordL // What is the time complexity of your new solution? // YOUR ANSWER HERE public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { - + //HashSet for faster read HashSet wordListSet = new HashSet<>(wordList); - + //For every word in wordSet for( String word : wordSet) { - + //Check if the word exists in wordList if(wordListSet.contains(word)) { - + //Return true if word exists in WordList return true; } } - + // return false if none are found return false; } From c93dfd21f8fef7b5ceecc74acd676e3b082dc7f6 Mon Sep 17 00:00:00 2001 From: LauraV702 Date: Tue, 22 Oct 2024 23:30:47 -0700 Subject: [PATCH 14/14] Answered last 3 question --- src/Main.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Main.java b/src/Main.java index 3745066..0a36a34 100644 --- a/src/Main.java +++ b/src/Main.java @@ -192,7 +192,7 @@ public static boolean hasCommon(HashSet wordSet, ArrayList wordL // Rewrite hasCommon so it does the same thing as hasCommon, but with a better time complexity. // Do not change the datatype of wordSet or wordList. // What is the time complexity of your new solution? - // YOUR ANSWER HERE + // O(1) public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { //HashSet for faster read HashSet wordListSet = new HashSet<>(wordList); @@ -215,14 +215,14 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordSet, ArrayList