Advertisement

Largest Number - LeetCode 179 Solution

Largest Number - Complete Solution Guide

Largest Number is LeetCode problem 179, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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, so you need to return a string instead of an integer. Example 1: Input: nums = [10,2] Output: "210" Example 2: Input: nums = [3,30,34,5,9] Output: "9534330" Constraints: 1 <= nums.length <= 100 0 <= nums[i] <= 10 9

Detailed Explanation

The problem asks us to take a list of non-negative integers and arrange them in such a way that they form the largest possible number. The result needs to be returned as a string because the resulting number might be very large and exceed the maximum value representable by integer data types. For example, given `[10, 2]`, we should return "210" because 210 > 102. Given `[3, 30, 34, 5, 9]`, we should return "9534330".

Solution Approach

The solution involves converting the integer array to an array of strings. Then, it sorts the array of strings using a custom comparison function. The comparison function determines the order based on whether concatenating two strings in one order results in a larger number than concatenating them in the reverse order. Finally, the sorted strings are joined together to form the largest possible number. A final check ensures that if the resulting string starts with '0', then the string represents 0, which is then returned.

Step-by-Step Algorithm

  1. Step 1: Convert the array of integers `nums` into an array of strings `numsStr`.
  2. Step 2: Sort the `numsStr` array using a custom comparison function. This function compares two strings `x` and `y` by checking if `x + y` is greater than `y + x`. This custom sorting orders the numbers so they produce the largest possible number when concatenated.
  3. Step 3: Concatenate the sorted strings in `numsStr` into a single string `largestNumStr`.
  4. Step 4: Check if the resulting `largestNumStr` starts with '0'. If it does, it means all the input numbers were essentially zeros (e.g., [0,0,0]). In this case, return '0'. Otherwise, return the `largestNumStr`.

Key Insights

  • Insight 1: A standard sorting algorithm won't work directly because we need to compare numbers based on how they would appear when concatenated. For example, 30 should come after 3 because '330' is larger than '303'.
  • Insight 2: The core idea is to treat the numbers as strings and define a custom comparison function. This function compares two strings `x` and `y` by concatenating them in both orders (`x+y` and `y+x`) and comparing the resulting strings. The string that results in a larger concatenation should come first.
  • Insight 3: Handle the edge case where all numbers are zero. In this case, the output should be '0', not a string of multiple zeros.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Array, String, Greedy, Sorting.

Companies

Asked at: Accenture, Goldman Sachs, Graviton, Huawei, Myntra, Nykaa, Oracle, Paytm, Salesforce, ServiceNow, Siemens, Visa, Works Applications, Zalando, Zoho, josh technology, tcs.