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..b909c85 --- /dev/null +++ b/src/test/java/com/leetcode/easy/RichestCustomerWealth1672Test.java @@ -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); + } +}