From 6a6d0b675a3df7eee3867782398ba186758e4abd Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Wed, 24 Mar 2021 08:19:11 -0700 Subject: [PATCH 1/5] Add challenge --- DIRECTORY.md | 1 + data_structures/arrays/richest_customer.rb | 31 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 data_structures/arrays/richest_customer.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index e8d246d..910dd30 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -13,6 +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) * [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) diff --git a/data_structures/arrays/richest_customer.rb b/data_structures/arrays/richest_customer.rb new file mode 100644 index 0000000..8fc8173 --- /dev/null +++ b/data_structures/arrays/richest_customer.rb @@ -0,0 +1,31 @@ +# Challenge name: Richest Customer +# +# You are given an m x n integer grid accounts where accounts[i][j] +# is the amount of money the i​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​​th​​​​ bank. +# +# Return the wealth that the richest customer has. +# A customer's wealth is the amount of money they have in all +# their bank accounts. The richest customer is the customer that +# has the maximum wealth. +# +# Example 1: +# Input: accounts = [[1,2,3],[3,2,1]] +# Output: 6 +# Explanation: +# 1st customer has wealth = 1 + 2 + 3 = 6 +# 2nd customer has wealth = 3 + 2 + 1 = 6 +# Both customers are considered the richest with a wealth of 6 +# each, so return 6. +# +# Example 2: +# Input: accounts = [[1,5],[7,3],[3,5]] +# Output: 10 +# Explanation: +# 1st customer has wealth = 6 +# 2nd customer has wealth = 10 +# 3rd customer has wealth = 8 +# The 2nd customer is the richest with a wealth of 10. +# +# Example 3: +# Input: accounts = [[2,8,7],[7,1,3],[1,9,5]] +# Output: 17 \ No newline at end of file From 187cdac8b137495c347c77ff0fa71b91ccb99a5b Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Thu, 25 Mar 2021 10:08:15 -0700 Subject: [PATCH 2/5] Add brute force solution --- DIRECTORY.md | 2 +- ...customer.rb => richest_customer_wealth.rb} | 29 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) rename data_structures/arrays/{richest_customer.rb => richest_customer_wealth.rb} (63%) diff --git a/DIRECTORY.md b/DIRECTORY.md index 910dd30..ea0a672 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) diff --git a/data_structures/arrays/richest_customer.rb b/data_structures/arrays/richest_customer_wealth.rb similarity index 63% rename from data_structures/arrays/richest_customer.rb rename to data_structures/arrays/richest_customer_wealth.rb index 8fc8173..23cf3de 100644 --- a/data_structures/arrays/richest_customer.rb +++ b/data_structures/arrays/richest_customer_wealth.rb @@ -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 i​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​​th​​​​ bank. @@ -28,4 +28,29 @@ # # Example 3: # Input: accounts = [[2,8,7],[7,1,3],[1,9,5]] -# Output: 17 \ No newline at end of file +# 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 \ No newline at end of file From 5e7efbb73d0e3f2c5973013d0f0a47b77c913cf2 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Thu, 25 Mar 2021 14:48:00 -0700 Subject: [PATCH 3/5] Add hash solution --- .../arrays/richest_customer_wealth.rb | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/richest_customer_wealth.rb b/data_structures/arrays/richest_customer_wealth.rb index 23cf3de..5842348 100644 --- a/data_structures/arrays/richest_customer_wealth.rb +++ b/data_structures/arrays/richest_customer_wealth.rb @@ -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 \ No newline at end of file +# => 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 From 08e35de175bd75ca2d285c36e5b65ed409510a99 Mon Sep 17 00:00:00 2001 From: Jessica Kwok <60627484+jsca-kwok@users.noreply.github.com> Date: Fri, 26 Mar 2021 10:54:37 -0700 Subject: [PATCH 4/5] Update data_structures/arrays/richest_customer_wealth.rb Co-authored-by: Vitor Oliveira --- data_structures/arrays/richest_customer_wealth.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/richest_customer_wealth.rb b/data_structures/arrays/richest_customer_wealth.rb index 5842348..7680ec9 100644 --- a/data_structures/arrays/richest_customer_wealth.rb +++ b/data_structures/arrays/richest_customer_wealth.rb @@ -58,7 +58,7 @@ puts find_richest_customer_wealth([[2,8,7],[7,1,3],[1,9,5]]) # # Approach 2: Hash # -# Time Complexity: +# Time Complexity: O(n) # def find_richest_customer_wealth(accounts) result_hash = {} From fded3fa4df2090ed3ae0d85207a209d59d0da2af Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Mon, 29 Mar 2021 14:33:12 -0700 Subject: [PATCH 5/5] Move approach to hash_table folder --- .../arrays/richest_customer_wealth.rb | 30 +--------- .../hash_table/richest_customer_wealth.rb | 59 +++++++++++++++++++ 2 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 data_structures/hash_table/richest_customer_wealth.rb diff --git a/data_structures/arrays/richest_customer_wealth.rb b/data_structures/arrays/richest_customer_wealth.rb index 7680ec9..4efabeb 100644 --- a/data_structures/arrays/richest_customer_wealth.rb +++ b/data_structures/arrays/richest_customer_wealth.rb @@ -31,7 +31,7 @@ # Output: 17 # -# Approach 1: Brute Force +# Approach: Brute Force # # Time Complexity: O(n) # @@ -54,31 +54,3 @@ 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: O(n) -# -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 diff --git a/data_structures/hash_table/richest_customer_wealth.rb b/data_structures/hash_table/richest_customer_wealth.rb new file mode 100644 index 0000000..11849e1 --- /dev/null +++ b/data_structures/hash_table/richest_customer_wealth.rb @@ -0,0 +1,59 @@ +# 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 i​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​​th​​​​ bank. +# +# Return the wealth that the richest customer has. +# A customer's wealth is the amount of money they have in all +# their bank accounts. The richest customer is the customer that +# has the maximum wealth. +# +# Example 1: +# Input: accounts = [[1,2,3],[3,2,1]] +# Output: 6 +# Explanation: +# 1st customer has wealth = 1 + 2 + 3 = 6 +# 2nd customer has wealth = 3 + 2 + 1 = 6 +# Both customers are considered the richest with a wealth of 6 +# each, so return 6. +# +# Example 2: +# Input: accounts = [[1,5],[7,3],[3,5]] +# Output: 10 +# Explanation: +# 1st customer has wealth = 6 +# 2nd customer has wealth = 10 +# 3rd customer has wealth = 8 +# The 2nd customer is the richest with a wealth of 10. +# +# Example 3: +# Input: accounts = [[2,8,7],[7,1,3],[1,9,5]] +# Output: 17 + +# +# Approach: Hash +# +# Time Complexity: O(n) +# +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