Skip to content

Latest commit

 

History

History
29 lines (24 loc) · 1.66 KB

File metadata and controls

29 lines (24 loc) · 1.66 KB

3536. Maximum Product of Two Digits (Easy)

Date and Time: May 3, 2025

Link: https://leetcode.com/problems/maximum-product-of-two-digits


Python:

class Solution:
    def maxProduct(self, n: int) -> int:
        # Q: Return the max product of any two digits
        # S: Find the biggest and the second biggest digit
        # TC: O(n), n=len(n), SC: O(1)
        first, second = 0, 0
        while n:
            if n % 10 > first:
                first, second = n % 10 , first
            elif n % 10 > second:
                second = n % 10
            n //= 10
        return first * second

Time Complexity: $O(n)$
Space Complexity: $O(1)$


CC BY-NC-SABY: credit must be given to the creatorNC: Only noncommercial uses of the work are permittedSA: Adaptations must be shared under the same terms