Merge branch 'TheAlgorithms:master' into master

This commit is contained in:
Sahil Afrid Farookhi 2021-06-27 21:51:56 +05:30 committed by GitHub
commit 1b783c05c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 237 additions and 0 deletions

View file

@ -51,6 +51,7 @@
* Hash Table
* [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/anagram_checker.rb)
* [Arrays Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/arrays_intersection.rb)
* [Common Characters](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/common_characters.rb)
* [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/find_all_duplicates_in_an_array.rb)
* [Good Pairs](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/good_pairs.rb)
* [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/richest_customer_wealth.rb)
@ -77,6 +78,7 @@
* [Coin Change](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/coin_change.rb)
* [Count Sorted Vowel Strings](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/count_sorted_vowel_strings.rb)
* [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/fibonacci.rb)
* [Pascal Triangle Ii](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/pascal_triangle_ii.rb)
## Maths
* [3N Plus 1 or Collatz Conjecture](https://github.com/TheAlgorithms/Ruby/blob/master/maths/3nPlus1.rb)
@ -103,6 +105,7 @@
* [Find Min](https://github.com/TheAlgorithms/Ruby/blob/master/maths/find_min.rb)
* [Lucas Series](https://github.com/TheAlgorithms/Ruby/blob/master/maths/lucas_series.rb)
* [Number Of Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/number_of_digits.rb)
* [Pascal Triangle Ii](https://github.com/TheAlgorithms/Ruby/blob/master/maths/pascal_triangle_ii.rb)
* [Power Of Two](https://github.com/TheAlgorithms/Ruby/blob/master/maths/power_of_two.rb)
* [Prime Number](https://github.com/TheAlgorithms/Ruby/blob/master/maths/prime_number.rb)
* [Roman To Integer](https://github.com/TheAlgorithms/Ruby/blob/master/maths/roman_to_integer.rb)

View file

@ -0,0 +1,51 @@
# Challenge name: Find Common Characters
#
# Given an array A of strings made only from lowercase letters, return a list
# of all characters that show up in all strings within the list
# (including duplicates). For example, if a character occurs 3 times in all
# strings but not 4 times, you need to include that character three times in
# the final answer.
#
# You may return the answer in any order.
#
# Example 1:
# Input: ["bella","label","roller"]
# Output: ["e","l","l"]
#
# Example 2:
# Input: ["cool","lock","cook"]
# Output: ["c","o"]
#
# Approach 1: Hash
#
# Time Complexity: O(n)
#
def common_characters(arr)
target_count = arr.count
hash = Hash.new(0)
(0...target_count).each do |i|
arr[i].split('').each do |letter|
hash[letter] += 1
end
end
result = []
hash.each do |k, v|
while v >= target_count
if v >= target_count
result << k
v -= target_count
end
end
end
result
end
puts common_characters(["bella","label","roller"])
# => ["e","l","l"]
puts common_characters(["cool","lock","cook"])
# => ["c","o"]

View file

@ -0,0 +1,138 @@
# Given an integer row_index, return the rowIndexth (0-indexed) row of the Pascal's triangle.
# Example 1:
#
# Input: row_index = 3
# Output: [1,3,3,1]
#
# Example 2:
#
# Input: row_index = 0
# Output: [1]
#
# Example 3:
#
# Input: row_index = 1
# Output: [1,1]
#
# Approach 1: Brute Force
#
# Complexity Analysis
#
# Time complexity: O(k^2).
# Space complexity: O(k) + O(k) ~ O(k)
def get_num(row, col)
return 1 if row == 0 || col == 0 || row == col
get_num(row - 1, col - 1) + get_num(row - 1, col)
end
def get_row(row_index)
result = []
(row_index + 1).times do |i|
result.push(get_num(row_index, i))
end
result
end
row_index = 3
print(get_row(row_index))
# => [1,3,3,1]
row_index = 0
print(get_row(row_index))
# => [1]
row_index = 1
print(get_row(row_index))
# => [1,1]
#
# Approach 2: Dynamic Programming
#
# Complexity Analysis
#
# Time complexity: O(k^2).
# Space complexity: O(k) + O(k) ~ O(k).
# @param {Integer} row_index
# @return {Integer[]}
def get_row(row_index)
result = generate(row_index)
result[result.count - 1]
end
def generate(num_rows)
return [[1]] if num_rows < 1
result = [[1], [1, 1]]
(2...num_rows + 1).each do |row|
prev = result[row - 1]
current = [1]
med = prev.count / 2
(1...prev.count).each do |i|
current[i] = prev[i - 1] + prev[i]
end
current.push(1)
result.push(current)
end
result
end
row_index = 3
print(get_row(row_index))
# => [1,3,3,1]
row_index = 0
print(get_row(row_index))
# => [1]
row_index = 1
print(get_row(row_index))
# => [1,1]
#
# Approach 3: Memory-efficient Dynamic Programming
#
# Complexity Analysis
#
# Time complexity: O(k^2).
# Space complexity: O(k).
# @param {Integer} row_index
# @return {Integer[]}
def get_row(row_index)
pascal = [[1]]
(1..row_index).each do |i|
pascal[i] = []
pascal[i][0] = pascal[i][i] = 1
(1...i).each do |j|
pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j]
end
end
pascal[row_index]
end
row_index = 3
print(get_row(row_index))
# => [1,3,3,1]
row_index = 0
print(get_row(row_index))
# => [1]
row_index = 1
print(get_row(row_index))
# => [1,1]

View file

@ -0,0 +1,45 @@
# Given an integer row_index, return the rowIndexth (0-indexed) row of the Pascal's triangle.
# Example 1:
#
# Input: row_index = 3
# Output: [1,3,3,1]
#
# Example 2:
#
# Input: row_index = 0
# Output: [1]
#
# Example 3:
#
# Input: row_index = 1
# Output: [1,1]
# Complexity Analysis
#
# Time complexity: O(k).
# Space complexity: O(k).
def get_row(row_index)
(0..row_index).map {|num| combination(row_index, num) }
end
def combination(num1, num2)
factorial(num1) / (factorial(num2) * factorial(num1 - num2))
end
def factorial(num)
(1..num).inject(1) { |res, i| res * i }
end
row_index = 3
print(get_row(row_index))
# => [1,3,3,1]
row_index = 0
print(get_row(row_index))
# => [1]
row_index = 1
print(get_row(row_index))
# => [1,1]