📉
leetcode-题解
  • leetcode-notes
  • linked-list
    • 2-Add-Two-Numbers
    • 109-Convert-Sorted-List-to-Binary-Search-Tree
    • 19-Remove-Nth-Node-From-End-of-List
    • 92-Reverse-Linked-List-II
    • 142-Linked-List-Cycle-II
    • 83-Remove-Duplicates-from-Sorted-List
    • 61-Rotate-List
    • 148-Sort-List
    • 86-Partition-List
    • 82-Remove-Duplicates-from-Sorted-List-II
    • 138-Copy-List-with-Random-Pointer
    • 328-Odd-Even-Linked-List
    • 23- Merge-k-Sorted-Lists
    • 25-Reverse-Nodes-in-k-Group
  • templates
    • bitmap
    • ologn
    • Morris
    • dp
    • binary-search
    • Maxwindow
    • 递归
    • union
    • graph
    • greedy-algorithm
    • kmp
    • list
    • ordered-list
    • tree
    • Manacher
    • Monotonic-stack
    • big-data
    • sort-Summary
    • Bucket-sort
    • bit-opreation
    • heap-sort
  • arrays
    • others
      • 31-Next-Permutation
      • 66-Plus- One
      • 229-Majority-Element-II
      • 414-Third-Maximum-Number
    • matrix
      • 74-Search-a-2D-Matrix
      • 289-Game-of-Life
    • PrefixOrSuffix
      • 560-Subarray-Sum-Equals-K
      • 238-Product-of-Array-Except-Self
    • 二分法
      • rotated-array-problem
      • D天内送达包裹的能力
      • 162-Find-Peak-Element
      • Minimize-maximum-and-maximize-minimum
    • 多指针
      • 611-Valid-Triangle-Number
      • 228-Summary-Ranges
      • 75-Sort-Colors
      • 18-4Sum
      • 27-Remove-Element
      • 三数之和
      • 26-Remove-Duplicates-from-Sorted-Array
      • 盛最多水的容器
      • 80-Remove-Duplicates-from-Sorted-Array-II
      • 最接近的三数之和
    • array-circle
      • 457-Circular-Array-Loop
      • 287-Find-the-Duplicate-Number
      • 565-Array-Nesting
    • 智力题
      • 73-Set-Matrix-Zeroes
      • 最佳观光组合
    • 几何问题
      • 统计全为1的正方形子矩阵
      • 495-Teemo-Attacking
    • sort
      • 88-Merge-Sorted-Array
      • 57-Insert-Interval
  • tree
    • 105-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal
    • 230-Kth-Smallest-Element in-a-BST
    • 106-Construct-Binary-Tree-from-Inorder-and-Postorder-Traversal
    • 257-Binary-Tree-Paths
    • 113-Path-Sum-II
    • 96-Unique-Binary-Search-Trees
    • 124-Binary-Tree-Maximum-Path-Sum
    • 103-Binary-Tree-Zigzag-Level-Order-Traversal
    • 426-Convert-Binary-Search-Tree-to-Sorted-Doubly-Linked-List
    • 117-Populating-Next-Right-Pointers-in-Each-Node-II
    • 99-Recover-Binary-Search-Tree
    • 366-Find-Leaves-of-Binary-Tree
    • 337-House-Robber-III
    • 333-Largest-BST-Subtree
    • 298-Binary-Tree-Longest-Consecutive-Sequence
    • 428-Serialize-and-Deserialize-N-ary-Tree
    • 1367-Linked-List-in-Binary-Tree
    • 173-Binary-Search-Tree-Iterator
    • 98-Validate-Binary-Search-Tree
    • 156-Binary-Tree-Upside-Down
    • 404-Sum-of-Lef- Leaves
    • 255-Verify-Preorder-Sequence-in-Binary-Search-Tree
    • 272-Closest-Binary-Search-Tree-Value-II
    • 95-Unique-Binary-Search-Trees-II
    • 222-Count-Complete-Tree-Nodes
    • 431-Encode-N-ary-Tree to-Binary-Tree
    • Lowest-Common-Ancestor-of-a-Binary-Tree
    • 129-Sum-Root-to-Leaf-Numbers
  • recursive
    • 前言
    • 39-Combination-Sum
    • 79-Word-Search
    • 04-Power-Set-LCCI
    • 前言
    • 90-Subsets-II
    • 40-Combination-Sum-II
    • 351-Android-Unlock-Patterns
  • dynamic-programming
    • 276-Paint-Fence
    • 132-Palindrome-Partitioning-II
    • 361-Bomb-Enemy
    • 62-Unique-Paths
    • 376-Wiggle-Subsequence
    • 403-Frog-Jump
    • 32-Longest-Valid-Parentheses
    • 97-Interleaving-String
    • 354-Russian-Doll-Envelopes
    • 279-Perfect-Squares
    • 304-Range-Sum-Query-2D-Immutable
    • 10-Regular-Expression-Matching
    • Paint-House-series
    • 139-Word-Break
    • Best-Time-to-Buy-and-Sell-Stock-series
    • 416-Partition-Equal-Subset-Sum
    • 300-Longest-Increasing-Subsequence
    • 91-Decode-Ways
    • Ugly-Number-series
    • 363-Max-Sum-of-Rectangle-No-Larger-Than-K
    • 368-Largest-Divisible-Subset
    • 63-Unique-Paths-II
    • 312-Burst-Balloons
    • 322-Coin-Change
    • 64-Minimum-Path-Sum
    • 140-Word-Break-II
    • 120-Triangle
    • 72-Edit-Distance
    • House-Robber-series
    • 413-Arithmetic-Slices
    • 174-Dungeon-Game
    • 87-Scramble-String
    • 44-Wildcard-Matching
    • 338-Counting-Bits
    • 152-Maximum-Product-Subarray
    • 375-Guess-Number-Higher-or-Lower-II
  • hash-table
    • 381-Insert-Delete-GetRandom-O(1) - Duplicates-allowed
    • 442-Find-All-Duplicates-in-an-Array
    • 380-Insert-Delete-GetRandom-O(1)
    • 1-Two-Sum
    • 3-Longest-Substring-Without-Repeating-Characters
    • 41-First-Missing-Positive
  • stack
    • Monotonic stack
      • 84-Larges-Rectangle-in-Histogram
      • 42-Trapping-Rain-Water
  • bit-manipulation
    • 08-Draw-Line-LCCI
  • Mysql
    • 185-Department-Top-Three-Salaries
    • 177-N-Highest-Salary
    • 178-Rank-Scores
    • 180-Consecutive-Numbers
  • greedy
    • 56-Merge-Intervals
    • 55-Jump-Game
    • 53-Maximum-Subarray
  • math
    • 357-Count-Numbers-with-Unique-Digits
    • 343-Integer-Break
    • 119-Pascal's-Triangle-II
  • string
    • Palindrome
      • 5-Longest-Palindromic-Substring
      • Manacher
  • sliding-window
    • 209-Minimum-Size-Subarray-Sum
