<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)313:超級丑數(shù)

          共 2550字,需瀏覽 6分鐘

           ·

          2021-07-09 02:45

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 超級丑數(shù),我們先來看題面:
          https://leetcode-cn.com/problems/super-ugly-number/

          A super ugly number is a positive integer whose prime factors are in the array primes. Given an integer n and an array of integers primes, return the nth super ugly number. The nth super ugly number is guaranteed to fit in a 32-bit signed integer.


          編寫一段程序來查找第 n 個超級丑數(shù)。
          超級丑數(shù)是指其所有質(zhì)因數(shù)都是長度為 k 的質(zhì)數(shù)列表 primes 中的正整數(shù)。


          示例


          輸入: n = 12, primes = [2,7,13,19]
          輸出: 32
          解釋: 給定長度為 4 的質(zhì)數(shù)列表 primes = [2,7,13,19],前 12 個超級丑數(shù)序列為:[1,2,4,7,8,13,14,16,19,26,28,32] 。


          解題



          class Solution {
          public:
              int nthSuperUglyNumber(int n, vector<int>& primes) {
                int i, j, k = primes.size();
                vector<long> idx(k,1);
                  vector<long long> dp(n+1, LONG_LONG_MAX);
                  dp[1] = 1;
                for(i = 2; i <= n; ++i)
                {
                  for(j = 0; j < k; ++j)
                          if(dp[idx[j]]*primes[j] < dp[i])
                              dp[i] = dp[idx[j]]*primes[j];
                  for(j = 0; j < k; ++j)
                    if(dp[i] == dp[idx[j]]*primes[j])
                      idx[j]++;
              }
              return dp[n];
              }
          };


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點(diǎn)個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-280題匯總,希望對你有點(diǎn)幫助!

          LeetCode刷題實(shí)戰(zhàn)301:刪除無效的括號

          LeetCode刷題實(shí)戰(zhàn)302:包含全部黑色像素的最小矩陣
          LeetCode刷題實(shí)戰(zhàn)303:區(qū)域和檢索 - 數(shù)組不可變
          LeetCode刷題實(shí)戰(zhàn)304:二維區(qū)域和檢索 - 矩陣不可變
          LeetCode刷題實(shí)戰(zhàn)305:島嶼數(shù)量II
          LeetCode刷題實(shí)戰(zhàn)306:累加數(shù)
          LeetCode刷題實(shí)戰(zhàn)307:區(qū)域和檢索 - 數(shù)組可修改
          LeetCode刷題實(shí)戰(zhàn)308:二維區(qū)域和檢索 - 可變
          LeetCode刷題實(shí)戰(zhàn)309:最佳買賣股票時機(jī)含冷凍期
          LeetCode刷題實(shí)戰(zhàn)310:最小高度樹
          LeetCode刷題實(shí)戰(zhàn)311:稀疏矩陣的乘法
          LeetCode刷題實(shí)戰(zhàn)312:戳氣球

          瀏覽 45
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产精品粉嫩在线播放 | 欧美乱伦xxx | 免费日韩在线三级黄色电影网址 | 殴美成人性爱大片免费看 | 无码视频免费在线播放 |