> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Largest Number Python Solution with Tests

> Tested Python solution for LeetCode 179 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 179, [Medium](https://leetcode-py.wisl.dev/catalog/medium.md). Topics: [Array](https://leetcode-py.wisl.dev/catalog/topics/array), [String](https://leetcode-py.wisl.dev/catalog/topics/string), [Greedy](https://leetcode-py.wisl.dev/catalog/topics/greedy), [Sorting](https://leetcode-py.wisl.dev/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/largest-number/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](https://leetcode-py.wisl.dev/practice/testing.md), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 179   # by problem number
lcpy gen -s largest_number   # by problem name
```

## Problem

Given a list of non-negative integers `nums`, arrange them such that they form the largest number and return it.

Since the result may be very large, you need to return a string instead of an integer.

### Examples

```
Input: nums = [10,2]
Output: "210"
```

```
Input: nums = [3,30,34,5,9]
Output: "9534330"
```

### Constraints

* 1 \<= nums.length \<= 100
* 0 \<= nums\[i] \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/largest_number/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/largest_number/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from functools import cmp_to_key


class Solution:
    # Time: O(k * n log n) — comparator does string concat of length k
    # Space: O(n * k) for the string keys
    def largest_number(self, nums: list[int]) -> str:
        strs = [str(n) for n in nums]

        def compare(a: str, b: str) -> int:
            if a + b > b + a:
                return -1
            if a + b < b + a:
                return 1
            return 0

        strs.sort(key=cmp_to_key(compare))
        result = "".join(strs)
        # Leading zero means every value was zero
        return "0" if result[0] == "0" else result
```

## Complexity

| Time | Space |
| - | - |
| O(k \* n log n) — comparator does string concat of length k | O(n \* k) for the string keys |

## Tags

[Grind](https://leetcode-py.wisl.dev/catalog/grind.md), [NeetCode All](https://leetcode-py.wisl.dev/catalog/neetcode.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.