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

Daniela Sanchez - Paper #1

Open
wants to merge 3 commits 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
100 changes: 91 additions & 9 deletions lib/exercises.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,113 @@
// 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^2)
// 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.

👍 However this is not an O(n^2) solution. Instead it's O(n), if the words are limited in length (like to English words). If the words are not limited it's O(n * m log m) due to sorting.

throw new Error("Method hasn't been implemented yet!");

var dic_anagrams = {};

for (let i = 0; i < strings.length; i++) {
var wordkey = strings[i].split('').sort().join('');
if (wordkey in dic_anagrams) {
dic_anagrams[wordkey].push(strings[i])
}
else {
dic_anagrams[wordkey] = [strings[i]]
}
}

const output = []
for (const [key, value] of Object.entries(dic_anagrams)) {
output.push(value)
}
return output;
}



// 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^2)
// Space Complexity: O(n)
function top_k_frequent_elements(list, k) {
Comment on lines +30 to 32

Choose a reason for hiding this comment

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

👍 But the time complexity is O(n) time complexity is O(n * k * m) where n is the number of elements and k the number of most frequent elements and m the size of the most frequent element. It can be written more efficiently, but this is an ok solution.

throw new Error("Method hasn't been implemented yet!");
if (list.length < 1) {
return []
}
var highest_freq = 0
var hash_ele = {}
for (let i = 0; i < list.length; i++) {
hash_ele[list[i]] = (hash_ele[list[i]] || 0) + 1;
if (hash_ele[list[i]] > highest_freq) {
highest_freq = hash_ele[list[i]]
}
}
var counting_hash = {}
for (const [key, value] of Object.entries(hash_ele)) {
if (value in counting_hash) {
counting_hash[value].push(key)
}
else {
counting_hash[value] = [key]
}
}

output = []
while (output.length < k) {
if (highest_freq in counting_hash) {
for (let i = 0; i < counting_hash[highest_freq].length; i++) {
if (output.length === k) {
continue
}
Comment on lines +56 to +60

Choose a reason for hiding this comment

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

Maybe instead of counting down highest_freq each time, you simply find the largest value in the object and then remove that key-value pair. That might speed things up.

var number = counting_hash[highest_freq][i]
output.push(parseInt(number));
}
}
highest_freq -= 1
}
return output
}



// This method will return true if the table is still
// a valid sudoku table.
// Each element can either be a ".", or a digit 1-9
// The same digit cannot appear twice or more in the same
// row, column or 3x3 subgrid
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(n^2)
// Space Complexity: O(n)
function valid_sudoku(table) {
Comment on lines +77 to 79

Choose a reason for hiding this comment

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

One note. Because Sudoku boards never change in size, scale isn't an issue!

throw new Error("Method hasn't been implemented yet!");
let rows = [[], [], [], [], [], [], [], [], []];
let columns = [[], [], [], [], [], [], [], [], []];
let boxes = [[], [], [], [], [], [], [], [], []];

for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {

let cell = table[i][j];

if(cell !== ".") {
if (rows[i].includes(cell)) {
return false
} else rows[i].push(cell);

if (columns[j].includes(cell)) {
return false;
} else columns[j].push(cell);

let boxIndex = Math.floor((i / 3)) * 3 + Math.floor(j / 3);

if (boxes[boxIndex].includes(cell)) {
return false;
} else boxes[boxIndex].push(cell);

}
}
}
return true
}


module.exports = {
grouped_anagrams,
top_k_frequent_elements,
Expand Down
2 changes: 1 addition & 1 deletion test/exercises.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,4 @@ describe("exercises", function () {
expect(valid).toEqual.false;
});
});
});
});