-
Notifications
You must be signed in to change notification settings - Fork 55
Finished timeSpacePractice #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f1b0d8b
ed61e8d
38c4058
6975f89
efb5d8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,8 @@ | |
|
|
||
| public class Practice { | ||
|
|
||
| // Time Complexity: | ||
| // Space Complexity: | ||
| // Time Complexity: O(n) | ||
| // Space Complexity: O(n) | ||
| public static List<Integer> findEvens(int[] array) { | ||
| List<Integer> evens = new ArrayList<>(); | ||
| for (int num : array) { | ||
|
|
@@ -17,19 +17,18 @@ public static List<Integer> findEvens(int[] array) { | |
| return evens; | ||
| } | ||
|
|
||
| // Time Complexity: | ||
| // Space Complexity: | ||
| // Time Complexity: O(n) | ||
| // Space Complexity: O(1) | ||
| public static int sumDiagonal(int[][] matrix) { | ||
| int sum = 0; | ||
| for (int i = 0; i < matrix.length; i++) { | ||
| sum += matrix[i][i]; | ||
| } | ||
| return sum; | ||
| } | ||
|
|
||
|
|
||
| // Time Complexity: | ||
| // Space Complexity: | ||
| // Time Complexity: O(n) | ||
| // Space Complexity: O(n^2) | ||
| // Does the 'T' look confusing? Consider refreshing on generic methods | ||
| // We'll revisit generics as a class later | ||
| public static <T> Map<T, Integer> countFrequencies(T[] array) { | ||
|
|
@@ -49,16 +48,34 @@ public static <T> Map<T, Integer> countFrequencies(T[] array) { | |
| * | ||
| * Once you finish, WRITE TESTS FOR IT in PracticeTest.java | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Time Complexity: O(n) | ||
| * Space Complexity: O(n) | ||
| * | ||
| * @param nums An array of integers | ||
| * @return the integer that shows up most commonly | ||
| */ | ||
| public static int mostCommonTimeEfficient(int[] nums) { | ||
| // TODO: Complete this method with an implementation that runs | ||
| // in O(n) time. n = nums.size() | ||
| return -1; | ||
|
|
||
| HashMap<Integer, Integer> frequencies = new HashMap<>(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember to use interface types where appropriate (Map). |
||
| int highestCount = 0; | ||
| int mostCommon = 0; | ||
| for (int i = 0; i < nums.length; i++) { | ||
| if (frequencies.containsKey(nums[i])) { | ||
| int count = frequencies.get(nums[i]); | ||
| count++; | ||
| frequencies.put(nums[i], count); | ||
| } else { | ||
| frequencies.put(nums[i], 1); | ||
| } | ||
| if (frequencies.get(nums[i]) >= highestCount) { | ||
| highestCount = frequencies.get(nums[i]); | ||
| mostCommon = nums[i]; | ||
| } | ||
|
Comment on lines
+64
to
+75
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice logic! |
||
| } | ||
|
|
||
| return mostCommon; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -70,15 +87,37 @@ public static int mostCommonTimeEfficient(int[] nums) { | |
| * | ||
| * Once you finish, WRITE TESTS FOR IT in PracticeTest.java | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * | ||
| * @param nums An array of integers | ||
| * @return the integer that shows up most commonly | ||
| */ | ||
| public static int mostCommonSpaceEfficient(int[] nums) { | ||
| // TODO: Complete this method with an implementation that runs | ||
| // in O(1) space. | ||
| return -1; | ||
|
|
||
| // HashMap that stores the number without duplicates | ||
| Map<Integer, Integer> mostCommonMap = new HashMap<>(); | ||
| // Keeps track of the most common number | ||
| int mostCommonCount = 0; | ||
| int mostCommonInt = nums[0]; | ||
|
|
||
| // Loops over the input array to add each number to the HashMap | ||
| // Sets each numbers count to 1 | ||
| for (int num : nums) { | ||
| mostCommonMap.put(num, mostCommonMap.getOrDefault(num, 0)); | ||
|
|
||
| // Variable that targets the count, that being the number that represents the | ||
| // amount of times a number appears in the array | ||
| int count = mostCommonMap.get(num); | ||
|
|
||
| if (count > mostCommonCount) { | ||
| mostCommonCount = count; | ||
| mostCommonInt = num; | ||
| } | ||
| } | ||
|
Comment on lines
+101
to
+119
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the idea, but this doesn't get us to our desired space complexity of |
||
|
|
||
| return mostCommonInt; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,25 @@ | ||
| import org.junit.jupiter.api.Test; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
|
|
||
| public class PracticeTest { | ||
|
|
||
| // TODO: Implement tests for Practice.mostCommonTimeEfficient and Practice.mostCommonSpaceEfficient | ||
|
|
||
| // Hints: They are static methods, so you will use the full Practice.mostCommonTimeEfficient for method calls | ||
| // Forgot how to do unit tests? Look back at the ramblebot tests or old slides for a refresher! | ||
| // TODO: Implement tests for Practice.mostCommonTimeEfficient and | ||
| // Practice.mostCommonSpaceEfficient | ||
|
|
||
| // Hints: They are static methods, so you will use the full | ||
| // Practice.mostCommonTimeEfficient for method calls | ||
| // Forgot how to do unit tests? Look back at the ramblebot tests or old slides | ||
| // for a refresher! | ||
|
|
||
| @Test | ||
| void mostCommonTimeEfficient() { | ||
| int[] nums = { 0, 1, 1, 2, 3, 4, 5, 6, 7, 8 }; | ||
| assertEquals(1, Practice.mostCommonTimeEfficient(nums)); | ||
| } | ||
|
|
||
| @Test | ||
| void mostCommonSpaceEfficient() { | ||
| int[] nums = { 2, 2, 2, 2, 4, 4, 6, 6, 6, 8, 9, 10 }; | ||
| assertEquals(2, Practice.mostCommonSpaceEfficient(nums)); | ||
| } | ||
|
Comment on lines
+14
to
+24
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like these tests, but there needs to be more of them to give us more confidence that our code is working. What other cases could you consider testing here? |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take another look at space complexity for this one. How big is each entry going to be? How many entries are there going be total?