1672. Richest Customer Wealth | Day 3 | Arrays | Leetcode
https://github.com/vilgad/DSA-Days
Table of contents
Problem Statement
You are given an m x n
integer grid accounts
where accounts[i][j]
is the amount of money the i<sup>th</sup>
customer has in the j<sup>th</sup>
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:
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.
Constraints:
m == accounts.length
n == accounts[i].length
1 <= m, n <= 50
1 <= accounts[i][j] <= 100
Explanation
Here we need to find the richest customer wealth i.e. the amount of money the richest person has.
In the accounts array,
i -> denotes the customer
j -> denotes the bank
accounts[i][j] -> denotes the money ith person has in jth account
For example, suppose two people named Luffy and Naruto has money in two different banks say SBI and HDFC
so to access Luffy's money from SBI bank we will write accounts[Luffy][SBI]
Hope you got it 😆
Solution
Now, to find the richest person's wealth
First, we will traverse the whole array.
find the sum of the money of each person
Then find who has the highest amount among them
public int maximumWealth(int[][] accounts) {
int maxWealth = 0; // to store the hihghest wealth
// Brute Force
for(int i=0; i<accounts.length; i++) {
int sum = 0;
// to find the total amount of money
for (int j=0; j<accounts[i].length; ++j) {
sum += accounts[i][j];
}
// to find the highest wealth
if (sum > maxWealth)
maxWealth = sum;
}
return maxWealth;
}
Time Complexity: O(n^2)
Thanks for reading this blog, I Hope it helped you in understanding the question better.
If you want more solutions like this then subscribe to my newsletter, I am making a series of DSA-Days in which I will post one solution a day of leetcode question. The series will cover all types of questions like Easy/Medium/Hard and they will be topic-wise.