Solution (for fun) that beats 100%


  • 0

    The observation is that for the product to be representable by long, n cannot be very large. So we can pre-compute and memoize.

    Here's my solution:

    public class Solution {
        public int LargestPalindrome(int n) {
            if (n == 1) return 9;
            if (n == 2) return 987;
            if (n == 3) return 123;
            if (n == 4) return 597;
            if (n == 5) return 677;
            if (n == 6) return 1218;
            if (n == 7) return 877;
            if (n == 8) return 475;
            
            throw new NotSupportedException();
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.