diff --git a/src/Main.java b/src/Main.java index 036c766..e72492f 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) where n is the value of x 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) where n is the length of the word 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) because the array is 3 passwords long meaning it will run 3 times 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) where n is the length of the array 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) because computeProduct iterates over all n elements in the array 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) where n is the input n public static int computeFactorial(int n) { int result = 1; for(int i = 1; i <= n; i++) { @@ -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) because it's a for each looping through num*nums public static void computeAllFactorials(int[] nums) { for(int num : nums) { int result = computeFactorial(num); @@ -81,7 +82,7 @@ public static void computeAllFactorials(int[] nums) { // assume that each String is bounded by a constant length // The time complexity is: - // YOUR ANSWER HERE + // O(n) where n is the length of the ArrayList public static void checkIfContainedArrayList(ArrayList arr, String target) { if (arr.contains(target)) { System.out.println(target + " is present in the list"); @@ -94,7 +95,8 @@ 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 + // O(n^2) where n is length of wordsA & wordsB because they + // are equal in length public static boolean containsOverlap(String[] wordsA, String[] wordsB) { for(String wordA : wordsA) { for(String wordB : wordsB) { @@ -108,7 +110,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 + // O(n) where n is the length of the array public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { Set wordsSet = new HashSet<>(); for(String word : wordsA) { @@ -125,7 +127,7 @@ public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { } // The time complexity is: - // YOUR ANSWER HERE + // O(n) where n is the length of the array public static void printCharacters(char[] chars) { for (int i = 0; i < chars.length; i++) { char character = chars[i]; @@ -133,14 +135,14 @@ public static void printCharacters(char[] chars) { } } // The time complexity is: - // YOUR ANSWER HERE + // O(1) because it is just a basic math problem 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 + // O(1) because .contains with a hashset is constant public static void checkIfContainedHashSet(HashSet set, String target) { if (set.contains(target)) { @@ -156,7 +158,9 @@ 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 + + // Would it be O(n) where n is the length of the names array because in the + // for loop we iterate names.length amount of times public static String emailLookup(String[] names, String[] emails, String queryName) { for(int i = 0; i < names.length; i++) { if (names[i].equals(queryName)) { @@ -172,15 +176,20 @@ 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 + // Since it's a hashmap i think it would be O(1) because .constainsKey is constant 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) + // Outer loop is the hashset which is O(n) and then we multiply + // it by the inner loop where the ArrayList is which is also O(n) public static boolean hasCommon(HashSet wordSet, ArrayList wordList) { for(String word : wordSet) { if(wordList.contains(word)) { @@ -193,8 +202,16 @@ public static boolean hasCommon(HashSet wordSet, ArrayList wordL // 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) + // When we flip it so the iteration runs through the ArrayList + // first that becomes O(n) and then when we run .contains on + // the hashset which is O(1) which would be O(n)+O(1) public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { + for(String word : wordList){ + if (wordSet.contains(word)){ + return true; + } + } return false; } @@ -203,14 +220,16 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList because it is mentioned we want the ticker symbol as the key // Suppose you are building a music player application where users can create playlists. // Songs can be added to the end of the playlist in the order the user chooses, and the user can // skip to the next or previous song. Most operations involve adding songs and accessing them by // their position in the playlist. // What would be a good choice of data structure? - // YOUR ANSWER HERE + // ArrayList I think an array list is best because it is dynamic and will + // resize as we add new songs and it allows the user to access the song by + // it's position // Suppose you are developing a search feature that keeps track of the user's // recent search queries. You want to store the queries in the order they were made, @@ -218,5 +237,5 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList since it would allow us to keep the insertion order in tact. } \ No newline at end of file