↓Skip to main content
  1. LeetCode/

LeetCode 3536: Maximum Product of Two Digits

·2 mins· ·
LeetCode Easy Math Sorting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-05-04

  • Submissions: Runtime Error ×4 → Wrong Answer ×2 → Accepted

Approach
#

The answer is the product of the two largest digits (a digit appearing twice may be used twice). Sorting the digits and taking the last two is simplest; in one scan, keep max1 >= max2:

  • A digit > max1: the old max1 becomes max2 and the new digit becomes max1.
  • Otherwise, if > max2: update max2.

My Attempts
#

VersionProblem
Four REsmap(n, str) had its arguments reversed; then type errors from comparing strings with the integer 0 and multiplying strings
Two WAsTypes fixed, but on a new maximum the old maximum was not moved to second place. For n = 789 the answer is 9 × 8 = 72; both returned 0. The first used >=, so the second 7 only overwrote first place, and n = 707 returned 0 instead of 49
ACChanged to max1, max2 = s, max1

Solution
#

class Solution:
    def maxProduct(self, n: int) -> int:
        digits = sorted(int(c) for c in str(n))
        return digits[-1] * digits[-2]

Complexity
#

Time O(d log d) for d digits (at most 10), space O(d).

Mistakes to Avoid
#

  • Comparing strings with integers.
  • When tracking a top two, a new first place pushes the old first into second.

Interview Explanation
#

The answer multiplies the two largest digits. I sort the digits and take the last two; in a single scan I would keep first and second place, demoting the old first whenever a new first appears. Time O(d).

Related

LeetCode 1030: Matrix Cells in Distance Order
·2 mins
LeetCode Easy Array Math Geometry Sorting
LeetCode 1030 note: list every coordinate and sort by Manhattan distance to the center; return coordinates, not distances
LeetCode 628: Maximum Product of Three Numbers
·1 min
LeetCode Easy Array Math Sorting
LeetCode 628 note: after sorting there are only two candidates: the three largest, or the two smallest (negatives) times the largest
LeetCode 3492: Maximum Containers on a Ship
·1 min
LeetCode Easy Math
LeetCode 3492 note: at most n² cells and at most maxWeight // w by weight, so take the smaller, using integer division
LeetCode 3074: Apple Redistribution into Boxes
·1 min
LeetCode Easy Array Greedy Sorting
LeetCode 3074 note: apples can be split freely, so take boxes from the largest capacity until the total fits every apple
LeetCode 2099: Find Subsequence of Length K With the Largest Sum
·1 min
LeetCode Easy Array Hash-Table Sorting Heap-Priority-Queue
LeetCode 2099 note: pick the k largest values along with their indices, then sort by index to restore the original order
LeetCode 2231: Largest Number After Digit Swaps by Parity
·2 mins
LeetCode Easy Sorting Heap-Priority-Queue
LeetCode 2231 note: odd digits swap only with odd digits and even with even; sort each group and refill each position with the largest digit of its parity