diff --git a/src/Main.java b/src/Main.java index 036c766..21aaae1 100644 --- a/src/Main.java +++ b/src/Main.java @@ -5,8 +5,7 @@ public class Main { - // The time complexity is: - // YOUR ANSWER HERE + // The time complexity is: o(x^2) public static void timesTable(int x) { for(int i = 1; i <= x; i++) { for(int j = 1; j <= x; j++) { @@ -16,8 +15,7 @@ public static void timesTable(int x) { } } - // The time complexity is: - // YOUR ANSWER HERE + // The time complexity is: o(n) where n is the length of the word public static void printLetters(String word) { char[] letters = word.toCharArray(); @@ -26,8 +24,7 @@ public static void printLetters(String word) { } } - // The time complexity is: - // YOUR ANSWER HERE + // The time complexity is: o(1) public static boolean isBanned(String password) { String[] bannedPasswords = {"password", "hello", "qwerty"}; boolean banned = false; @@ -40,8 +37,7 @@ public static boolean isBanned(String password) { } - // The time complexity is: - // YOUR ANSWER HERE + // The time complexity is: o(n) where n is the size of the array public static int computeProduct(int[] nums) { int total = 1; for(int num : nums) { @@ -50,8 +46,7 @@ public static int computeProduct(int[] nums) { return total; } - // The time complexity is: - // YOUR ANSWER HERE + // The time complexity is: o(n) public static void describeProduct(int[] nums) { System.out.println("About to compute the product of the array..."); int product = computeProduct(nums); @@ -59,8 +54,7 @@ public static void describeProduct(int[] nums) { } - // The time complexity is: - // YOUR ANSWER HERE + // The time complexity is: o(n) public static int computeFactorial(int n) { int result = 1; for(int i = 1; i <= n; i++) { @@ -69,6 +63,7 @@ public static int computeFactorial(int n) { return result; } + // The time complexity is: o(n*m) where m is the length of the array // Assume that the largest number is no bigger than the length // of the array public static void computeAllFactorials(int[] nums) { @@ -80,8 +75,7 @@ public static void computeAllFactorials(int[] nums) { // assume that each String is bounded by a constant length - // The time complexity is: - // YOUR ANSWER HERE + // The time complexity is: o(n) where n is the length of the array public static void checkIfContainedArrayList(ArrayList arr, String target) { if (arr.contains(target)) { System.out.println(target + " is present in the list"); @@ -93,8 +87,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: - // YOUR ANSWER HERE + // The time complexity is: o(n^2) where n is the length of the arrays public static boolean containsOverlap(String[] wordsA, String[] wordsB) { for(String wordA : wordsA) { for(String wordB : wordsB) { @@ -107,8 +100,7 @@ public static boolean containsOverlap(String[] wordsA, String[] wordsB) { } // assume that each String is bounded by a constant length - // The time complexity is: - // YOUR ANSWER HERE + // The time complexity is: o(n+m) where n is the length of wordsA[] and m is the length of wordsB[] public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { Set wordsSet = new HashSet<>(); for(String word : wordsA) { @@ -124,23 +116,21 @@ public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { return false; } - // The time complexity is: - // YOUR ANSWER HERE + // The time complexity is: o(n) where n is the length of chars array 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 + + // The time complexity is: o(1) 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: - // YOUR ANSWER HERE + // The time complexity is: o(1) public static void checkIfContainedHashSet(HashSet set, String target) { if (set.contains(target)) { @@ -156,7 +146,7 @@ public static void checkIfContainedHashSet(HashSet set, String target) // Otherwise, it returns "Person not found" // assume that each String is bounded by a constant length // What is the time complexity of this method? - // YOUR ANSWER HERE + // o(n) where n is the length of the names array public static String emailLookup(String[] names, String[] emails, String queryName) { for(int i = 0; i < names.length; i++) { if (names[i].equals(queryName)) { @@ -172,29 +162,40 @@ public static String emailLookup(String[] names, String[] emails, String queryNa // Write this method to efficiently return the corresponding email or "Person not found" if appropriate // assume that each String is bounded by a constant length // What is the time complexity of your solution? - // YOUR ANSWER HERE + // o(1) public static String emailLookupEfficient(HashMap namesToEmails, String queryName) { - return null; + if (namesToEmails.containsKey(queryName)) { + return namesToEmails.get(queryName); + } + return "Person not found"; } // What is the time complexity of this method? // assume that each String is bounded by a constant length // (assume the set and list have the same number of elements) - // YOUR ANSWER HERE + // o(n^2) where n is length of the array public static boolean hasCommon(HashSet wordSet, ArrayList wordList) { for(String word : wordSet) { if(wordList.contains(word)) { 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. // assume that each String is bounded by a constant length // 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; } @@ -203,14 +204,14 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordSet, ArrayList