forked from AdaGold/hash-practice-js
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Aimee 👾 - Fire 🔥 #1
Open
marks214
wants to merge
4
commits into
Ada-C14:master
Choose a base branch
from
marks214:main
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,85 @@ | ||
// 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^3) - outer while loop (line 10) has a nested for loop (lines 24 and 27), it also has .splice() nested in a for loop, but this should still be O(n^3) overall. | ||
// Space Complexity: O(n) - result, hash, and matched will be at most length n, where n is the number of elements in the strings array. Even though result is a nested array, it will never contain more than n elements. | ||
function grouped_anagrams(strings) { | ||
throw new Error("Method hasn't been implemented yet!"); | ||
|
||
let result = []; | ||
let resultIndex = 0; | ||
|
||
while (strings.length > 0) { | ||
let hash = {}; | ||
let word = strings.pop(); | ||
result.push([word]) | ||
|
||
for (char in word) { | ||
if (hash[word[char]]) { | ||
hash[word[char]]++; | ||
} else { | ||
hash[word[char]] = 1 | ||
} | ||
} | ||
|
||
let matched = []; | ||
for (let i = 0; i < strings.length; i++) { | ||
let letterCount = 0; | ||
let temp = strings[i]; | ||
for (let j = 0; j < temp.length; j++) { | ||
if (hash[temp[j]]) { | ||
letterCount++; | ||
if (letterCount === temp.length) { | ||
result[resultIndex].push(temp); | ||
matched.push(i); | ||
} | ||
} else { | ||
break | ||
} | ||
} | ||
} | ||
|
||
for (let i = matched.length - 1; i > -1; i--) { | ||
strings.splice(matched[i], 1); | ||
} | ||
|
||
resultIndex++; | ||
} | ||
|
||
return result; | ||
|
||
} | ||
|
||
// 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(nlog(n)) - uses javascript sort, where n is the unique number of elements (worst case) | ||
// Space Complexity: O(n) - the hash object will never have more than n number of key-value pairs (based on what elements are in list), and the result array will never have more than n elements, where n is the value of k | ||
function top_k_frequent_elements(list, k) { | ||
Comment on lines
+53
to
55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 This will work |
||
throw new Error("Method hasn't been implemented yet!"); | ||
let hash = {}; | ||
let result = []; | ||
|
||
if (list.length === 0) { | ||
return result; | ||
} | ||
|
||
for (i in list) { | ||
if (hash[list[i]]) { | ||
hash[list[i]]++; | ||
} else { | ||
hash[list[i]] = 1; | ||
} | ||
} | ||
|
||
let keysSorted = Object.keys(hash).sort(function(a,b) { | ||
// returns keys in descending order of freq | ||
return (hash[b]-hash[a]) | ||
} | ||
) | ||
|
||
for (let i = 0; i < k; i++) { | ||
result.push(parseFloat(keysSorted[i])); | ||
} | ||
|
||
return result | ||
|
||
} | ||
|
||
|
||
|
@@ -20,10 +88,82 @@ function top_k_frequent_elements(list, k) { | |
// 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) | ||
// Space Complexity: O(n) | ||
function valid_sudoku(table) { | ||
Comment on lines
+91
to
93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Fun fact, since a Sudoku board is always 9x9, this problem is actually O(1) ! |
||
throw new Error("Method hasn't been implemented yet!"); | ||
// start by checking that the same value doesn't occur twice in the same row | ||
for (let i = 0; i < 9; i++) { | ||
let rowHash = {}; | ||
for (let j = 0; j < 9; j++) { | ||
if (table[i][j] === '.') { | ||
continue; | ||
} else if (rowHash[table[i][j]]) { | ||
return false; | ||
} else { | ||
rowHash[table[i][j]] = j; | ||
} | ||
} | ||
} | ||
|
||
// check for column duplicates | ||
for (let i = 0; i < 9; i++) { | ||
let colHash = {}; | ||
for (let j = 0; j < 9; j++) { | ||
if (table[j][i] === '.') { | ||
continue; | ||
} else if (colHash[table[j][i]]) { | ||
return false; | ||
} else { | ||
colHash[table[j][i]] = i; | ||
} | ||
} | ||
} | ||
|
||
// check for 3x3 grid duplicates | ||
let _3x3 = {}; | ||
let i = 0; | ||
let j = 0; | ||
let boxes = 9; | ||
// there will be a total of 9 "boxes" to check on the Sudoku 3x3 grid | ||
// the box colum number represents the 3 columns of the 3x3 grid | ||
let boxColNum = 1; | ||
|
||
while (boxes < 9) { | ||
if (table[i][j] === '.') { | ||
continue; | ||
} else if (_3x3[table[i][j]]) { | ||
return false; | ||
} else { | ||
_3x3[table[i][j]] = i; | ||
} | ||
|
||
j++; | ||
i++; | ||
|
||
if (j === 3 && boxColNum === 1) { | ||
j = 0; | ||
} else if (j === 6 && boxColNum === 2) { | ||
j = 3; | ||
} else if (j === 9 && boxColNum === 3) { | ||
j = 6; | ||
} | ||
|
||
if (i === 3 || i === 6) { | ||
_3x3 = {} | ||
boxes++; | ||
} | ||
|
||
if (i > 8) { | ||
boxColNum++; | ||
if (boxColNum === 2) { | ||
j = 3; | ||
} else if (boxColNum === 3) { | ||
j = 6; | ||
} | ||
i = 0; | ||
} | ||
} | ||
|
||
} | ||
|
||
module.exports = { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Sort the letters in each word and use that as a key to a hash and make the value a list of words with those letters.