forked from AdaGold/hash-practice-js
-
Notifications
You must be signed in to change notification settings - Fork 8
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 - Kelsey #1
Open
kelsk
wants to merge
3
commits into
Ada-C12:master
Choose a base branch
from
kelsk:master
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
3 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,78 @@ | ||
// This method will return an array of arrays. | ||
// Each subarray will have strings which are anagrams of each other | ||
// Time Complexity: ? | ||
// Space Complexity: ? | ||
function grouped_anagrams(strings) { | ||
throw new Error("Method hasn't been implemented yet!"); | ||
// Time Complexity: O(n) overall. Iterating over the array of strings to extract each string is O(n) where n is the length of the array. Alphabetizing each string is itself O(n) where n is the length of the word. Checking for the key in the uniqAnagrams object is O(1), and inserting an object into an array in Javascript is O(n). Pretty sure Object.keys is an O(n) situation as well. | ||
// Space Complexity: O(n). Creating a hash with a size of up to 'n' length of strings, and creating an array of subarrays with a total size that's up to 'n' length of strings. (Also creating an array 'key' that's the size of the length of one string.) | ||
function groupedAnagrams(strings) { | ||
// return empty if no strings | ||
if (strings.length === 0) return strings; | ||
|
||
let uniqAnagrams = {}; | ||
|
||
strings.forEach(s => { | ||
// alphabetize one string to use as a key | ||
let key = s.split(''); | ||
key = key.sort(); | ||
key = key.join(''); | ||
|
||
if (uniqAnagrams.hasOwnProperty(key)) { | ||
// if alphabetized string exists as a key | ||
// add the un-alphabetized string to the key's array | ||
uniqAnagrams[key].push(s) | ||
} else { | ||
// create a new key (alphabetized string) with value of an array containing the un-alphabetized string | ||
uniqAnagrams[key] = [s]; | ||
} | ||
}) | ||
|
||
let anagramArrays = []; | ||
let keys = Object.keys(uniqAnagrams); | ||
|
||
// for each key in uniqAnagrams, | ||
// add the value (array of anagram strings) to anagramArrays | ||
keys.forEach(key => { | ||
anagramArrays.push(uniqAnagrams[key]) | ||
}) | ||
|
||
return anagramArrays; | ||
|
||
} | ||
|
||
// 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: ? | ||
function top_k_frequent_elements(list, k) { | ||
throw new Error("Method hasn't been implemented yet!"); | ||
// Time Complexity: O(n). | ||
// Iterating over 'list' is O(n) where n is the length of 'list', iterating over 'uniqEls' is O(n) where n is the length of uniqEls(less than or equal to the length of 'list'). | ||
// Space Complexity: O(n). Creating a hash and an array are both O(n). | ||
|
||
function topKFrequentElements(list, k) { | ||
Comment on lines
+42
to
+46
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. Given your two nested loops you have an O(n^2) time complexity. |
||
if (k === 0 || list.length === 0) return []; | ||
|
||
// iterate over list (O(n)) | ||
// each unique item becomes a new key | ||
// each repeated item adds the value of the key by 1 | ||
let listHash = {}; | ||
let uniqEls = []; | ||
list.forEach(el => { | ||
listHash[el] ? listHash[el] += 1 : listHash[el] = 1 | ||
if (!uniqEls.includes(el)) uniqEls.push(el) | ||
}) | ||
|
||
// for each unique element, | ||
// we sort based on their corresponding value | ||
for (let i = 0; i <= uniqEls.length; i++) { | ||
if (listHash[uniqEls[i]] < listHash[uniqEls[i+1]]) { | ||
let j = i; | ||
while (listHash[uniqEls[j]] < listHash[uniqEls[j + 1]]) { | ||
let temp = uniqEls[j]; | ||
uniqEls[j] = uniqEls[j + 1]; | ||
uniqEls[j + 1] = temp; | ||
j -= 1; | ||
} | ||
} | ||
} | ||
|
||
// return an array of the first 'k' elements from the unique elements array | ||
if (uniqEls.length <= k) return uniqEls | ||
return uniqEls.slice(0, k) | ||
} | ||
|
||
|
||
|
@@ -22,12 +83,12 @@ function top_k_frequent_elements(list, k) { | |
// row, column or 3x3 subgrid | ||
// Time Complexity: ? | ||
// Space Complexity: ? | ||
function valid_sudoku(table) { | ||
function validSudoku(table) { | ||
throw new Error("Method hasn't been implemented yet!"); | ||
} | ||
|
||
module.exports = { | ||
grouped_anagrams, | ||
top_k_frequent_elements, | ||
valid_sudoku | ||
groupedAnagrams, | ||
topKFrequentElements, | ||
validSudoku | ||
}; |
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
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.
Good, but the lines are a bit long to read.