1 //动态方程dp[i]=min(dp[p_2]*2,dp[p_3]*3,dp[p_5]*5) 2 class Solution 3 { 4 public: 5 int nthUglyNumber(int n) 6 { 7 vector<int> dp(n); 8 dp[0] = 1; 9 int p2,p3,p5; 10 p2 = p3 = p5 = 0; 11 for (int i = 1;i < n;++ i) 12 { 13 dp[i] = min(2 * dp[p2],min(3 * dp[p3],5 * dp[p5])); 14 if(dp[i] == 2 * dp[p2]) ++p2; 15 if(dp[i] == 3 * dp[p3]) ++p3; 16 if(dp[i] == 5 * dp[p5]) ++p5; 17 } 18 return dp.at(n-1); 19 } 20 };
原文地址:https://www.cnblogs.com/yuhong1103/p/12700284.html
时间: 2024-11-08 09:52:51