-
Notifications
You must be signed in to change notification settings - Fork 41
Time - Haben #23
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?
Time - Haben #23
Changes from all commits
ebfc68c
1a64a3f
b4ecb41
5959b6f
6085e2c
df321eb
eb7f4d1
f13eac1
ed078f5
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,29 +1,104 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # 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!" | ||
|
|
||
| new_hash = Hash.new() | ||
|
|
||
| strings.each do |word| | ||
| # calculate the hash key | ||
| key_word = word.downcase.chars.sort.join | ||
|
|
||
| if new_hash[key_word] == nil | ||
| new_hash[key_word] = [] | ||
| end | ||
| array_value = new_hash[key_word] | ||
|
|
||
| length = array_value.length | ||
| array_value[length] = word | ||
| end | ||
|
|
||
| return new_hash.values | ||
| end | ||
|
|
||
| # Another solution | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
|
|
||
| # def grouped_anagrams(strings) | ||
| # new_hash = Hash.new([]) | ||
| # strings.each.with_index { |word, i| new_hash[word.downcase.chars.sort.join] += [i] } | ||
| # new_hash.map { |key, indexes| indexes.map { |i| strings[i] } } | ||
| # 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^2) | ||
| # Space Complexity: O(n) | ||
|
|
||
| def top_k_frequent_elements(list, k) | ||
|
Comment on lines
+42
to
45
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. 👍 Could you use a sort to improve the time complexity to O(n log n) or better? |
||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| return [] if list.empty? | ||
| hash1 = Hash.new() | ||
| # Create a hash where identical elements are grouped | ||
| # into array values and the element is a key | ||
| list.each do | num | | ||
| if !hash1[num] | ||
| hash1[num] = 1 | ||
| else | ||
| hash1[num] += 1 | ||
| end | ||
| end | ||
| # Put the elements in one array and the frequency of | ||
| # each element in another, sorted based on descending # freqency | ||
| elements = [] | ||
| elements_frequency = [] | ||
| hash1.each do | key, frequency | | ||
|
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. Could you sort the hash key-value pairs here or select the max k elements instead? |
||
| if elements == [] | ||
| elements[0] = key | ||
| elements_frequency[0] = frequency | ||
| else | ||
| pos = elements_frequency.length | ||
| elements_frequency.each_with_index do | val, index | | ||
| if frequency > val | ||
| pos = index | ||
| break | ||
| end | ||
| end | ||
| elements_frequency.insert(pos, frequency) | ||
| elements.insert(pos, key) | ||
| end | ||
| end | ||
| # Returns the first k elements of the elements after | ||
| # the elements are sorted in decreasing frequency | ||
| return elements[0..(k-1)] | ||
| end | ||
|
|
||
| # another solution | ||
| # input = [1, 1, 1, 2, 2, 3, 3] | ||
| # k = 2 | ||
|
|
||
| # def top_k_frequent_elements(list, k) | ||
| # list.group_by(&:itself).sort_by { |key, value| -value.length }.first(k).map(&:first) | ||
| # end | ||
|
|
||
| # top_k_frequent_elements(input, k) | ||
|
|
||
|
|
||
| # This method will return the 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 | ||
| # The same digit cannot appear twice or more in the same | ||
| # row, column or 3x3 subgrid | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def valid_sudoku(table) | ||
|
|
||
| def valid_sudoku(_table) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| 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.
👍