Sponsored
Sponsored
This approach involves iterating over each customer's accounts, calculating their total wealth by summing up the amounts, and keeping track of the maximum wealth found during these calculations. This method is simple and direct, leveraging basic iteration and comparison.
Time Complexity: O(m * n), where m is the number of customers and n is the number of banks.
Space Complexity: O(1), as we only use a few extra variables irrespective of input size.
1def maximumWealth(accounts):
2 max_wealth = 0
3 for customer in accounts:
4 current_wealth = sum(customer)
5 max_wealth = max(max_wealth, current_wealth)
6 return max_wealth
7
8print(maximumWealth([[2,8,7],[7,1,3],[1,9,5]]))
Uses Python's built-in sum
function to efficiently handle the sum calculation for each customer, comparing it to track the largest.
This approach considers a more functional programming style by mapping over the initial list to compute each customer's wealth, thereby generating an array of wealth values, which is in turn reduced (or simply searched) to obtain the maximum wealth.
Time Complexity: O(m * n)
Space Complexity: O(m)
1def maximumWealth(accounts):
2
Here, map
is used to apply the sum
function across all customers in accounts, and then max
finds the highest resulting sum.