Powered by GitBook
On this page
  • 前言
  • 丑数-I
  • 0x0 题目详情
  • 0x1 解题思路
  • 0x2 代码实现
  • 0x3 课后总结
  • 丑数-II
  • 0x0 题目详情
  • 0x1 解题思路
  • 0x2 代码实现
  • 0x3 课后总结

Was this helpful?

  1. dynamic-programming

Ugly-Number-series

Previous91-Decode-WaysNext363-Max-Sum-of-Rectangle-No-Larger-Than-K

Last updated 4 years ago

Was this helpful?

前言

本章分为两个部分:

丑数-I

0x0 题目详情

测试用例:

0x1 解题思路

0x2 代码实现

0x3 课后总结

丑数-II

0x0 题目详情

编写一个程序,找出第 n 个丑数。 丑数就是质因数只包含 2, 3, 5 的正整数。

测试用例:

示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。

说明: 1 是丑数。 n 不超过1690。

0x1 解题思路

这道题有两个思路,分别为动态规划和使用小根堆。

小根堆:

我们可以从第一个丑数开始,将最小的丑数分别乘以2、3或者5。这样新求出3个丑数入堆。然后再从堆中选择一个最小继续重复乘以2、3、5的操作。直到重读这个操作n次。

使用小根堆的原因是因为它能够帮助对求出的丑数进行排序,从而总是能够选出最小的丑数。但是新求的丑数有可能重复。例如丑数6,可以通过2X3得到,也可以通过3X2得到。这都是有可能发生的,所以从堆中弹出最小的丑数x后,要看看新的栈顶是否与x相同,如果相同,则需要将新的丑数弹出直至二者不等。

动态规划

小根堆是将丑数插入到结果集后再排序,而动态规划的方法时在在求出三个新丑数后,我们自己排序找到最小的丑数后插入结果集中。

我们可以声明一个一维dp数组,dp[i]表示第i个丑数。并且我们需要维护三个指针,p2、p3、p5。分别表示指向需要乘2的丑数,需要乘3的丑数,需要乘5的丑数。而指针的移动规则就有点巧妙了:如果新求出的丑数是通过p2指针指向的丑数求出来的,那么就将p2++,如果新求出的丑数是通过p3指针指向的丑数求出的,那么就将p3++。p5移动的规则类似。这是为什么呢?这里举个例子:

刚开始,我们只有一个丑数,p2=0,p3=0,p5=0,都指向1这丑数。那么新求的最小丑数dp[1]=Math.min(dp[p2]*2,dp[p3]*3,dp[p5]*5)=2。那么我们就将p2++。因为这表示1这个丑数再也不参与求新丑数的操作了,就将p2++。然后可以参与求新丑数的丑数包括1、2。那么我们就需要对这两个数分别乘以2、3、5从而才能决定最小的丑数到底是谁。

但是,2*3>1*3的,2*5>1*5。所以对于2这个丑数,它根本不用乘以3或者5,因为这些工作都由前面的丑数做了。

0x2 代码实现

class Solution {
    public int nthUglyNumber(int n) {
        if(n<0){
            return 0;
        }
        int[] dp=new int[n];
        dp[0]=1;
        int p2=0;
        int p3=0;
        int p5=0;
        for(int i=1;i<n;i++){
            dp[i]=Math.min(dp[p2]*2,Math.min(dp[p3]*3,dp[p5]*5));
            //这三个if需要并且的原因是如果当前的丑数序列是1、2、3、4、5,p2指向3,p3指向2,p5指向2
            //如果三个if不是并列的,下一个丑数=min(dp[p2]*2=3*2=6,dp[p3]*3=6,dp[p5]*5=10)=6
            //注意如果不是并列的,那么只会移动p2,这是p3仍然指向2
            //下一轮,dp[p2]*2=4*2=8,dp[p3]*3=2*3,dp[p5]*5=2*5=10,这里又求出来一个6,重复了,而且竟然还会被认为是有效的添加至
            //dp数组中
            if(dp[i]==dp[p2]*2){
                p2++;
            }
            //不是并且的就是指这里采用else if
            if(dp[i]==dp[p3]*3){
                p3++;
            }
            //不是并且的就是指这里采用else
            if(dp[i]==dp[p5]*5){
                p5++;
            }
        }
        return dp[n-1];
    }
}

0x3 课后总结

这动态规划的做法,这...没做过谁想得到啊!

如何判断一个数是不是丑数
如何求第n个丑数