美文网首页
动态规划 12

动态规划 12

作者: 眼若繁星丶 | 来源:发表于2021-04-11 16:41 被阅读0次
    LeetCode 264

    原题:https://leetcode-cn.com/problems/ugly-number-ii/

    动态规划

    参考官方题解:

    |——https://leetcode-cn.com/problems/ugly-number-ii/solution/chou-shu-ii-by-leetcode-solution-uoqd/

    |——https://leetcode-cn.com/problems/ugly-number-ii/solution/san-zhi-zhen-fang-fa-de-li-jie-fang-shi-by-zzxn/

    public class Solution {
        public int nthUglyNumber(int n) {
            int[] dp = new int[n + 1];
            dp[1] = 1;
            int p2 = 1, p3 = 1, p5 = 1;
            for (int i = 2; i <= n; i++) {
                int num2 = dp[p2] * 2, num3 = dp[p3] * 3, num5 = dp[p5] * 5;
                dp[i] = Math.min(Math.min(num2, num3), num5);
                if (dp[i] == num2) p2++;
                if (dp[i] == num3) p3++;
                if (dp[i] == num5) p5++;
            }
            return dp[n];
        }
    }
    

    相关文章

      网友评论

          本文标题:动态规划 12

          本文链接:https://www.haomeiwen.com/subject/jynakltx.html