Skip to content

Liban & Stephen project done#7

Open
brittLiban wants to merge 9 commits into
auberonedu:mainfrom
brittLiban:main
Open

Liban & Stephen project done#7
brittLiban wants to merge 9 commits into
auberonedu:mainfrom
brittLiban:main

Conversation

@brittLiban

Copy link
Copy Markdown

No description provided.

@auberonedu auberonedu left a comment

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 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.

Comment thread src/Practice.java
Comment on lines +18 to 23
// 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];

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 the time complexity here. How many times does the for loop run?

Comment thread src/Practice.java
Comment on lines +51 to +61
* 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>();

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 the space complexity. Think of what data structures you're allocating, and how they'll grow as the input size increases.

Comment thread src/Practice.java
Comment on lines +88 to +99
* 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>();

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.

Again, take a look at the space complexity. This doesn't satisfy the requirement of O(1) space.

Comment thread src/PracticeTest.java
Comment on lines +14 to +20
int[] numsTest = new int[6];
numsTest[0] = 1;
numsTest[1] = 6;
numsTest[2] = 2;
numsTest[3] = 4;
numsTest[4] = 2;
numsTest[5] = 2;

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.

Consider using a shorter syntax to initialize arrays:
int[] numsTest = {1, 6, 2, 4, 2, 2};

Comment thread src/PracticeTest.java
Comment on lines +29 to +37
@Test
void testMostCommonSpaceEfficient(){
int[] numsTest = {1, 2, 3, 3, 4, 4, 2};

int howMany = Practice.mostCommonSpaceEfficient(numsTest);

assertEquals( 2, howMany);

}

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.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants