diff --git a/README.md b/README.md index d96b4a6..2a8f739 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# complexity-practice +# complexity-practice (Alston Dsouza) An exercise in analyzing complexity, writing efficient methods, and choosing appropriate data structures. diff --git a/src/Main.java b/src/Main.java index 1ca5e5b..2776c48 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,7 +6,7 @@ public class Main { // The time complexity is: - // YOUR ANSWER HERE + // 0(x^2) 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 + // 0(n) where n is the length of the string 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 + // 0(m) where m is the number of banned passwords 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 + // 0(n) where n is the length of the array nums 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 + // 0(n), same as computeProduct public static void describeProduct(int[] nums) { System.out.println("About to compute the product of the array..."); int product = computeProduct(nums); @@ -60,17 +60,18 @@ public static void describeProduct(int[] nums) { // The time complexity is: - // YOUR ANSWER HERE + // 0(n) where n is the input value public static int computeFactorial(int n) { int result = 1; for(int i = 1; i <= n; i++) { - result *= n; + result *= i; } return result; } // Assume that the largest number is no bigger than the length // of the array + // 0(n * k) where n is the length of nums, and k is the value of the largest number public static void computeAllFactorials(int[] nums) { for(int num : nums) { int result = computeFactorial(num); @@ -80,7 +81,7 @@ public static void computeAllFactorials(int[] nums) { // The time complexity is: - // YOUR ANSWER HERE + // 0(n) where n is the size of the array list public static void checkIfContainedArrayList(ArrayList arr, String target) { if (arr.contains(target)) { System.out.println(target + " is present in the list"); @@ -92,7 +93,7 @@ public static void checkIfContainedArrayList(ArrayList arr, String targe // assume n = wordsA.length = wordsB.length // The time complexity is: - // YOUR ANSWER HERE + // 0(n^2) where n is the lenght of the arrays public static boolean containsOverlap(String[] wordsA, String[] wordsB) { for(String wordA : wordsA) { for(String wordB : wordsB) { @@ -105,7 +106,7 @@ public static boolean containsOverlap(String[] wordsA, String[] wordsB) { } // The time complexity is: - // YOUR ANSWER HERE + // 0(n) where n is the length of wordsA public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { Set wordsSet = new HashSet<>(); for(String word : wordsA) { @@ -122,7 +123,7 @@ public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { } // The time complexity is: - // YOUR ANSWER HERE + // 0(n) where n is the length of the chars array public static void printCharacters(char[] chars) { for (int i = 0; i < chars.length; i++) { char character = chars[i]; @@ -130,12 +131,12 @@ public static void printCharacters(char[] chars) { } } // The time complexity is: - // YOUR ANSWER HERE + // 0(1) since only two variables are involved public static double computeAverage(double a, double b) { return (a + b) / 2.0; } // The time complexity is: - // YOUR ANSWER HERE + // 0(1) since HashSet operations (contains) are 0(1) on average public static void checkIfContainedHashSet(HashSet set, String target) { if (set.contains(target)) { @@ -150,7 +151,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 + // 0(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)) { @@ -165,14 +166,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 + // 0(1) since HashMap operations get are 0(1) on average public static String emailLookupEfficient(HashMap namesToEmails, String queryName) { return null; } // What is the time complexity of this method? // (assume the set and list have the same number of elements) - // YOUR ANSWER HERE + // 0(n * m) where n is the size of the set and m is the size of the list public static boolean hasCommon(HashSet wordSet, ArrayList wordList) { for(String word : wordSet) { if(wordList.contains(word)) { @@ -184,7 +185,7 @@ 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. // What is the time complexity of your new solution? - // YOUR ANSWER HERE + // 0(n + m), where n is the size of the set and m is the size of the size of the list public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { return false; } @@ -194,14 +195,16 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList + // HashMap allows for efficient update and lookups (0)1) on average. // 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 + // A good choice of data structure for playlists would be: LinkedList + // LinkedList allows for efficient insertion at the end and traversal (0)1) insertion, 0(n) access). // 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, @@ -209,5 +212,6 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList + // ArrayList allows for fast iteration and maintains order of insertion. } \ No newline at end of file