From 88314c61f397bb0567a9f2e4ccd9ee97bd371927 Mon Sep 17 00:00:00 2001 From: HumaGitGud Date: Tue, 22 Oct 2024 00:26:47 -0700 Subject: [PATCH 1/2] Almost finished the assignment. Need to double check the answers --- src/Main.java | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/Main.java b/src/Main.java index 1ca5e5b..7431b06 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,12 +1,13 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; 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 +18,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 +28,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; @@ -41,7 +42,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 +52,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 +61,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++) { @@ -71,6 +72,7 @@ public static int computeFactorial(int n) { // Assume that the largest number is no bigger than the length // of the array + // O(N) public static void computeAllFactorials(int[] nums) { for(int num : nums) { int result = computeFactorial(num); @@ -80,7 +82,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 +94,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 +107,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 +124,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 +132,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 +152,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 +167,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; + return namesToEmails.getOrDefault(queryName, "Person not found"); // Citation: learned at W3schools - HashMap methods } // What is the time complexity of this method? // (assume the set and list have the same number of elements) - // YOUR ANSWER HERE + // O(N*M) public static boolean hasCommon(HashSet wordSet, ArrayList wordList) { for(String word : wordSet) { if(wordList.contains(word)) { @@ -181,11 +183,17 @@ 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 + // O(N) public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { + for (String word : wordList) { + if (wordSet.contains(word)) { + return true; + } + } return false; } @@ -194,14 +202,14 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordSet, ArrayList Date: Wed, 23 Oct 2024 12:35:27 -0700 Subject: [PATCH 2/2] Finished the assignment by answering all quesions --- src/Main.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Main.java b/src/Main.java index 7431b06..78280fa 100644 --- a/src/Main.java +++ b/src/Main.java @@ -52,7 +52,7 @@ public static int computeProduct(int[] nums) { } // The time complexity is: - // O(N) + // O(N) becaues method uses another method that executes a loop public static void describeProduct(int[] nums) { System.out.println("About to compute the product of the array..."); int product = computeProduct(nums); @@ -72,7 +72,7 @@ public static int computeFactorial(int n) { // Assume that the largest number is no bigger than the length // of the array - // O(N) + // O(N*M) because uses another method with O(N) public static void computeAllFactorials(int[] nums) { for(int num : nums) { int result = computeFactorial(num); @@ -82,7 +82,7 @@ public static void computeAllFactorials(int[] nums) { // The time complexity is: - // O(N) + // O(N) because method has contains function public static void checkIfContainedArrayList(ArrayList arr, String target) { if (arr.contains(target)) { System.out.println(target + " is present in the list"); @@ -107,7 +107,7 @@ public static boolean containsOverlap(String[] wordsA, String[] wordsB) { } // The time complexity is: - // O(N) + // O(N*M) 2 loops public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { Set wordsSet = new HashSet<>(); for(String word : wordsA) { @@ -174,7 +174,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) - // O(N*M) + // O(N*M) because method has contains function public static boolean hasCommon(HashSet wordSet, ArrayList wordList) { for(String word : wordSet) { if(wordList.contains(word)) {