Solving Richest Customer Wealth

Bryam Vicente
4 min readMar 15, 2021

--

It took me quite some time to solve this problem because I didn’t know how to approach it.

As mentioned in my previous blog, the key in solving algorithm problems is to understand the question and look out for any clues that can be given. Note that this problem will be solved in JavaScript!

At first, I wasn’t able to understand the prompt, but the examples do a great job at explaining what the input and output should be and why. The question is asking us to return the wealth of the richest customer. Each subarray inside of the array represents one customer and the output would be coming from the subarray that has the maximum sum. I was able to solve this problem with a time complexity of On² because I knew that I had to iterate over the accounts array and then iterate over the subarrays inside of accounts. Please feel free to reach out or leave a comment on this blog if there are better solutions for this problem!

Because we have to return the wealth of the richest customer, I created a variable that represents the wealth and set that equal to zero. This is what my code looks like at this moment:

The next step is to iterate over the accounts array and then iterate over the subarrays inside of accounts. This is what the code should look like so far:

For the nested for loop on line 5, we’re supposed to iterate over the subarrays which is why I wrote j < accounts[i].length.

We currently have one variable (richestCustomer) that represents the sum of the richest customer, but we also need to have a variable that represents the sum of each customer, not just the richest. That’s why I decided to create a variable customerWealth set equal to zero that’s only defined inside of the first for loop.

Inside of the second for loop, customerWealth has to be set to the sum of accounts[i][j] . Here’s a visual of the current code:

In case you’re wondering what accounts[i][j] looks like, I recommend placing a console.log(accounts[i][j]) between lines 6 and 7. Here’s what accounts[i][j] looks like when console.logging:

This is the output that I want to get when console.logging accounts[i][j], because that’s the same exact set of numbers that I’m using to test if my function is working.

Now that customerValue has its new value, richestCustomer needs to be set equal to the maximum number between richestCustomer which is currently zero and customerWealth. Once that’s done, I just have to return richestCustomer outside of both for loops. Here’s the code:

It’s always best practice to use different test cases to ensure that the function is working properly! Here are the results when submitting the solution on LeetCode:

As mentioned before, please reach out if there are better ways of solving this LeetCode problem. For more info on the Math library, for loops, and subarrays, make sure to check out the links below!

Resources:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response