Python - simple & fast with explanation - no permutation
Please upvote if you like my solution. Let me know in the comments if you have any suggestions to increase performance or readability. \n\nNotice that:\n1. We d
Please upvote if you like my solution. Let me know in the comments if you have any suggestions to increase performance or readability. \n\n**Notice** that:\n1. We do not need to generate all possible combinations of spliting the input into two numbers. In the ideal solution, num1 and num2 will always be of equal length.\n2. We will step by step build num1 and num2 as we go through the algorithm. It is not necessary to generate all combinations of num1 and num2 (with both numbers having equal length).\n3. We do not need to save num1 and num2 in a variable. We can keep adding to the total sum as we go through the algorithm. \n4. The algorithm below works for any input length. If you want you can simplify it, since you know the input length is 4. \n\n**My algorithm** goes as follows:\n1. convert the input into a list of digits\n2. sort the list of digits in descending order\n3. iterate through the list of digits\n3.1 with each iteration, multiply the current digit with 10*current position in num1/num2. \n3.2 add the current digit to the result\n3.3 if you already added an even number of digits to the total of results, then increase the current position by one\n4. return result\n\n```\nclass Solution:\n    def minimumSum(self, num: int) -> int:\n        num = sorted(str(num),reverse=True)\n        n = len(num)    \n        res = 0\n        even_iteration = False\n        position = 0\n        for i in range(n):\n            res += int(num[i])*(10**position)\n            if even_iteration:\n                position += 1\n                even_iteration = False\n            else:\n                even_iteration = True\n        return res\n```\n\nif you know that the input is going to be exaclty 4 digits, you can simplify it:\n\n```\nclass Solution:\n    def minimumSum(self, num: int) -> int:\n        num = sorted(str(num),reverse=True)\n        return int(num[0]) + int(num[1]) + int(num[2])*10 + int(num[3])*10\n```\n
['Python', 'Python3']