You are given an 
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.
m x n integer grid accounts where accounts[i][j] is the amount of money the ith the customer has in the jth 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.
Example 1:
Input: accounts = [[1,2,3],[3,2,1]]
Output: 6Approach
Java
public class RichestCustomerWealth {public static void main(String[] args) {int[][] accounts = { { 1, 2, 3 }, { 3, 2, 1 } };System.out.println(maximumWealth(accounts));}static int maximumWealth(int[][] accounts) {int ans = Integer.MIN_VALUE;for (int i = 0; i < accounts.length; i++) {int sum = 0;for (int j = 0; j < accounts[i].length; j++)sum += accounts[i][j];ans = Math.max(ans, sum);}return ans;}}
C++
#include <bits/stdc++.h>using namespace std;int maximumWealth(vector<vector<int>> &accounts){int ans = INT_MIN;for (int i = 0; i < accounts.size(); i++){int sum = 0;for (int j = 0; j < accounts[i].size(); j++)sum += accounts[i][j];ans = max(ans, sum);}return ans;}int main(){vector<vector<int>> accounts = {{1, 2, 3}, {3, 2, 1}};cout << maximumWealth(accounts);return 0;}
No comments:
Post a Comment