Skip to content
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

🌊 - Kim #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ Given a non-empty array of integers, return the *k* most frequent elements.
```
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]

```

## Example 2

```
Input: nums = [1], k = 1
Output: [1]
```
Expand Down
76 changes: 66 additions & 10 deletions lib/exercises.rb
Original file line number Diff line number Diff line change
@@ -1,29 +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) where n is the number of chars
# Space Complexity: O(n) where n is the number of words
def grouped_anagrams(strings)
Comment on lines +4 to 6

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Interesting solution, the time complexity is O(n) if the words are pretty short, if they're unlimited in length the time complexity is O(nm) where m is the length of the longest string.

raise NotImplementedError, "Method hasn't been implemented yet!"
frequency_hash_to_words = Hash.new

strings.each do |word|
frequency_hash = word.each_char.with_object(Hash.new(0)) { |letter, count| count[letter] += 1 }

if frequency_hash_to_words.include?(frequency_hash)
frequency_hash_to_words[frequency_hash] << word
else
frequency_hash_to_words[frequency_hash] = [ word ]
end
end

return frequency_hash_to_words.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 log n)
# Space Complexity: O(n)
def top_k_frequent_elements(list, k)
Comment on lines +24 to 26

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Well done

raise NotImplementedError, "Method hasn't been implemented yet!"
count = Hash.new(0)

list.each do |num|
count[num] += 1
end

count = count.sort_by { |k, v| -v }.to_h

return count.keys[0...k]
end

def get_sub_box(row, col)
return 0 if row <= 2 && col <= 2
return 1 if row <= 2 && col >= 3 && col <= 5
return 2 if row <= 2 && col >= 6

return 3 if row >= 3 && row <= 5 && col <= 2
return 4 if row >= 3 && row <= 5 && col >= 3 && col <= 5
return 5 if row >= 3 && row <= 5 && col >= 6

return 6 if row >= 6 && col <= 2
return 7 if row >= 6 && col >= 3 && col <= 5
return 8
end

# 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
# row, column or 3x3 subgrid
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(1)
# Space Complexity: O(n)
def valid_sudoku(table)
Comment on lines +57 to 59

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I would say the time/space complexities are both O(1) since Sudoku never gets bigger than 9x9.

raise NotImplementedError, "Method hasn't been implemented yet!"
row_number_frequencies = [ {}, {}, {}, {}, {}, {}, {}, {}, {} ]
column_number_frequencies = [ {}, {}, {}, {}, {}, {}, {}, {}, {} ]
sub_box_number_frequencies = [ {}, {}, {}, {}, {}, {}, {}, {}, {} ]

table.each_with_index do |row, r|
row.each_with_index do |column, c|
cell = table[r][c]
next if cell == '.'

# check/update row
return false if row_number_frequencies[r].include?(cell)
row_number_frequencies[r][cell] = true

# check/update column
return false if column_number_frequencies[c].include?(cell)
column_number_frequencies[c][cell] = true

# check/update sub-box
sub_box = get_sub_box(r, c)
return false if sub_box_number_frequencies[sub_box].include?(cell)
sub_box_number_frequencies[sub_box][cell] = true
end
end

return true
end
2 changes: 1 addition & 1 deletion test/exercises_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
end
end

xdescribe "valid sudoku" do
describe "valid sudoku" do
it "works for the table given in the README" do
# Arrange
table = [
Expand Down