|
| 1 | +package task_1672; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +/* |
| 6 | +You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the ith customer has in the jth bank. Return the wealth that the richest customer has. |
| 7 | +
|
| 8 | +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. |
| 9 | +
|
| 10 | +
|
| 11 | +
|
| 12 | +Example 1: |
| 13 | +
|
| 14 | +Input: accounts = [[1,2,3],[3,2,1]] |
| 15 | +Output: 6 |
| 16 | +Explanation: |
| 17 | +1st customer has wealth = 1 + 2 + 3 = 6 |
| 18 | +2nd customer has wealth = 3 + 2 + 1 = 6 |
| 19 | +Both customers are considered the richest with a wealth of 6 each, so return 6. |
| 20 | +Example 2: |
| 21 | +
|
| 22 | +Input: accounts = [[1,5],[7,3],[3,5]] |
| 23 | +Output: 10 |
| 24 | +Explanation: |
| 25 | +1st customer has wealth = 6 |
| 26 | +2nd customer has wealth = 10 |
| 27 | +3rd customer has wealth = 8 |
| 28 | +The 2nd customer is the richest with a wealth of 10. |
| 29 | +Example 3: |
| 30 | +
|
| 31 | +Input: accounts = [[2,8,7],[7,1,3],[1,9,5]] |
| 32 | +Output: 17 |
| 33 | +
|
| 34 | +
|
| 35 | +Constraints: |
| 36 | +
|
| 37 | +m == accounts.length |
| 38 | +n == accounts[i].length |
| 39 | +1 <= m, n <= 50 |
| 40 | +1 <= accounts[i][j] <= 100 |
| 41 | + */ |
| 42 | +public class Solution { |
| 43 | + |
| 44 | + public int maximumWealth(int[][] accounts) { |
| 45 | + int moneyOfRichestCustomer = 0; |
| 46 | + int moneyOfCurrentCustomer = 0; |
| 47 | + for (int i = 0; i < accounts.length; i++) { |
| 48 | + for (int j = 0; j < accounts[i].length; j++) { |
| 49 | + moneyOfCurrentCustomer += accounts[i][j]; |
| 50 | + } |
| 51 | + if (moneyOfCurrentCustomer > moneyOfRichestCustomer) { |
| 52 | + moneyOfRichestCustomer = moneyOfCurrentCustomer; |
| 53 | + } |
| 54 | + moneyOfCurrentCustomer = 0; |
| 55 | + } |
| 56 | + return moneyOfRichestCustomer; |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments