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.
1#include <stdio.h>
2
3int maximumWealth(int accounts[50][50], int m, int n) {
4 int maxWealth = 0;
5 for (int i = 0; i < m; ++i) {
6 int currentWealth = 0;
7 for (int j = 0; j < n; ++j) {
8 currentWealth += accounts[i][j];
9 }
10 if (currentWealth > maxWealth) {
11 maxWealth = currentWealth;
12 }
13 }
14 return maxWealth;
15}
16
17int main() {
18 int accounts[3][3] = {{2,8,7},{7,1,3},{1,9,5}};
19 printf("%d\n", maximumWealth(accounts, 3, 3));
20 return 0;
21}
The code sums each row's elements in the accounts
grid to calculate the wealth of each customer. The variable maxWealth
keeps track of the highest wealth encountered.
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 return
Here, map
is used to apply the sum
function across all customers in accounts, and then max
finds the highest resulting sum.