From 25a619faa82ebb16f13e30ff3b2ffa8d0267087d Mon Sep 17 00:00:00 2001 From: Maxwell Maslov Date: Sat, 19 Oct 2024 10:31:09 -0700 Subject: [PATCH 1/3] Completed the time complexity analysis question, will work on the re-writing portions and scenario questions next. --- src/Main.java | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Main.java b/src/Main.java index 1ca5e5b..d1a4f33 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++) { @@ -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(); @@ -27,7 +27,7 @@ public static void printLetters(String word) { } // The time complexity is: - // YOUR ANSWER HERE + // O(1) public static boolean isBanned(String password) { String[] bannedPasswords = {"password", "hello", "qwerty"}; boolean banned = false; @@ -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++) { @@ -80,7 +80,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"); @@ -92,7 +92,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) { @@ -105,7 +105,7 @@ public static boolean containsOverlap(String[] wordsA, String[] wordsB) { } // The time complexity is: - // YOUR ANSWER HERE + // O(n) public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { Set wordsSet = new HashSet<>(); for(String word : wordsA) { @@ -122,7 +122,7 @@ 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]; @@ -130,12 +130,12 @@ 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 + // O(1) public static void checkIfContainedHashSet(HashSet set, String target) { if (set.contains(target)) { @@ -150,7 +150,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)) { @@ -165,14 +165,14 @@ 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) { return null; } // What is the time complexity of this method? // (assume the set and list have the same number of elements) - // YOUR ANSWER HERE + // O(n^2) public static boolean hasCommon(HashSet wordSet, ArrayList wordList) { for(String word : wordSet) { if(wordList.contains(word)) { From 65a8e4a8264eae5a6c8481c4e5d8097eb5cf1482 Mon Sep 17 00:00:00 2001 From: Maxwell Maslov Date: Sat, 19 Oct 2024 10:41:12 -0700 Subject: [PATCH 2/3] Implemented the emailLookupEfficient function --- src/Main.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index d1a4f33..78b8d91 100644 --- a/src/Main.java +++ b/src/Main.java @@ -167,7 +167,11 @@ public static String emailLookup(String[] names, String[] emails, String queryNa // What is the time complexity of your solution? // O(1) 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 9bc8c0d3d14128b8b5f49ca62936aaae721801cb Mon Sep 17 00:00:00 2001 From: Maxwell Maslov Date: Sat, 19 Oct 2024 11:15:56 -0700 Subject: [PATCH 3/3] Implemented the hasCommonEfficient function --- src/Main.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Main.java b/src/Main.java index 78b8d91..6dbd2f2 100644 --- a/src/Main.java +++ b/src/Main.java @@ -190,6 +190,12 @@ 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 wordListSet = new HashSet<>(wordList); + for (String word : wordSet) { + if (wordListSet.contains(word)) { + return true; + } + } return false; }