From c48aa7b7a2003fc28c0d0a4ca180f318d581d150 Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Thu, 6 Feb 2025 11:29:46 -0800 Subject: [PATCH 1/7] Worked on first three problems. --- src/Main.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Main.java b/src/Main.java index 036c766..af16f3d 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++) { @@ -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(1) public static boolean isBanned(String password) { String[] bannedPasswords = {"password", "hello", "qwerty"}; boolean banned = false; From fb7d2ff02bac09a0f785a80008d5786dc844a78d Mon Sep 17 00:00:00 2001 From: Gabby-Moon <194023457+Gabby-Moon@users.noreply.github.com> Date: Thu, 6 Feb 2025 11:38:59 -0800 Subject: [PATCH 2/7] Completed next three problems --- src/Main.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Main.java b/src/Main.java index af16f3d..3217d9f 100644 --- a/src/Main.java +++ b/src/Main.java @@ -7,6 +7,7 @@ public class Main { // The time complexity is: // O(n^2) + // n = x 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: // O(n) + // n = letters in word public static void printLetters(String word) { char[] letters = word.toCharArray(); @@ -41,7 +43,8 @@ public static boolean isBanned(String password) { // The time complexity is: - // YOUR ANSWER HERE + // O(n) + // n = length of nums public static int computeProduct(int[] nums) { int total = 1; for(int num : nums) { @@ -51,7 +54,8 @@ public static int computeProduct(int[] nums) { } // The time complexity is: - // YOUR ANSWER HERE + // O(n) + // n = length of nums public static void describeProduct(int[] nums) { System.out.println("About to compute the product of the array..."); int product = computeProduct(nums); @@ -60,7 +64,8 @@ public static void describeProduct(int[] nums) { // The time complexity is: - // YOUR ANSWER HERE + // O(n) + // n = int n public static int computeFactorial(int n) { int result = 1; for(int i = 1; i <= n; i++) { From cfb95f5530d2c4958a51cd71d74d8ddce976e3a4 Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Thu, 6 Feb 2025 11:46:34 -0800 Subject: [PATCH 3/7] Completed next three problems. --- src/Main.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Main.java b/src/Main.java index 3217d9f..af3eb2b 100644 --- a/src/Main.java +++ b/src/Main.java @@ -76,6 +76,8 @@ public static int computeFactorial(int n) { // Assume that the largest number is no bigger than the length // of the array + // The time complexity is: + // O(n^2); n = length of nums, n within for each loop = int n within computeFactorial public static void computeAllFactorials(int[] nums) { for(int num : nums) { int result = computeFactorial(num); @@ -86,7 +88,8 @@ 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) + // n = the .contains method public static void checkIfContainedArrayList(ArrayList arr, String target) { if (arr.contains(target)) { System.out.println(target + " is present in the list"); @@ -99,7 +102,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) + // n = length of wordsA/length of words B public static boolean containsOverlap(String[] wordsA, String[] wordsB) { for(String wordA : wordsA) { for(String wordB : wordsB) { From 48c28dccca67a4918b524739b323d949e9f663d4 Mon Sep 17 00:00:00 2001 From: Gabby-Moon <194023457+Gabby-Moon@users.noreply.github.com> Date: Thu, 6 Feb 2025 11:53:16 -0800 Subject: [PATCH 4/7] Completed three more problems --- src/Main.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Main.java b/src/Main.java index af3eb2b..2c14627 100644 --- a/src/Main.java +++ b/src/Main.java @@ -117,7 +117,8 @@ 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) + // n = length of wordsA/ length of wordsB public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { Set wordsSet = new HashSet<>(); for(String word : wordsA) { @@ -134,7 +135,8 @@ public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { } // The time complexity is: - // YOUR ANSWER HERE + // O(n) + // n = length of chars public static void printCharacters(char[] chars) { for (int i = 0; i < chars.length; i++) { char character = chars[i]; @@ -142,7 +144,7 @@ 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; } From 16146c2648deb3f9631e5464bf6e0ec9b1dacaeb Mon Sep 17 00:00:00 2001 From: Maple <135763163+maple-johnson@users.noreply.github.com> Date: Mon, 10 Feb 2025 14:12:51 -0800 Subject: [PATCH 5/7] Completed next three problems. --- src/Main.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Main.java b/src/Main.java index 2c14627..97c1c05 100644 --- a/src/Main.java +++ b/src/Main.java @@ -151,7 +151,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)) { @@ -167,7 +167,8 @@ 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) + // n = names.length public static String emailLookup(String[] names, String[] emails, String queryName) { for(int i = 0; i < names.length; i++) { if (names[i].equals(queryName)) { @@ -183,9 +184,16 @@ 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 - public static String emailLookupEfficient(HashMap namesToEmails, String queryName) { - return null; + // O(1) + public static String emailLookupEfficient(HashMap namesToEmails, String queryName) + { + if (namesToEmails.containsKey(queryName)) + { + return namesToEmails.get(queryName); + } + + + return "Person not found"; } // What is the time complexity of this method? From 58e7bc85bfe92ad157f27c9561775c111452cc5d Mon Sep 17 00:00:00 2001 From: Gabby-Moon <194023457+Gabby-Moon@users.noreply.github.com> Date: Mon, 10 Feb 2025 14:29:42 -0800 Subject: [PATCH 6/7] Completed next three problems --- src/Main.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Main.java b/src/Main.java index 97c1c05..2fd6240 100644 --- a/src/Main.java +++ b/src/Main.java @@ -199,7 +199,8 @@ public static String emailLookupEfficient(HashMap namesToEmails, // 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) + // n = wordSet / wordList length public static boolean hasCommon(HashSet wordSet, ArrayList wordList) { for(String word : wordSet) { if(wordList.contains(word)) { @@ -212,8 +213,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) + // n = size of wordList public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { + for(int i = 0; i < wordList.size(); i++) { + if(wordSet.contains(wordList.get(i))) { + return true; + } + } return false; } @@ -222,7 +229,8 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList Date: Mon, 10 Feb 2025 14:38:14 -0800 Subject: [PATCH 7/7] Completed the last two problems. --- src/Main.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Main.java b/src/Main.java index 2fd6240..66c99ee 100644 --- a/src/Main.java +++ b/src/Main.java @@ -237,7 +237,8 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordSet, ArrayList