Hello there π, enthusiasts.
I hope this blog inspires people who come across it and want to start problem-solving from the ground up. Why am I telling you this? Because it demonstrates that a person does not have to be the greatest runner of all time to win a race; simply being a runner is sufficient.He/she will undoubtedly win their race one day.
So don't lose hope and don't give up on yourself.
Question - 6 :
Richest Customer Wealth
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 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
Initial approach :-
class Solution {
public int maximumWealth(int[][] accounts) {
int maxWealth=0;
for(int i=0;i<accounts.length;i++){
int[] tempArray= accounts[i];
int tempValue=0;
for(int j=0;j<tempArray.length;j++){
tempValue+=tempArray[j];
}
if(tempValue>=maxWealth)
maxWealth=tempValue;
}
return maxWealth;
}
}
Step-by-step explanation :
Initialize a variable
maxWealth
to 0, which will hold the maximum wealth value found so far.Start a for loop with a loop variable
i
that iterates over each row of theaccounts
array using the length ofaccounts
to determine the number of iterations.Within each iteration of the outer loop, extract the row from
accounts
using the indexi
and store it in a temporary array calledtempArray
.Initialize a temporary variable
tempValue
to 0, which will hold the sum of the account balances for the current customer.Start another for loop with a loop variable
j
that iterates over each element oftempArray
using the length oftempArray
to determine the number of iterations.Within each iteration of the inner loop, add up the values of each element in
tempArray
totempValue
usingtempValue+=tempArray[j];
.After the inner loop completes, check whether
tempValue
is greater than or equal tomaxWealth
using an if statement. IftempValue
is greater than or equal tomaxWealth
, setmaxWealth
totempValue
.After all the iterations are completed, return
maxWealth
as the maximum wealth value found among all customers.
Time complexity:
The outer loop iterates
accounts.length
times, whereaccounts.length
is the number of rows in the 2D arrayaccounts
.The inner loop iterates
tempArray.length
times, wheretempArray.length
is the number of columns in the current row ofaccounts
.Therefore, the total number of iterations for both loops is given by the sum of the product of the number of rows and columns in
accounts
, i.e.,O(N*M)
whereN
is the number of rows andM
is the number of columns.The other statements in the code take constant time, so the overall time complexity is
O(N*M)
.
Space complexity:
The space complexity of the code is determined by the variables declared in the code.
The variable
maxWealth
takes constant space.The variables
tempArray
andtempValue
each take up toM
space, whereM
is the maximum number of columns inaccounts
.Therefore, the overall space complexity is
O(M)
.
In summary, the time complexity of the code is O(N*M)
and the space complexity is O(M)
.
An altered version of the initial approach :-
class Solution {
public int maximumWealth(int[][] accounts) {
int maxWealth=0;
for(int[] account:accounts){
int sum=0;
for(int money:account)
sum+=money;
maxWealth=Math.max(sum,maxWealth);
}
return maxWealth;
}
}
Step-by-step explanation:
Initialize a variable
maxWealth
to 0, which will hold the maximum wealth value found so far.Start a for loop with a loop variable
account
that iterates over each row of theaccounts
array using the enhanced for loop.Within each iteration of the outer loop,
account
represents the current row ofaccounts
.Initialize a temporary variable
sum
to 0, which will hold the sum of the account balances for the current customer.Start another for loop with a loop variable
money
that iterates over each element of theaccount
array using the enhanced for loop.Within each iteration of the inner loop, add up the values of each element in
account
tosum
usingsum+=money;
.After the inner loop completes, check whether
sum
is greater thanmaxWealth
usingMath.max
function. This function returns the maximum of two integers. Ifsum
is greater thanmaxWealth
, setmaxWealth
tosum
.After all the iterations are completed, return
maxWealth
as the maximum wealth value found among all customers.
The time complexity of the code is O(N*M)
, where N
is the number of rows and M
is the number of columns in the 2D array accounts
. This is because the code iterates through all the elements in the array exactly once, using two nested loops.
The space complexity of the code is O(1)
. This is because the code only uses a constant amount of extra memory to store the variables maxWealth
, sum
, account
, and money
. The space used by these variables does not depend on the size of the input array.
Conclusion :-
Both the code solutions solve the same problem and have the same time complexity of O(N*M)
where N
is the number of rows and M
is the number of columns in the input array. However, the second solution is more concise and easier to read and understand as it uses a for-each loop for the outer loop which makes the code cleaner and easier to understand. Additionally, the second solution uses less space as it doesn't create a new array in the inner loop, whereas the first solution does create a new array tempArray
for every row. Overall, the second solution is a better implementation.