Why my accepted solution on visual studio can not pass leetcode? Not time limited error


  • 0
    I
    class Solution {
    public:
    	int nthUglyNumber(int n){
    		vector<long> dp(n, 0);
    		dp[0] = 1;
    		dp[1] = 2; 
    		dp[2] = 3;
    		int pos = 2;
    		long min_max = dp[pos - 2] * 5;
    		for (int i = 3; i < n; ++i){
    			dp[i]= LONG_MAX;
    			for (int j = 0; j < 3; i++){
    				if (dp[pos - j] * 2 > dp[i - 1])
    					dp[i] = min(dp[i], dp[pos - j] * 2);
    			}
    			for (int j = 0; j < 3; i++){
    				if (dp[pos - j] * 3 > dp[i - 1])
    					dp[i] = min(dp[i], dp[pos - j] * 3);
    			}
    			for (int j = 0; j < 3; i++){
    				if (dp[pos - j] * 5 > dp[i - 1])
    					dp[i] = min(dp[i], dp[pos - j] * 5);
    			}
    			if (min_max <= dp[i]){
    				pos++;
    				min_max = dp[pos - 2] * 5;
    			}
    		}
    		return dp[n - 1];
    	}
    };

Log in to reply
 

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