Add hash solution

This commit is contained in:
Jessica Kwok 2021-03-25 14:48:00 -07:00
parent 187cdac8b1
commit 5e7efbb73d

View file

@ -54,3 +54,31 @@ 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
#
# Approach 2: Hash
#
# Time Complexity:
#
def find_richest_customer_wealth(accounts)
result_hash = {}
accounts.each_with_index do |customer, i|
result_hash[i] = customer.sum
end
highest_value = 0
result_hash.each do |k, v|
if v > highest_value
highest_value = v
end
end
highest_value
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