Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 272b7ca

Browse files
committed
added task #1672 solution and explanation of it
1 parent 4ee605a commit 272b7ca

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/task_1672/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Intuition
2+
It seems easy. It's interesting to count sum of a row of given grid. It's a sum of money in all banks for one customer. Let's find maximum in set of "row-sums".
3+
4+
# Approach
5+
Row sum is sum of money in all banks for one customer. Let's save this value to a ```moneyOfCurrentCustomer``` variable. After that, let's compare: is this value greater than ```moneyOfRichestCustomer``` value? If yes, let's re-write value of maximum. After that, it's necessary to save 0 to ```moneyOfCurrentCustomer``` variable (we are not calculating money sum for all people!) and repeat iteration.
6+
7+
# Complexity
8+
- Time complexity:
9+
$O(n*m)$, where $n$ is number of rows, $m$ is number of columns in the grid. Necessary to iterate by all rows and all columns for finding an answer.
10+
11+
- Space complexity:
12+
$O(1)$. There are only two ```int``` type variables for storing all necessary data.
13+
14+
# Code
15+
```
16+
class Solution {
17+
18+
public int maximumWealth(int[][] accounts) {
19+
int moneyOfRichestCustomer = 0;
20+
int moneyOfCurrentCustomer = 0;
21+
for (int i = 0; i < accounts.length; i++) {
22+
for (int j = 0; j < accounts[i].length; j++) {
23+
moneyOfCurrentCustomer += accounts[i][j];
24+
}
25+
if (moneyOfCurrentCustomer > moneyOfRichestCustomer) {
26+
moneyOfRichestCustomer = moneyOfCurrentCustomer;
27+
}
28+
moneyOfCurrentCustomer = 0;
29+
}
30+
return moneyOfRichestCustomer;
31+
}
32+
33+
}
34+
```

src/task_1672/Solution.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 i​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​​th​​​​ 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

Comments
 (0)