动态规划 12

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];
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容

  • 背包问题 描述在n个物品中挑选若干物品装入背包,最多能装多满?假设背包的大小为m,每个物品的大小为A[i]动态规划...
    杰米阅读 376评论 0 0
  • to-do:看一下别人写的题解 https://github.com/981377660LMT/algorithm...
    winter_sweetie阅读 765评论 1 0
  • https://leetcode-cn.com/tag/dynamic-programming/ 题目汇总264....
    今天柚稚了么阅读 203评论 0 0
  • leetcode 链接[https://leetcode-cn.com/problemset/lcof/] 3、数...
    漫彻思特阅读 329评论 0 0
  •   为了准备三月份蓝桥杯的比赛和提高下自己数据结构和算法方面的水平,所以开始在leetcode上刷起了题。刚刚开始...
    冯宇祺阅读 3,605评论 0 5