diff --git a/src/Main.java b/src/Main.java index 036c766..628e81d 100644 --- a/src/Main.java +++ b/src/Main.java @@ -7,6 +7,7 @@ public class Main { // The time complexity is: // YOUR ANSWER HERE + //o(x^2) public static void timesTable(int x) { for(int i = 1; i <= x; i++) { for(int j = 1; j <= x; j++) { @@ -18,6 +19,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(); @@ -28,6 +30,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; @@ -42,6 +45,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) { @@ -52,6 +56,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); @@ -61,6 +66,8 @@ 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 +78,7 @@ public static int computeFactorial(int n) { // Assume that the largest number is no bigger than the length // of the array + //o(n^2) public static void computeAllFactorials(int[] nums) { for(int num : nums) { int result = computeFactorial(num); @@ -82,6 +90,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) public static void checkIfContainedArrayList(ArrayList arr, String target) { if (arr.contains(target)) { System.out.println(target + " is present in the list"); @@ -95,6 +104,7 @@ public static void checkIfContainedArrayList(ArrayList arr, String targe // assume that each String is bounded by a constant 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) { @@ -109,6 +119,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) public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { Set wordsSet = new HashSet<>(); for(String word : wordsA) { @@ -126,14 +137,16 @@ 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]; System.out.println("The character at index " + i + " is " + character); } } - // The time complexity is: + // The time complexity is // YOUR ANSWER HERE + //o(1) public static double computeAverage(double a, double b) { return (a + b) / 2.0; } @@ -141,6 +154,7 @@ public static double computeAverage(double a, double b) { // assume that each String is bounded by a constant length // The time complexity is: // YOUR ANSWER HERE + //o(1) public static void checkIfContainedHashSet(HashSet set, String target) { if (set.contains(target)) { @@ -155,8 +169,9 @@ 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" // assume that each String is bounded by a constant length - // What is the time complexity of this method? + // 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)) { @@ -173,14 +188,16 @@ public static String emailLookup(String[] names, String[] emails, String queryNa // 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; + return namesToEmails.getOrDefault(queryName, "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) public static boolean hasCommon(HashSet wordSet, ArrayList wordList) { for(String word : wordSet) { if(wordList.contains(word)) { @@ -192,18 +209,31 @@ public static boolean hasCommon(HashSet wordSet, ArrayList wordL // 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? + // What is the time complexity of your new solution? // YOUR ANSWER HERE + //o(n+m) public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { - return false; + // Create a HashSet from wordList for O(1) lookups + HashSet wordListSet = new HashSet<>(wordList); + + // Check if any word in wordSet is in wordListSet + for (String word : wordSet) { + if (wordListSet.contains(word)) { + return true; + } + } + + return false; } - + // Suppose you are building a dashboard that displays real-time stock prices. // You want to keep track of the current price of each stock, with the stock's ticker symbol as the key. // The prices will be updated frequently throughout the day, and you need to efficiently update // and access the current price for each stock. The order of the ticker symbols is not important. // What would be a good choice of data structure? // YOUR ANSWER HERE + //HashMap + // 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 @@ -211,6 +241,8 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordSet, ArrayList