Solution in O(1) only 4 Lines Code


  • 1
    D

    Hello Guys!

    I know after watching code definitely you will say you nailed it. That's why this problem is in Easy.

    In problem it's clearly says that there are only 8 possibility of the input, so i find answer for every input and store it into the array and return answer directly by using input as index.

    For example if n=2 and answer = 987, so i store 987 in array at 2nd position.

    So whole array will be look like {0,9,987,123,597,677,1218,877,475}.

    Here's the code.

    int[] ans = {0,9,987,123,597,677,1218,877,475};
            if(n>=1 && n<=8)
                return ans[n];
            return 0;
    

  • 0
    L

    @dharmikjogi You aren't serious..are you? lol


  • 0
    D

    @labs Yes i am serious. I know nobody will believe because LeetCode has such great test cases for every problem set. I submitted this code and it works. You can check also.


  • 0
    L

    @dharmikjogi Lol..I didn't mean that. I know the code will work if you have valid answers. I was wondering if you were implying or not if it was just a hack.


  • -4
    L

    Something is wrong with the runtime system here. This constant look-up solution only beats 45.36% of submissions. What can possibly be faster than this??!!


  • 0
    H

    @lijenpan I think you will get different runtimes every time you submit your solution. And the range could be very large. And I agree, this should be the fastest solution. LOL...


  • 0

    @lijenpan In what language did you submit it, and what was the time in ms? And what makes you think there's something faster?


  • 0
    S

    @dharmikjogi said in Solution in O(1) only 4 Lines Code:

    For example if n=2 and answer = 987, so i store 987 in array at 2nd position.

    In interview we don't have luxury of giving output for every test input in a list or array


  • 0
    D

    @sha256pki
    Yes you are right that in interview we don't have all type of test cases and expected output.

    But in interview, at least we will check for different inputs and when you are told explicitly that you will have input between range A and B which is so small then you should check for all.

    And i know that interviewer will not impress with this solution but you should tell him that,
    "I know that, this solution is not what you expected, but this skills will work when when we are with the client or giving demo to the client, and some unexpected error occurred then, at that time we don't have sufficient time to solve that error and can't tell to client that some error occurred, so at that time it's better to fix that error anyhow and handle client and then after that we can fix properly so that this type of error will never occurs again."

    This will make an impression on interviewer.

    I hope you will understand my point.


  • 0
    T
    int largestPalindrome(int n) {
        return ((int []){0,9,987,123,597,677,1218,877,475})[n];
    }
    

Log in to reply
 

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