DEV Community


Posted on

Solving Leetcode's "Richest Customer Wealth" Challange with Javascript

This problem was quite challenging for me at the beginning, cos one needs to have a fairly good understanding of how multi-dimensional arrays work. Eventually, I was able to figure it out, and I will walk you through my solution.

The question states thus;

You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the I​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​​th​​​​ 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.


Input: accounts = [[1,5],[7,3],[3,5]]
Output: 10
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.
Enter fullscreen mode Exit fullscreen mode

Firstly as with all programming challenges, we first need to understand the problem here, so we can come up with an algorithm for the solution.

  • We are expected to iterate through each embedded array.

  • Since it's a multi-dimensional array, we would need a nested loop

  • Get the sum of elements in each array

  • Push these sums into a new array

  • Return the highest number in the new array

function richestCus(accounts) {
    let newArr = [];
    for(let i = 0; i < accounts.length; i++) {
        let sum = 0;
        for(let j = 0; j < accounts[i].length; j++) {
            sum += accounts[i][j]
    return Math.max.apply(null, newArr)

Enter fullscreen mode Exit fullscreen mode

What we did above was

  • We initialized a new array variable let newArr = []; inside the function. So that we can push our results into the array
  • Iterate through the outer array. If we log/print the result of the outer array we will see that the outer array has been broken down into single arrays. for example if;
accounts = [[1,2,3],[1,2,3],[6, 8, 10]]
console.log(accounts)// [1,2,3][1,2,3][6, 8, 10]
Enter fullscreen mode Exit fullscreen mode
  • Inside the outer loop, initialize a variable let sum = 0, so that we can use it to add up elements in each array
  • Create a nested loop inside the initial loop, so that we can have access to the elements in each sub-array.

  • Add the elements in each sub-array inside the nested loop and assign the result to the sum variable.

  • At the end of each iteration of the nested loop, the sum result is pushed unto the new array variable that was created.

  • Finally, to get the highest number in our new array, we call the Math.max() function on our array.
    The Math.max() function returns the largest of zero or more numbers, and we can pass any number of arguments.

console.log(Math.max(5,9,7,3)); // logs 9

But you can't pass an array of numbers on the Math.max() function like this

var num = [5,9,7,3];
console.log(Math.max(num)); // logs NaN

Enter fullscreen mode Exit fullscreen mode

Thus we introduce the apply() method which accepts arguments in an array, like
Math.max.apply(null, newArr)

N:B => The first argument to the apply() method, sets the value to this, and since we don't have a need for it, hence we passed null as our first argument.

With the above explanation, I hope you've learned a thing or two from this article.

PS: I'm open to more efficient solutions


Top comments (0)