Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /1672 - Richest Customer Wealth
    1672 - Richest Customer Wealth
    1672 - Richest Customer Wealth
    1672 - Richest Customer Wealth

    1672 - Richest Customer Wealth

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/richest-customer-wealth/

    Solution 1 - Iterative Sum with Max

    Iterate through each customer's accounts, calculate the sum of each row, and track the maximum wealth seen so far.

    TimeComplexity:O(m×n)TimeComplexity: O(m \times n)TimeComplexity:O(m×n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)

    Solution 2 - Pythonic One-Liner using max() and sum()

    Use Python built-ins max() with a generator expression and sum() to compute the result in a single concise line.

    TimeComplexity:O(m×n)TimeComplexity: O(m \times n)TimeComplexity:O(m×n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)

    Solution 3 - Using map() with sum()

    Use map() to apply sum() across all rows, then take the max() of the resulting iterator.

    TimeComplexity:O(m×n)TimeComplexity: O(m \times n)TimeComplexity:O(m×n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    """ 1672.1 - Richest Customer Wealth - Solution 1 - Iterative Sum with Max """
    
    #####################################################################################
    # Imports
    #####################################################################################
    from typing import List
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def maximumWealth(self, accounts: List[List[int]]) -> int:
            """Maximum Wealth Function"""
            max_wealth = 0
            for customer in accounts:
                wealth = 0
                for account in customer:
                    wealth += account
                max_wealth = max(max_wealth, wealth)
            return max_wealth
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().maximumWealth([[1, 2, 3], [3, 2, 1]]))
        print(Solution().maximumWealth([[1, 5], [7, 3], [3, 5]]))
        print(Solution().maximumWealth([[2, 8, 7], [7, 1, 3], [1, 9, 5]]))
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 1672.2 - Richest Customer Wealth - Solution 2 - Pythonic One-Liner """
    
    #####################################################################################
    # Imports
    #####################################################################################
    from typing import List
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def maximumWealth(self, accounts: List[List[int]]) -> int:
            """Maximum Wealth Function"""
            return max(sum(customer) for customer in accounts)
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().maximumWealth([[1, 2, 3], [3, 2, 1]]))
        print(Solution().maximumWealth([[1, 5], [7, 3], [3, 5]]))
        print(Solution().maximumWealth([[2, 8, 7], [7, 1, 3], [1, 9, 5]]))
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 1672.3 - Richest Customer Wealth - Solution 3 - Using map() """
    
    #####################################################################################
    # Imports
    #####################################################################################
    from typing import List
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def maximumWealth(self, accounts: List[List[int]]) -> int:
            """Maximum Wealth Function"""
            return max(map(sum, accounts))
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().maximumWealth([[1, 2, 3], [3, 2, 1]]))
        print(Solution().maximumWealth([[1, 5], [7, 3], [3, 5]]))
        print(Solution().maximumWealth([[2, 8, 7], [7, 1, 3], [1, 9, 5]]))
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()