From ff5fe1cbe81bb8a9296d3764e7d003e04acf66f5 Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Tue, 9 Sep 2025 11:47:45 +0800 Subject: [PATCH 1/2] Add unit tests for the "Richest Customer Wealth" problem. --- src/RichestCustomerWealth1672.java | 18 ------- .../easy/RichestCustomerWealth1672.java | 29 +++++++++++ .../easy/RichestCustomerWealth1672Test.java | 48 +++++++++++++++++++ 3 files changed, 77 insertions(+), 18 deletions(-) delete mode 100644 src/RichestCustomerWealth1672.java create mode 100644 src/main/java/com/leetcode/easy/RichestCustomerWealth1672.java create mode 100644 src/test/java/com/leetcode/easy/RichestCustomerWealth1672Test.java diff --git a/src/RichestCustomerWealth1672.java b/src/RichestCustomerWealth1672.java deleted file mode 100644 index 84dd746..0000000 --- a/src/RichestCustomerWealth1672.java +++ /dev/null @@ -1,18 +0,0 @@ -public class RichestCustomerWealth1672 { - public int maximumWealth(int[][] accounts) { - int res = 0; - for (int r = 0; r < accounts.length; r++) { - int personWealth = 0; - for (int c = 0; c < accounts[0].length; c++) { - personWealth += accounts[r][c]; - } - res = Math.max(res, personWealth); - } - return res; - } - - public static void main(String[] args) { - int[][] accounts = {{1, 2, 3}, {3, 2, 1}}; - System.out.println(new RichestCustomerWealth1672().maximumWealth(accounts)); // 6 - } -} diff --git a/src/main/java/com/leetcode/easy/RichestCustomerWealth1672.java b/src/main/java/com/leetcode/easy/RichestCustomerWealth1672.java new file mode 100644 index 0000000..ad44116 --- /dev/null +++ b/src/main/java/com/leetcode/easy/RichestCustomerWealth1672.java @@ -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; + } +} diff --git a/src/test/java/com/leetcode/easy/RichestCustomerWealth1672Test.java b/src/test/java/com/leetcode/easy/RichestCustomerWealth1672Test.java new file mode 100644 index 0000000..2d4aaee --- /dev/null +++ b/src/test/java/com/leetcode/easy/RichestCustomerWealth1672Test.java @@ -0,0 +1,48 @@ +package com.leetcode.easy; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +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); + assert result == expected : "Expected " + expected + " but got " + result; + } + + @Test + void testMaximumWealth_Example2() { + int[][] accounts = { + {1, 5}, + {7, 3}, + {3, 5} + }; + int expected = 10; + int result = solution.maximumWealth(accounts); + assert result == expected : "Expected " + expected + " but got " + result; + } + + @Test + void testMaximumWealth_Example3() { + int[][] accounts = { + {2, 8, 7}, + {7, 1, 3}, + {1, 9, 5} + }; + int expected = 17; + int result = solution.maximumWealth(accounts); + assert result == expected : "Expected " + expected + " but got " + result; + } +} From 8a4ad462775b969a11a42953bd843cd6fe96875a Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Tue, 9 Sep 2025 11:52:33 +0800 Subject: [PATCH 2/2] Refactor assertions in "Richest Customer Wealth" tests to use assertEquals for better test failure reporting and IDE integration. --- .../com/leetcode/easy/RichestCustomerWealth1672Test.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/leetcode/easy/RichestCustomerWealth1672Test.java b/src/test/java/com/leetcode/easy/RichestCustomerWealth1672Test.java index 2d4aaee..b909c85 100644 --- a/src/test/java/com/leetcode/easy/RichestCustomerWealth1672Test.java +++ b/src/test/java/com/leetcode/easy/RichestCustomerWealth1672Test.java @@ -3,6 +3,8 @@ 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; @@ -19,7 +21,7 @@ void testMaximumWealth_Example1() { }; int expected = 6; int result = solution.maximumWealth(accounts); - assert result == expected : "Expected " + expected + " but got " + result; + assertEquals(expected, result); } @Test @@ -31,7 +33,7 @@ void testMaximumWealth_Example2() { }; int expected = 10; int result = solution.maximumWealth(accounts); - assert result == expected : "Expected " + expected + " but got " + result; + assertEquals(expected, result); } @Test @@ -43,6 +45,6 @@ void testMaximumWealth_Example3() { }; int expected = 17; int result = solution.maximumWealth(accounts); - assert result == expected : "Expected " + expected + " but got " + result; + assertEquals(expected, result); } }