Liban & Stephen project done#7
Conversation
auberonedu
left a comment
There was a problem hiding this comment.
Nice job! However, there are a few places where the complexity analysis is off and your space efficient solution does not meet the O(1) space requirement. Consider taking another look at these.
| // Time Complexity: O(n^2) | ||
| // 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]; |
There was a problem hiding this comment.
Take another look at the time complexity here. How many times does the for loop run?
| * Time Complexity: O(n) | ||
| * Space Complexity: O(1) | ||
| * | ||
| * @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; | ||
| int highestValue = 0; | ||
| Map <Integer, Integer> mostCommon = new HashMap<Integer, Integer>(); |
There was a problem hiding this comment.
Take another look at the space complexity. Think of what data structures you're allocating, and how they'll grow as the input size increases.
| * Space Complexity: O(1) | ||
| * | ||
| * @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; | ||
| int highestCommon = 0; | ||
| int mostFrequentNum = nums[0];; | ||
|
|
||
| Map <Integer, Integer> mostCommon = new HashMap<Integer, Integer>(); |
There was a problem hiding this comment.
Again, take a look at the space complexity. This doesn't satisfy the requirement of O(1) space.
| int[] numsTest = new int[6]; | ||
| numsTest[0] = 1; | ||
| numsTest[1] = 6; | ||
| numsTest[2] = 2; | ||
| numsTest[3] = 4; | ||
| numsTest[4] = 2; | ||
| numsTest[5] = 2; |
There was a problem hiding this comment.
Consider using a shorter syntax to initialize arrays:
int[] numsTest = {1, 6, 2, 4, 2, 2};
| @Test | ||
| void testMostCommonSpaceEfficient(){ | ||
| int[] numsTest = {1, 2, 3, 3, 4, 4, 2}; | ||
|
|
||
| int howMany = Practice.mostCommonSpaceEfficient(numsTest); | ||
|
|
||
| assertEquals( 2, howMany); | ||
|
|
||
| } |
There was a problem hiding this comment.
These are good tests, but we need more of them to have more confidence that our solution is working. What other cases can you test?
No description provided.