Shortest O(n) Java DP solution
public int nthUglyNumber(int n) {\n            if(n==1) return 1;\n            int[] dp = new int[n+1]; // dp[i] holds the ith's ugly number\n            dp[1] 
    public int nthUglyNumber(int n) {\n            if(n==1) return 1;\n            int[] dp = new int[n+1]; // dp[i] holds the ith's ugly number\n            dp[1] = 1;\n            int p2=1, p3=1, p5=1;\n            for(int i=2; i<=n; i++){ // loop invariant:dp[i] holds the smallest ith uglynumber\n                dp[i] = Math.min(2*dp[p2], Math.min(3*dp[p3],5*dp[p5])); // the next ugly number must be built from a smaller ugly number\n                if(dp[i]==2*dp[p2])p2++; \n                if(dp[i]==3*dp[p3])p3++;\n                if(dp[i]==5*dp[p5])p5++;\n            }\n            return dp[n];\n        }
[]