Skip to content
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

Branches- Amal #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
46 changes: 39 additions & 7 deletions lib/exercises.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
// This method will return an array of arrays.
// Each subarray will have strings which are anagrams of each other
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(n * m)
// Space Complexity: O(n)
function grouped_anagrams(strings) {
Comment on lines +3 to 5

Choose a reason for hiding this comment

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

The Time complexity would be O(n) if the words aren't too long. Or O(m * n log n) if the words are (m words and each word is at most n letters). The sort is a mergesort and it costs the most with each word.

throw new Error("Method hasn't been implemented yet!");
let h = {}

strings.forEach((word) => {
const orderedChars = word.split("").sort().join("")
if(h[orderedChars]){
h[orderedChars].push(word)
}else{
h[orderedChars] = [word]
}
})
return Object.values(h)
}

// This method will return the k most common elements
// in the case of a tie it will select the first occuring element.
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(n)
// Space Complexity: O(n)
function top_k_frequent_elements(list, k) {
Comment on lines +21 to 23

Choose a reason for hiding this comment

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

Since you're doing a sort it's O(n log n) time complexity.

throw new Error("Method hasn't been implemented yet!");
if (list.length == 0 ){
return []
}
let listObj = {}
let res= [];
list.forEach((num) =>{
if(listObj[num]){
listObj[num] += 1
}else {
listObj[num] = 1
}
})
const maxArray = Object.keys(listObj).sort((function(a, b) {
if(listObj[a] == listObj[b]){
return -1
}else{
Comment on lines +37 to +39

Choose a reason for hiding this comment

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

Why not just subtract?

return listObj[a] - listObj[b]
}
})).reverse()
for( let i = 0; i < k; i++){
res.push(parseInt(maxArray[i]))
}
return res
}


Expand All @@ -30,4 +62,4 @@ module.exports = {
grouped_anagrams,
top_k_frequent_elements,
valid_sudoku
};
}
2 changes: 1 addition & 1 deletion test/exercises.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe("exercises", function () {
});
});

describe.skip("top_k_frequent_elements", function () {
describe("top_k_frequent_elements", function () {
it("works with example 1", function () {
// Arrange
const list = [1, 1, 1, 2, 2, 3];
Expand Down