From bd86c3772f90c95e4223b990c0f2a7af45ba00d3 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Thu, 6 Feb 2025 11:30:12 -0800 Subject: [PATCH 1/6] first question --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 036c766..81592f5 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) public static void timesTable(int x) { for(int i = 1; i <= x; i++) { for(int j = 1; j <= x; j++) { From f805eb6b17066594373ee3406b74c353e5578187 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Thu, 6 Feb 2025 11:36:16 -0800 Subject: [PATCH 2/6] remembered to add what is n --- src/Main.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Main.java b/src/Main.java index 81592f5..79fb776 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,7 +6,7 @@ public class Main { // The time complexity is: - // O(n^2) + // O(n^2) where n is the number of times the loop runs (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) 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(n^2) where n is the number of passwords in the String[] 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 number of items in the int[] public static int computeProduct(int[] nums) { int total = 1; for(int num : nums) { From 5f5f2174d815d5b16e9f2fdb417433db8f445795 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Thu, 6 Feb 2025 12:57:28 -0800 Subject: [PATCH 3/6] a few more we collaborated on in class --- src/Main.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Main.java b/src/Main.java index 79fb776..362728c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -51,7 +51,8 @@ public static int computeProduct(int[] nums) { } // The time complexity is: - // YOUR ANSWER HERE + // O(n) where n is the number of items in array nums + // (which is passed to the (n) function computeProduct) public static void describeProduct(int[] nums) { System.out.println("About to compute the product of the array..."); int product = computeProduct(nums); @@ -60,7 +61,7 @@ public static void describeProduct(int[] nums) { // The time complexity is: - // YOUR ANSWER HERE + // O(n) where n is the number of times the for loop will run public static int computeFactorial(int n) { int result = 1; for(int i = 1; i <= n; i++) { @@ -71,10 +72,11 @@ public static int computeFactorial(int n) { // Assume that the largest number is no bigger than the length // of the array + // O(n^2) (O(n)*O(n)) where n is the length of the nums array public static void computeAllFactorials(int[] nums) { - for(int num : nums) { - int result = computeFactorial(num); - System.out.println("The factorial of " + num + " is " + result); + for(int num : nums) { //O(n) + int result = computeFactorial(num); //O(n) + System.out.println("The factorial of " + num + " is " + result); //(1) } } From 922539bf1e3b28584b81ef20f58f3e368aee5a73 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Thu, 6 Feb 2025 13:51:12 -0800 Subject: [PATCH 4/6] outside of class collaboration --- src/Main.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Main.java b/src/Main.java index 362728c..1eddff1 100644 --- a/src/Main.java +++ b/src/Main.java @@ -83,7 +83,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) because it is a list it has to go through every item public static void checkIfContainedArrayList(ArrayList arr, String target) { if (arr.contains(target)) { System.out.println(target + " is present in the list"); @@ -96,7 +96,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 + // O(n^2) each loop will operate O(n) times, so O(n)*O(n)=O(n^2) public static boolean containsOverlap(String[] wordsA, String[] wordsB) { for(String wordA : wordsA) { for(String wordB : wordsB) { @@ -110,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) each loop will run O(n) times O(N)+O(n)=O(n) public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { Set wordsSet = new HashSet<>(); for(String word : wordsA) { @@ -127,7 +127,7 @@ public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { } // The time complexity is: - // YOUR ANSWER HERE + // O(n) where n is the number of characters in the array public static void printCharacters(char[] chars) { for (int i = 0; i < chars.length; i++) { char character = chars[i]; @@ -135,14 +135,14 @@ public static void printCharacters(char[] chars) { } } // The time complexity is: - // YOUR ANSWER HERE + // 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 + // O(1) because contains for a hashset is O(1) and there is not a loop public static void checkIfContainedHashSet(HashSet set, String target) { if (set.contains(target)) { @@ -158,7 +158,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 number of items in 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)) { From f2dbd49cd3136152a70ca5649d15821f65c15ec2 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Thu, 6 Feb 2025 14:05:10 -0800 Subject: [PATCH 5/6] more examples --- src/Main.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Main.java b/src/Main.java index 1eddff1..d65c6c7 100644 --- a/src/Main.java +++ b/src/Main.java @@ -174,15 +174,19 @@ 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) becuase .get and .contains are both O(1) operations for a hashmap 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 the loop is O(n) for each item in the wordSet and the wordList.contains is O(n) public static boolean hasCommon(HashSet wordSet, ArrayList wordList) { for(String word : wordSet) { if(wordList.contains(word)) { @@ -197,6 +201,8 @@ public static boolean hasCommon(HashSet wordSet, ArrayList wordL // What is the time complexity of your new solution? // YOUR ANSWER HERE public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { + + return false; } From 5f6e4e38e9c392098de70ef7cd4e16ffacd46f32 Mon Sep 17 00:00:00 2001 From: AAshGray <193518051+AAshGray@users.noreply.github.com> Date: Thu, 6 Feb 2025 14:25:12 -0800 Subject: [PATCH 6/6] last example --- src/Main.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Main.java b/src/Main.java index d65c6c7..57b84f8 100644 --- a/src/Main.java +++ b/src/Main.java @@ -199,9 +199,14 @@ 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) since .contains for hashSet is complexity O(1)*O(n)for the for each loop + // where n is the number of items in the loop public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { - + for(String word : wordList) { + if(wordSet.contains(word)) { + return true; + } + } return false; } @@ -211,14 +216,14 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordSet, ArrayList