LeetCode Problem 1672: Solving the Richest Customer Wealth Challenge
SeriesLeetCodepart 2 of 10
- LeetCode Problem 1480: Sum
- LeetCode Problem 1672: Solving the Richest Customer Wealth Challenge
- Leetcode Question 412: Fizz Buzz - My Simple Solution
- LeetCode Question 1342: Number of Steps to Reduce a Number to Zero - My Simple Solution
- Solving LeetCode Problem 876: Middle of the Linked List
- LeetCode Question 383: Ransom Note Solution Breakdown
- LeetCode 977: Squares of a Sorted Array Solution
- LeetCode 1295: Find Numbers with Even Number of Digits
- LeetCode 485 - Finding Max Consecutive Ones
- Is Bitwise & Better than Arithmetic to Determine Even or Odd? Let’s Dive In!
On this page
The Problem
You’re given a 2D array accounts, where each row represents a customer, and each column represents the amount of money they have in different banks. Your job is to find out which customer is the richest. In other words, you need to find out the maximum wealth among all customers.
Visualize it like this:
accounts = [[1, 2, 3],
[3, 2, 1],
[4, 5, 6]]
Here, the first customer has wealth of 6, the second has 6 as well, and the third has 15. So the answer is 15.
The Solution
Here’s how I went about solving this thing in Java. I mean, it’s pretty basic but hey, it works like a charm! Here’s the code:
class Solution {
public int maximumWealth(int[][] accounts) {
int richKid = 0; // Store the wealth of the richest customer
for (int i = 0; i < accounts.length; i++) { // Loop through each customer
int custWealth = 0; // Reset customer's wealth for each customer
for (int j = 0; j < accounts[0].length; j++) { // Loop through each bank account
custWealth += accounts[i][j]; // Add the wealth for this customer
}
// Update richKid if current customer's wealth is greater
richKid = Math.max(richKid, custWealth);
}
return richKid; // Return the maximum wealth found
}
}
Complexity
- Time Complexity: O(m * n), where m is the number of customers and n is the number of banks. Clearly, we’re checking every account for every customer.
- Space Complexity: O(1), since we’re using only a few additional variables for calculations.