|
| 1 | +package arrays.easy; |
| 2 | + |
| 3 | +/*** |
| 4 | + * Problem 1672 in Leetcode: https://leetcode.com/problems/richest-customer-wealth/ |
| 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. |
| 7 | + * |
| 8 | + * Return the wealth that the richest customer has. |
| 9 | + * |
| 10 | + * A customer's wealth is the amount of money they have in all their bank accounts. |
| 11 | + * |
| 12 | + * The richest customer is the customer that has the maximum wealth. |
| 13 | + * |
| 14 | + * Example 1: |
| 15 | + * Input: accounts = [[1,2,3],[3,2,1]] |
| 16 | + * Output: 6 |
| 17 | + * |
| 18 | + * Example 2: |
| 19 | + * Input: accounts = [[1,5],[7,3],[3,5]] |
| 20 | + * Output: 10 |
| 21 | + * |
| 22 | + * Example 3: |
| 23 | + * Input: accounts = [[2,8,7],[7,1,3],[1,9,5]] |
| 24 | + * Output: 17 |
| 25 | + */ |
| 26 | + |
| 27 | +public class MatrixTraversal { |
| 28 | + public static void main(String[] args) { |
| 29 | + int[][] accounts = {{2, 8, 7}, {7, 1, 3}, {1, 9, 5}}; |
| 30 | + System.out.println(maximumWealth(accounts)); |
| 31 | + } |
| 32 | + |
| 33 | + private static int maximumWealth(int[][] accounts) { |
| 34 | + int maxWealth = 0; |
| 35 | + int m = accounts.length, n = accounts[0].length; |
| 36 | + |
| 37 | + for (int i = 0; i < m; i++) { |
| 38 | + int sumWealth = 0; |
| 39 | + for (int j = 0; j < n; j++) { |
| 40 | + sumWealth += accounts[i][j]; |
| 41 | + } |
| 42 | + maxWealth = Math.max(maxWealth, sumWealth); |
| 43 | + } |
| 44 | + return maxWealth; |
| 45 | + } |
| 46 | +} |
0 commit comments