From bce52581f3da4a4e73661a36e3dd926cd5ebc7cf Mon Sep 17 00:00:00 2001 From: Juno Arciaga Date: Thu, 24 Oct 2024 01:24:31 -0700 Subject: [PATCH] Completed complecity-practice assignement --- src/Main.java | 125 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 74 insertions(+), 51 deletions(-) diff --git a/src/Main.java b/src/Main.java index 1ca5e5b..a58e90c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,10 +6,10 @@ public class Main { // The time complexity is: - // YOUR ANSWER HERE - public static void timesTable(int x) { - for(int i = 1; i <= x; i++) { - for(int j = 1; j <= x; j++) { + // O(n^2) + public static void timesTable(int x) { + for(int i = 1; i <= x; i++) { // n + for(int j = 1; j <= x; j++) { // n System.out.print(i*j + " "); } System.out.println(); @@ -17,21 +17,21 @@ 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(); + char[] letters = word.toCharArray(); // O(n) - for (char letter : letters) { + for (char letter : letters) { //O(n) System.out.println(letter); } } // The time complexity is: - // YOUR ANSWER HERE + // O(n) public static boolean isBanned(String password) { String[] bannedPasswords = {"password", "hello", "qwerty"}; boolean banned = false; - for(String bannedPassword : bannedPasswords) { + for(String bannedPassword : bannedPasswords) { // O(n) if(password.equals(bannedPassword)) { banned = true; } @@ -41,48 +41,49 @@ 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) { - total *= num; + for(int num : nums) { // O(n) + total *= num; // O(1) } return total; } // 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); + System.out.println("About to compute the product of the array..."); // O(1) + int product = computeProduct(nums); // O(n) System.out.println("The product I found was " + product); } // 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++) { - result *= n; + for(int i = 1; i <= n; i++) { // O(n) + result *= n; // O(1) } return result; } // Assume that the largest number is no bigger than the length // of the array + // Answer: O(n^2) public static void computeAllFactorials(int[] nums) { - for(int num : nums) { - int result = computeFactorial(num); + for(int num : nums) { //O(n) + int result = computeFactorial(num); //O(n) System.out.println("The factorial of " + num + " is " + result); } } // The time complexity is: - // YOUR ANSWER HERE + // O(n) public static void checkIfContainedArrayList(ArrayList arr, String target) { - if (arr.contains(target)) { + if (arr.contains(target)) { // O(n) System.out.println(target + " is present in the list"); } else { System.out.println(target + " is not present in the list"); @@ -92,10 +93,10 @@ 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) { + for(String wordA : wordsA) { // n + for(String wordB : wordsB) { // n if(wordA.equals(wordB)) { return true; } @@ -105,15 +106,15 @@ 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<>(); + Set wordsSet = new HashSet<>(); //O(1) for(String word : wordsA) { - wordsSet.add(word); + wordsSet.add(word); // O(n) } for(String word : wordsB) { - if(wordsSet.contains(word)) { + if(wordsSet.contains(word)) { //O(1) return true; } } @@ -122,23 +123,25 @@ 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++) { + for (int i = 0; i < chars.length; i++) { // O(n) char character = chars[i]; - System.out.println("The character at index " + i + " is " + character); + System.out.println("The character at index " + i + " is " + character); //O(1) } } + // The time complexity is: - // YOUR ANSWER HERE + // O(1) public static double computeAverage(double a, double b) { - return (a + b) / 2.0; + return (a + b) / 2.0; // operations = O(1) } + // The time complexity is: - // YOUR ANSWER HERE + // O(1) public static void checkIfContainedHashSet(HashSet set, String target) { - if (set.contains(target)) { + if (set.contains(target)) { // O (1) System.out.println(target + " is present in the set"); } else { System.out.println(target + " is not present in the set"); @@ -150,10 +153,10 @@ 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 + // 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)) { + for(int i = 0; i < names.length; i++) { // O(n) + if (names[i].equals(queryName)) { // O(1) return emails[i]; } } @@ -165,27 +168,38 @@ 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 - public static String emailLookupEfficient(HashMap namesToEmails, String queryName) { - return null; + // YOUR ANSWER HERE: O(1) + public static String emailLookupEfficient(HashMap namesToEmails, String queryName) { // O(1) + String email = namesToEmails.get((queryName)); + if (email == null) { + return "Person not found"; + } else { + return email; + } } // What is the time complexity of this method? // (assume the set and list have the same number of elements) - // YOUR ANSWER HERE + // YOUR ANSWER HERE: O(n^2) public static boolean hasCommon(HashSet wordSet, ArrayList wordList) { - for(String word : wordSet) { - if(wordList.contains(word)) { + for(String word : wordSet) { // O(n) + if(wordList.contains(word)) { // O(n) return true; } } 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) { + // YOUR ANSWER HERE: O(n) + public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { // O(1) + for (String word: wordList) { + if (wordSet.contains(word)) { // O(n) + return true; + } + } return false; } @@ -194,14 +208,21 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordSet, ArrayList