Date: 2026-06-06
Time: 19:15
This file solves LeetCode 2578: Split With Minimum Sum. Given a positive integer, split its digits into two new numbers such that their sum is minimized. The file owns both the solution implementation and its unit tests, following the repo's convention of colocating solution + tests in a single solution.py.
minsumoftwonumbers(num: int) -> intThe core solver. Contract: accepts a positive integer (10 <= num <= 10^9), returns the minimum possible sum after splitting its digits into exactly two numbers.
Algorithm (greedy, sort-and-deal):
1. Convert num to its digit characters and sort them ascending.
2. Deal digits round-robin into two accumulators (parts[0] and parts[1]), alternating by index.
3. Convert both strings back to integers and return their sum.
This works because the greedy optimal strategy is: place the smallest digits in the highest-significance positions, and distribute evenly across both numbers so neither accumulates disproportionate magnitude. Sorting ensures smallest-first; round-robin ensures balanced length (differ by at most 1 digit).
TestMinSumOfTwoNumbersSix test cases covering: the two LeetCode examples, the minimum-length input (2 digits), repeated digits, a large 9-digit input, and a number with embedded zeros (verifying that leading zeros in string parts are handled correctly by int() conversion).
minimum-sum-of-four-digit-number-after-splitting-digits/.parts[i % 2] += d). This avoids manual place-value math at the cost of a string-to-int conversion at the end.python solution.py or pytest.Imports: Only unittest from stdlib — no external dependencies.
Imported by: The testsolution.py files listed in the "Imported By" section don't actually import *this* file — that list appears to be a repo-wide cross-reference artifact. The real consumer is split-with-minimum-sum/testsolution.py, which likely imports minsumoftwonumbers from this module.
num=4325
→ str(num) = "4325"
→ sorted("4325") = ['2', '3', '4', '5']
→ deal: i=0 → parts[0]="2", i=1 → parts[1]="3", i=2 → parts[0]="24", i=3 → parts[1]="35"
→ int("24") + int("35") = 59
The round-robin assignment parts[i % 2] means even-indexed (0th, 2nd, ...) sorted digits go to parts[0] and odd-indexed go to parts[1]. Since digits are sorted ascending, the smallest digit becomes the leading digit of parts[0], second-smallest leads parts[1], and so on — minimizing place-value contribution.
len(parts[0]) - len(parts[1]) is 0 or 1. This prevents one number from having significantly more digits (and thus magnitude) than the other.int("02") yields 2 in Python, so inputs like 2030 (digits [0,0,2,3]) produce correct results without special handling.None. The function trusts its caller to provide a valid positive integer per the LeetCode constraint (10 <= num <= 10^9). No validation, no exception handling. Invalid inputs (negative, zero, single-digit) would produce silently wrong results or crash on int("") if num had fewer than 2 digits.
minimum-sum-of-four-digit-number-after-splitting-digits/solution.py — Closely related problem (restricted to exactly 4 digits); compare whether it uses the same greedy approach or a specialized onesplit-with-minimum-sum/solution.py:minsumoftwonumbers — Test whether the round-robin strategy remains optimal for the 3-way split variant (LeetCode 2160)split-with-minimum-sum/test_solution.py — Check how the external test file imports and exercises this solution, versus the inline testsgreedy-digit-distribution — Explore the proof that sort-and-deal minimizes the sum: it follows from the fact that minimizing total place-value contribution requires smallest digits at highest significance, distributed evenlysplit-min-sum-round-robin-optimal — Round-robin dealing of ascending-sorted digits into two accumulators produces the minimum possible sum for any digit countsplit-min-sum-leading-zeros-safe — Python's int() conversion silently drops leading zeros in the string accumulators, so inputs containing 0 digits (e.g., 2030) produce correct results without special-casingsplit-min-sum-no-input-validation — The function performs no input validation; single-digit inputs would cause int("") on the empty accumulatorsplit-min-sum-string-accumulator-pattern — Digits are concatenated as strings and converted to int at the end, rather than computed via arithmetic place-value multiplication