mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-13 08:01:03 +01:00
Add hash solution
This commit is contained in:
parent
187cdac8b1
commit
5e7efbb73d
1 changed files with 29 additions and 1 deletions
|
@ -53,4 +53,32 @@ puts find_richest_customer_wealth([[1,2,3],[3,2,1]])
|
|||
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
|
||||
# => 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
|
||||
|
|
Loading…
Reference in a new issue