---
# System prepended metadata

title: Leetcode 1071. Greatest Common Divisor of Strings
tags: [leetcode]

---

---
title: Leetcode 1672. Richest Customer Wealth
tags: leetcode
---

# 1672. Richest Customer Wealth

## 題目連結

https://leetcode.com/problems/richest-customer-wealth/description/

## 題目敘述

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.

簡單來說要找出哪一個row的總合最大，回傳該總合。

**Example 1**

:::success
Input: accounts = [[1,2,3],[3,2,1]]
Output: 6
Explanation:
1st customer has wealth = 1 + 2 + 3 = 6
2nd customer has wealth = 3 + 2 + 1 = 6
Both customers are considered the richest with a wealth of 6 each, so return 6.
:::

**Example 2**

:::success
Input: accounts = [[1,5],[7,3],[3,5]]
Output: 10
::: 

**Example 3**

:::success
Input: accounts = [[2,8,7],[7,1,3],[1,9,5]]
Output: 17
::: 

**Constraints**

- m == accounts.length
- n == accounts[i].length
- 1 <= m, n <= 50
- 1 <= accounts[i][j] <= 100

---

## 解題思路

用兩層迴圈，第一層用來跑每個row，第二層用來跑該row的每個元素(col)，將結果加總，之後判斷是否比較大，若是，則更新最大值。

**java程式碼**

```cpp=
class Solution {
    public int maximumWealth(int[][] accounts) {
        int max = 0, sum = 0;
        for(int i = 0 ; i < accounts.length ; i++) {
            for(int j = 0 ; j < accounts[0].length ; j++) {
                sum += accounts[i][j];
            }
            if(sum > max)
                max = sum;
            sum = 0;
        }
        return max;
    }
}
```

**執行結果**

![](https://hackmd.io/_uploads/HkqgtfqW6.png)
