Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 52 additions & 13 deletions src/Practice.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Comment on lines +30 to 34

Copy link
Copy Markdown
Owner

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?

Expand All @@ -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<>();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice logic!

}

return mostCommon;
}

/**
Expand All @@ -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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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 O(1). Consider how the size of mostCommonMap will increase with our input.


return mostCommonInt;
}
}
24 changes: 19 additions & 5 deletions src/PracticeTest.java
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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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?

}