-
Notifications
You must be signed in to change notification settings - Fork 40
Ari #7
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
base: master
Are you sure you want to change the base?
Ari #7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,64 @@ | ||
|
|
||
|
|
||
| # 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) | ||
| # Space Complexity: O(n) | ||
|
|
||
| def grouped_anagrams(strings) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| return [] if strings.empty? | ||
|
|
||
| anagram_hash = Hash.new() | ||
| strings.each do |string| | ||
| word_array = string.split("").sort | ||
| if anagram_hash.include?(word_array) | ||
| anagram_hash[word_array] << string | ||
| else | ||
| anagram_hash[word_array] = [string] | ||
| end | ||
| end | ||
|
|
||
| result = [] | ||
| anagram_hash.each do |key, value| | ||
| result << value | ||
| end | ||
| return result | ||
|
|
||
| end | ||
|
|
||
| # 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: ? | ||
| # in the case of a tie it will select the first occurring element. | ||
| # Time Complexity: O(n) | ||
|
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. Looking at your later loop, I would say this is O(k * n) |
||
| # Space Complexity: O(n) | ||
| def top_k_frequent_elements(list, k) | ||
|
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. Nice work! |
||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| return [] if list.empty? | ||
| return list if list.length == 1 | ||
|
|
||
| element_count = Hash.new() | ||
|
|
||
| list.each do |element| | ||
| if element_count[element] | ||
| element_count[element] += 1 | ||
| else | ||
| element_count[element] = 1 | ||
| end | ||
| end | ||
|
|
||
| result = [] | ||
|
|
||
| k.times do |i| | ||
| count = 0 | ||
| frequent_element = nil | ||
| element_count.each do |element, frequency| | ||
| if frequency > count | ||
| count = frequency | ||
| frequent_element = element | ||
| end | ||
| end | ||
| result << frequent_element | ||
| element_count[frequent_element] = 0 if frequent_element | ||
| end | ||
|
|
||
| return result | ||
|
|
||
| end | ||
|
|
||
|
|
||
|
|
@@ -23,8 +67,73 @@ def 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: I have no idea...O(n^2) since I have nested loops? | ||
| # Space Complexity: O(n) | ||
| def valid_sudoku(table) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| end | ||
|
|
||
| return true if in_row?(table) && in_column?(table) && in_box?(table) | ||
|
|
||
| return false | ||
|
|
||
| end | ||
|
|
||
| def in_row?(table) | ||
| table.length.times do |row| | ||
| sudoku_hash = Hash.new() | ||
| table.length.times do |i| | ||
|
|
||
| if sudoku_hash[table[row][i]] | ||
| return false | ||
| end | ||
|
|
||
| if table[row][i] != '.' | ||
| sudoku_hash[table[row][i]] = 1 | ||
| end | ||
|
|
||
| end | ||
| end | ||
| end | ||
|
|
||
| # Checks whether there is any | ||
| # duplicate in current column or not. | ||
| def in_column?(table) | ||
|
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. These in_row and in_column methods don't look very dry here. |
||
|
|
||
| table.length.times do |col| | ||
| column_hash = Hash.new() | ||
|
|
||
| table.length.times do |i| | ||
|
|
||
| if column_hash[table[i][col]] | ||
| return false | ||
| end | ||
| if table[i][col] != '.' | ||
| column_hash[table[i][col]] = 1 | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Checks whether there is any duplicate | ||
| # in current 3×3 box or not. | ||
| def in_box?(table) | ||
|
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 is a really slick method! Nice |
||
|
|
||
| # formula from https://stackoverflow.com/questions/41020695/how-do-i-split-a-9x9-array-into-9-3x3-components | ||
| grids = table.each_slice(3).map{|strip| strip.transpose.each_slice(3).map{|chunk| chunk.transpose}}.flatten(1) | ||
|
|
||
| grids.each do |box| | ||
| box_hash = Hash.new() | ||
| 3.times do |row| | ||
| 3.times do |col| | ||
| current = box[row][col] | ||
| if box_hash[current] | ||
| return false | ||
| end | ||
| if current != '.' | ||
| box_hash[current] = 1 | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
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. Minor thing: I suggest a |
||
| end | ||
|
|
||
| end | ||
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.
Assuming the words are small, this is correct.