Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 <= 1000 <= nums[i] <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[10,2]
[3,30,34,5,9]
smallest-value-of-the-rearranged-number)find-the-key-of-the-numbers)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #179: Largest Number
class Solution {
public String largestNumber(int[] nums) {
List<String> vs = new ArrayList<>();
for (int v : nums) {
vs.add(v + "");
}
vs.sort((a, b) -> (b + a).compareTo(a + b));
if ("0".equals(vs.get(0))) {
return "0";
}
return String.join("", vs);
}
}
// Accepted solution for LeetCode #179: Largest Number
func largestNumber(nums []int) string {
vs := make([]string, len(nums))
for i, v := range nums {
vs[i] = strconv.Itoa(v)
}
sort.Slice(vs, func(i, j int) bool {
return vs[i]+vs[j] > vs[j]+vs[i]
})
if vs[0] == "0" {
return "0"
}
return strings.Join(vs, "")
}
# Accepted solution for LeetCode #179: Largest Number
class Solution:
def largestNumber(self, nums: List[int]) -> str:
nums = [str(v) for v in nums]
nums.sort(key=cmp_to_key(lambda a, b: 1 if a + b < b + a else -1))
return "0" if nums[0] == "0" else "".join(nums)
// Accepted solution for LeetCode #179: Largest Number
impl Solution {
pub fn largest_number(nums: Vec<i32>) -> String {
let mut v: Vec<String> = nums.iter().map(|&num| num.to_string()).collect();
v.sort_by(|a: &String, b: &String| (b.clone() + a).cmp(&(a.clone() + b)));
if v[0] == "0" {
String::from("0")
} else {
v.join("")
}
}
}
// Accepted solution for LeetCode #179: Largest Number
function largestNumber(nums: number[]): string {
nums.sort((a, b) => {
const [ab, ba] = [String(a) + String(b), String(b) + String(a)];
return +ba - +ab;
});
return nums[0] ? nums.join('') : '0';
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.