From 6d1817be6c25dbc49ea369ae2390a282d91bda78 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 16:21:43 -0700 Subject: [PATCH 01/23] Finished timesTable time complexity --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 1ca5e5b..e624104 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) --> n*n = n^2 public static void timesTable(int x) { for(int i = 1; i <= x; i++) { for(int j = 1; j <= x; j++) { From 49d02c42006203e3ef7272f5582682c934fbce26 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 16:26:33 -0700 Subject: [PATCH 02/23] Finished printLetters time complexity --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index e624104..9f0f806 100644 --- a/src/Main.java +++ b/src/Main.java @@ -17,7 +17,7 @@ public static void timesTable(int x) { } // The time complexity is: - // YOUR ANSWER HERE + // O(N) -> n = length of the word public static void printLetters(String word) { char[] letters = word.toCharArray(); From 15abfb0a668aeee1b899a38456c18b563116707a Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 16:30:37 -0700 Subject: [PATCH 03/23] Finished isBanned time complexity and changed comments a bit --- src/Main.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Main.java b/src/Main.java index 9f0f806..f643450 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,7 +6,7 @@ public class Main { // The time complexity is: - // O(N^2) --> n*n = n^2 + // O(N^2) -> n*n = n^2 public static void timesTable(int x) { for(int i = 1; i <= x; i++) { for(int j = 1; j <= x; j++) { @@ -27,7 +27,7 @@ public static void printLetters(String word) { } // The time complexity is: - // YOUR ANSWER HERE + // O(1) -> n = a fixed number of banned passwords public static boolean isBanned(String password) { String[] bannedPasswords = {"password", "hello", "qwerty"}; boolean banned = false; From 548b2b833da47d744ab628c8fc547f7472854c66 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 16:33:44 -0700 Subject: [PATCH 04/23] Finished computeProduct time complexity --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index f643450..7d34244 100644 --- a/src/Main.java +++ b/src/Main.java @@ -41,7 +41,7 @@ public static boolean isBanned(String password) { // The time complexity is: - // YOUR ANSWER HERE + // O(N) -> n = the length of the array public static int computeProduct(int[] nums) { int total = 1; for(int num : nums) { From 4833efddddb37fbd73c304d9e23566f3d51b6782 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 16:36:53 -0700 Subject: [PATCH 05/23] Finished describeProduct time complexity --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 7d34244..d293577 100644 --- a/src/Main.java +++ b/src/Main.java @@ -51,7 +51,7 @@ public static int computeProduct(int[] nums) { } // The time complexity is: - // YOUR ANSWER HERE + // O(N) -> n = the length of the array public static void describeProduct(int[] nums) { System.out.println("About to compute the product of the array..."); int product = computeProduct(nums); From b0ad609b8fac5aaca99a4605ae5154b481293460 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 16:40:10 -0700 Subject: [PATCH 06/23] Finished computeFactorial time complexity --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index d293577..d067ef8 100644 --- a/src/Main.java +++ b/src/Main.java @@ -60,7 +60,7 @@ public static void describeProduct(int[] nums) { // The time complexity is: - // YOUR ANSWER HERE + // O(N) -> simplified to n due to for loop complexity public static int computeFactorial(int n) { int result = 1; for(int i = 1; i <= n; i++) { From 7cb4d65fe120805f39a81010d2b01d4af4c7ab38 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 16:43:42 -0700 Subject: [PATCH 07/23] Finished computeAllFactorials time complexity --- src/Main.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Main.java b/src/Main.java index d067ef8..a376444 100644 --- a/src/Main.java +++ b/src/Main.java @@ -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) -> n*n = n^2, double loops public static void computeAllFactorials(int[] nums) { for(int num : nums) { int result = computeFactorial(num); From 89b6d762422e8fb5f494cf6d7e10c39b0a27d175 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 16:46:06 -0700 Subject: [PATCH 08/23] Finished checkIfContainedArrayList time complexity --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index a376444..afe340a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -81,7 +81,7 @@ public static void computeAllFactorials(int[] nums) { // The time complexity is: - // YOUR ANSWER HERE + // O(N) -> n = size of the ArrayList public static void checkIfContainedArrayList(ArrayList arr, String target) { if (arr.contains(target)) { System.out.println(target + " is present in the list"); From d091562812d21c8e9de8a6f40684d019feebf3a8 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 16:47:47 -0700 Subject: [PATCH 09/23] Finished containsOverlap time complexity --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index afe340a..4daae08 100644 --- a/src/Main.java +++ b/src/Main.java @@ -93,7 +93,7 @@ public static void checkIfContainedArrayList(ArrayList arr, String targe // assume n = wordsA.length = wordsB.length // The time complexity is: - // YOUR ANSWER HERE + // O(N^2) -> n*n = n^2 public static boolean containsOverlap(String[] wordsA, String[] wordsB) { for(String wordA : wordsA) { for(String wordB : wordsB) { From e6ff94a660a188398d6476dedbebff77d290038d Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 16:51:14 -0700 Subject: [PATCH 10/23] Finished containsOverlap2 time complexity --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 4daae08..35afa52 100644 --- a/src/Main.java +++ b/src/Main.java @@ -106,7 +106,7 @@ public static boolean containsOverlap(String[] wordsA, String[] wordsB) { } // The time complexity is: - // YOUR ANSWER HERE + // O(N) -> n = length of wordsA public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { Set wordsSet = new HashSet<>(); for(String word : wordsA) { From e5949a81521e9e3efdb790437b72a491e273f284 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 16:54:05 -0700 Subject: [PATCH 11/23] Finished printCharacters time complexity and added clarification to the comments --- src/Main.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Main.java b/src/Main.java index 35afa52..de98f02 100644 --- a/src/Main.java +++ b/src/Main.java @@ -41,7 +41,7 @@ public static boolean isBanned(String password) { // The time complexity is: - // O(N) -> n = the length of the array + // O(N) -> n = the length of the int[] 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: - // O(N) -> n = the length of the array + // O(N) -> n = the length of the int[] array public static void describeProduct(int[] nums) { System.out.println("About to compute the product of the array..."); int product = computeProduct(nums); @@ -123,7 +123,7 @@ public static boolean containsOverlap2(String[] wordsA, String[] wordsB) { } // The time complexity is: - // YOUR ANSWER HERE + // O(N) -> n = the length of the chars[] array public static void printCharacters(char[] chars) { for (int i = 0; i < chars.length; i++) { char character = chars[i]; From 91ddaf7a1b75b65a61f91353140ba2f531a67c7e Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 16:55:58 -0700 Subject: [PATCH 12/23] Finished computeAverage time complexity --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index de98f02..4f9dfda 100644 --- a/src/Main.java +++ b/src/Main.java @@ -131,7 +131,7 @@ public static void printCharacters(char[] chars) { } } // The time complexity is: - // YOUR ANSWER HERE + // O(1) -> constant public static double computeAverage(double a, double b) { return (a + b) / 2.0; } From 79a729a1440d52bb5c769f2c7d4aa3ea5d5cbede Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 16:58:12 -0700 Subject: [PATCH 13/23] Finished checkIfContainedHashSet time complexity --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 4f9dfda..c96a08f 100644 --- a/src/Main.java +++ b/src/Main.java @@ -136,7 +136,7 @@ public static double computeAverage(double a, double b) { return (a + b) / 2.0; } // The time complexity is: - // YOUR ANSWER HERE + // O(1) -> simplifies public static void checkIfContainedHashSet(HashSet set, String target) { if (set.contains(target)) { From b2dc3053b4718dcc33403bec53d35eb074923198 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 17:00:01 -0700 Subject: [PATCH 14/23] Looked at class notes again, and added a more descriptive explanation to the checkIfContainedHashSet method --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index c96a08f..f53eac7 100644 --- a/src/Main.java +++ b/src/Main.java @@ -136,7 +136,7 @@ public static double computeAverage(double a, double b) { return (a + b) / 2.0; } // The time complexity is: - // O(1) -> simplifies + // O(1) -> simplifies and an average case for HashSet public static void checkIfContainedHashSet(HashSet set, String target) { if (set.contains(target)) { From 1c23ceb171321dd7b98c33f2eb8ad79686eff76d Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 17:02:31 -0700 Subject: [PATCH 15/23] Finished emailLookup time complexity --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index f53eac7..d7deceb 100644 --- a/src/Main.java +++ b/src/Main.java @@ -151,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 + // O(N) -> n = 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)) { From 1a4847e3e439a7a722f9a9b324b7739a991f91cd Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 17:07:50 -0700 Subject: [PATCH 16/23] Finished the method emailLookupEfficient and figured out the time complexity --- src/Main.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Main.java b/src/Main.java index d7deceb..b2fcebd 100644 --- a/src/Main.java +++ b/src/Main.java @@ -166,9 +166,9 @@ 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 + // O(1) -> average case for HashMap 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? From 2a1c15d27a36a6c2cd523996f72ebb7593f0b3c6 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 17:10:56 -0700 Subject: [PATCH 17/23] Finished hasCommon time complexity --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index b2fcebd..a0d16c5 100644 --- a/src/Main.java +++ b/src/Main.java @@ -173,7 +173,7 @@ public static String emailLookupEfficient(HashMap namesToEmails, // What is the time complexity of this method? // (assume the set and list have the same number of elements) - // YOUR ANSWER HERE + // O(N^2) -> n*n = n^2 public static boolean hasCommon(HashSet wordSet, ArrayList wordList) { for(String word : wordSet) { if(wordList.contains(word)) { From e1c684a84c75abeee5098b63910c5de49729019b Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 17:13:04 -0700 Subject: [PATCH 18/23] Added a for loop to iterate through --- src/Main.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Main.java b/src/Main.java index a0d16c5..d724086 100644 --- a/src/Main.java +++ b/src/Main.java @@ -187,6 +187,9 @@ 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) { + for (String word: wordList) { + + } return false; } From da6af3a21e040030349b22426d3901af687b099b Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 17:15:04 -0700 Subject: [PATCH 19/23] Added an if statement to hasCommonEfficient method --- src/Main.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index d724086..8f8045a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -188,7 +188,9 @@ public static boolean hasCommon(HashSet wordSet, ArrayList wordL // YOUR ANSWER HERE public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { for (String word: wordList) { - + if(wordSet.contains(word)) { + return true; + } } return false; } From a6baf71a341da46bee48e070487c2101be9b3885 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 17:22:00 -0700 Subject: [PATCH 20/23] Finished up the method hasCommonEfficient and flipped the order of ArrayList and HashSet for faster time complexity --- src/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 8f8045a..8e4e7c5 100644 --- a/src/Main.java +++ b/src/Main.java @@ -185,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 + // O(N) -> flipped the order of the ArrayList and HashSet to improve time complexity public static boolean hasCommonEfficient(HashSet wordSet, ArrayList wordList) { for (String word: wordList) { if(wordSet.contains(word)) { From bc00c1568da83c5bb4691192e1eb23eb26df073b Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 17 Oct 2024 17:26:27 -0700 Subject: [PATCH 21/23] Finished first question for data structure choice and chose a HashMap --- src/Main.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 8e4e7c5..2147f1e 100644 --- a/src/Main.java +++ b/src/Main.java @@ -200,7 +200,8 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList Date: Thu, 17 Oct 2024 17:30:16 -0700 Subject: [PATCH 22/23] Finished second question for data structure choice and chose an ArrayList --- src/Main.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Main.java b/src/Main.java index 2147f1e..91690fe 100644 --- a/src/Main.java +++ b/src/Main.java @@ -201,14 +201,15 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList Date: Thu, 17 Oct 2024 17:33:19 -0700 Subject: [PATCH 23/23] Finished last question for data structure choice and chose a LinkedList --- src/Main.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 91690fe..53851f6 100644 --- a/src/Main.java +++ b/src/Main.java @@ -217,5 +217,6 @@ public static boolean hasCommonEfficient(HashSet wordSet, ArrayList