Lets Code Everyday - Day6

Lets Code Everyday - Day6

Β·

5 min read

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 :

  1. Initialize a variable maxWealth to 0, which will hold the maximum wealth value found so far.

  2. Start a for loop with a loop variable i that iterates over each row of the accounts array using the length of accounts to determine the number of iterations.

  3. Within each iteration of the outer loop, extract the row from accounts using the index i and store it in a temporary array called tempArray.

  4. Initialize a temporary variable tempValue to 0, which will hold the sum of the account balances for the current customer.

  5. Start another for loop with a loop variable j that iterates over each element of tempArray using the length of tempArray to determine the number of iterations.

  6. Within each iteration of the inner loop, add up the values of each element in tempArray to tempValue using tempValue+=tempArray[j];.

  7. After the inner loop completes, check whether tempValue is greater than or equal to maxWealth using an if statement. If tempValue is greater than or equal to maxWealth, set maxWealth to tempValue.

  8. 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, where accounts.length is the number of rows in the 2D array accounts.

  • The inner loop iterates tempArray.length times, where tempArray.length is the number of columns in the current row of accounts.

  • 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) where N is the number of rows and M 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 and tempValue each take up to M space, where M is the maximum number of columns in accounts.

  • 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:

  1. Initialize a variable maxWealth to 0, which will hold the maximum wealth value found so far.

  2. Start a for loop with a loop variable account that iterates over each row of the accounts array using the enhanced for loop.

  3. Within each iteration of the outer loop, account represents the current row of accounts.

  4. Initialize a temporary variable sum to 0, which will hold the sum of the account balances for the current customer.

  5. Start another for loop with a loop variable money that iterates over each element of the account array using the enhanced for loop.

  6. Within each iteration of the inner loop, add up the values of each element in account to sum using sum+=money;.

  7. After the inner loop completes, check whether sum is greater than maxWealth using Math.max function. This function returns the maximum of two integers. If sum is greater than maxWealth, set maxWealth to sum.

  8. 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.

Did you find this article valuable?

Support Learn-->Share-->Learn by becoming a sponsor. Any amount is appreciated!

Β