Skip to content

3536. Maximum Product of Two Digits 👍

  • Time: $O(n)$
  • Space: $O(n)$
1
2
3
4
5
6
7
8
9
class Solution {
 public:
  int maxProduct(int n) {
    string s = to_string(n);
    ranges::sort(s);
    const int sz = s.length();
    return (s[sz - 1] - '0') * (s[sz - 2] - '0');
  }
};
1
2
3
4
5
6
7
8
class Solution {
  public int maxProduct(int n) {
    char[] s = String.valueOf(n).toCharArray();
    Arrays.sort(s);
    final int sz = s.length;
    return (s[sz - 1] - '0') * (s[sz - 2] - '0');
  }
}
1
2
3
4
class Solution:
  def maxProduct(self, n: int) -> int:
    s = sorted(str(n))
    return int(s[-1]) * int(s[-2])