Add brute force solution

This commit is contained in:
Jessica Kwok 2021-03-25 10:08:15 -07:00
parent 6a6d0b675a
commit 187cdac8b1
2 changed files with 28 additions and 3 deletions

View file

@ -13,7 +13,7 @@
* [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb)
* [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb)
* [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb)
* [Richest Customer](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/richest_customer.rb)
* [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/richest_customer.rb)
* [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.rb)
* [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb)
* [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb)

View file

@ -1,4 +1,4 @@
# Challenge name: Richest Customer
# Challenge name: Richest Customer Wealth
#
# You are given an m x n integer grid accounts where accounts[i][j]
# is the amount of money the ith customer has in the jth bank.
@ -28,4 +28,29 @@
#
# Example 3:
# Input: accounts = [[2,8,7],[7,1,3],[1,9,5]]
# Output: 17
# Output: 17
#
# Approach 1: Brute Force
#
# Time Complexity: O(n)
#
def find_richest_customer_wealth(accounts)
summed_accounts = []
accounts.each do |customer|
summed = 0
customer.each do |account|
summed += account
end
summed_accounts.push(summed)
end
summed_accounts.sort.pop()
end
puts find_richest_customer_wealth([[1,2,3],[3,2,1]])
# => 6
puts find_richest_customer_wealth([[1,5],[7,3],[3,5]])
# => 10
puts find_richest_customer_wealth([[2,8,7],[7,1,3],[1,9,5]])
# => 17