3-line Python


  • 0

    Took me several minutes to verify this is correct since any reordering in denominator will increase the denominator for the result.
    This problem should be for high school students who begin learning four arithmetic operations.

    class Solution(object):
        def optimalDivision(self, nums):
            """
            :type nums: List[int]
            :rtype: str
            """
            if len(nums) < 3:
                return "/".join(map(str, nums))
            return "%s/(%s)" % (nums[0], "/".join(map(str, nums[1:])))
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.