Link: https://leetcode.com/problems/largest-number/
Solution:
Topics: compare, cmp_to_key
Intuition
Annoying but somewhat interesting problem! Basically it comes down to writing a custom compare function. If int(a + b) > int(b + a)
, then a
should come first; otherwise b
. It’s that simple. Just sort and join.
Note that in python we have to provide a multi-argument compare function as so: key=cmp_to_key(my_compare_function)
. Return -1 if a
comes first, otherwise 1.
Implementation