Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions src/RichestCustomerWealth1672.java

This file was deleted.

29 changes: 29 additions & 0 deletions src/main/java/com/leetcode/easy/RichestCustomerWealth1672.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Tags: Array, Matrix
package com.leetcode.easy;

public class RichestCustomerWealth1672 {
/*
Let M be the number of customers and N be the number of banks.
Time complexity: O(M⋅N)
For each of the M customers,
we need to iterate over all N banks to find the sum of his/her wealth.
Hence, the total time complexity is O(M⋅N).

Space complexity: O(1)
We only need two variables personWealth and maxWealthSoFar
to store the wealth of the current customer,
and the highest wealth we have seen so far respectively.
Therefore, the space complexity is constant.
*/
public int maximumWealth(int[][] accounts) {
int maxWealthSoFar = 0;
for (int[] account : accounts) {
int personWealth = 0;
for (int money : account) {
personWealth += money;
}
maxWealthSoFar = Math.max(maxWealthSoFar, personWealth);
}
return maxWealthSoFar;
}
}
50 changes: 50 additions & 0 deletions src/test/java/com/leetcode/easy/RichestCustomerWealth1672Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.leetcode.easy;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class RichestCustomerWealth1672Test {
private RichestCustomerWealth1672 solution;

@BeforeEach
void setUp() {
solution = new RichestCustomerWealth1672();
}

@Test
void testMaximumWealth_Example1() {
int[][] accounts = {
{1, 2, 3},
{3, 2, 1}
};
int expected = 6;
int result = solution.maximumWealth(accounts);
assertEquals(expected, result);
}

@Test
void testMaximumWealth_Example2() {
int[][] accounts = {
{1, 5},
{7, 3},
{3, 5}
};
int expected = 10;
int result = solution.maximumWealth(accounts);
assertEquals(expected, result);
}

@Test
void testMaximumWealth_Example3() {
int[][] accounts = {
{2, 8, 7},
{7, 1, 3},
{1, 9, 5}
};
int expected = 17;
int result = solution.maximumWealth(accounts);
assertEquals(expected, result);
}
}