-
Couldn't load subscription status.
- Fork 40
Shubha 👽 #21
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?
Shubha 👽 #21
Changes from all commits
a93b91c
994dce2
cb72721
c679b6d
f356597
52da162
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,46 @@ | ||
|
|
||
| require "pry" | ||
|
|
||
| # 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 log m) where m is the number of strings, and m is the length of a string | ||
| # Space Complexity: O(n * m) where n is the number of strings, and m is the length of a string (m space complexity from sort) | ||
|
|
||
| def grouped_anagrams(strings) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| buckets = {} | ||
| strings.each do |str| | ||
| sorted = str.chars.sort.join | ||
| if buckets[sorted] | ||
| buckets[sorted] << str | ||
| else | ||
| buckets[sorted] = [str] | ||
| end | ||
| end | ||
| return buckets.values | ||
| 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: ? | ||
| # Time Complexity: O(n * k), where n is the length of the list and k is the number of common elements to return | ||
| # Space Complexity: O(n + k) where n is the length of the list and k is the number of common elements to return | ||
|
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. Since you're building a hash of unique elements I would say that dominates the time complexity. So O(m) where m is the number of unique elements. |
||
| def top_k_frequent_elements(list, k) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| counts = Hash.new(0) | ||
| top_values = Array.new() | ||
| list.each do |el| # O(n) | ||
| counts[el] += 1 | ||
| index = k - 1 | ||
| index -= 1 until top_values[index + 1] == el if top_values.include?(el) #O(k) | ||
| while index >= 0 #O(k) | ||
| if counts[el] > counts[top_values[index]] | ||
| temp = top_values[index] | ||
| top_values[index] = el | ||
| top_values[index + 1] = temp if index + 1 < k | ||
| index -= 1 | ||
| else | ||
| break | ||
| end | ||
| end | ||
| end | ||
| return top_values | ||
| end | ||
|
|
||
|
|
||
|
|
@@ -23,8 +49,66 @@ 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: O(n^2) where n is the number of elements in a row, column, or box | ||
| # Space Complexity: O(n) since only one counts hash is used at a time | ||
| def valid_sudoku(table) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
|
|
||
| # check if rows valid | ||
| table.each do |row| | ||
| counts = Hash.new(0) | ||
| row.each do |char| | ||
| if char.to_i.to_s == char | ||
| counts[char] += 1 | ||
| return false if counts[char] > 1 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # check if columns valid | ||
| column = 0 | ||
| while column < 9 | ||
| counts = Hash.new(0) | ||
| table.each do |row| | ||
| char = row[column] | ||
| if char.to_i.to_s == char | ||
| counts[char] += 1 | ||
| return false if counts[char] > 1 | ||
| end | ||
| end | ||
| column += 1 | ||
| end | ||
|
|
||
| # check if boxes valid | ||
|
|
||
| row_start = 0 | ||
| row_end = 3 | ||
|
|
||
| while row_end <= 9 | ||
| col_start = 0 | ||
| col_end = 3 | ||
| while col_end <= 9 | ||
|
|
||
| # iterate through each value in box to check for duplicates | ||
| counts = Hash.new(0) | ||
| (row_start...row_end).each do |row_index| | ||
| (col_start...col_end).each do |col_index| | ||
| char = table[row_index][col_index] | ||
| if char.to_i.to_s == char | ||
| counts[char] += 1 | ||
| return false if counts[char] > 1 | ||
| end | ||
| end | ||
| end | ||
|
Comment on lines
+92
to
+101
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, although you might be able to reduce the complexity of this loop a bit by creating a method. |
||
|
|
||
| # move on to next box in row | ||
| col_start += 3 | ||
| col_end += 3 | ||
| end | ||
| # move on to next row of boxes | ||
| row_start += 3 | ||
| row_end += 3 | ||
| end | ||
|
|
||
|
|
||
| return true | ||
| 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.
👍