MySql, 192 ms
First of all, this problem is why we can\'t have nice things. This is all about dealing with f----d up edge cases that are not adequately described in the probl
First of all, this problem is why we can\'t have nice things. This is all about dealing with f----d up edge cases that are not adequately described in the problem statement. For instance, when the description says "if there is no nth salary" what they mean is "if there is no nth unique salary". \n\nSecond, this problem is all about the edge cases and the improper wording on the problem. Dealing with this nonsense takes up all the time complexity AND developer time. So really, dealing with the edge case IS ACTUALLY THE PROBLEM. \n\nThe rest is just a side-show, whether you\'re taking a limit of a limit, or using an offset, or a subquery of some kind, or a window function if you\'re not on MySql (memo to leetcode: MySql supports window functions now).\n\nAnyway, the lion\'s share of the problem is just understanding that the author meant to say that salaries have to be unique and if N is greater than the number of unique salaries or otherwise invalid, then return NULL. \n\nFor extra points, also figure out how to take the Nth unique result. Normally you can just use a common table expression and/or window function - `select salary, rank() over (order by salary desc) ...` - but if you\'re still using a MySql version that\'s older than Brittany Spears, here\'s one way you can do it:\n\n```\nCREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT\nBEGIN\n    DECLARE results INT DEFAULT 0;\n    SET results = (select count(distinct(salary)) from Employee);\n    IF results = 0 OR N > results THEN \n       return NULL;\n    ELSE\n      SET N = N -1;\n      RETURN (\n          select distinct salary \n            from Employee\n            order by salary desc\n            limit 1 offset N\n      );\n    END IF; \n```
[]