<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)368:最大整除子集數(shù)

          共 4443字,需瀏覽 9分鐘

           ·

          2021-09-02 17:00

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

          今天和大家聊的問題叫做 最大整除子集,我們先來看題面:
          https://leetcode-cn.com/problems/largest-divisible-subset/

          Given a set of distinct positive integers nums, return the largest subset answer such that every pair (answer[i], answer[j]) of elements in this subset satisfies:


          answer[i] % answer[j] == 0, or

          answer[j] % answer[i] == 0


          If there are multiple solutions, return any of them.

          給你一個(gè)由 無重復(fù) 正整數(shù)組成的集合 nums ,請你找出并返回其中最大的整除子集 answer ,子集中每一元素對 (answer[i], answer[j]) 都應(yīng)當(dāng)滿足:
          answer[i] % answer[j] == 0 ,或
          answer[j] % answer[i] == 0
          如果存在多個(gè)有效解子集,返回其中任何一個(gè)均可。

          示例


          示例 1:

          輸入:nums = [1,2,3]
          輸出:[1,2]
          解釋:[1,3] 也會(huì)被視為正確答案。

          示例 2:

          輸入:nums = [1,2,4,8]
          輸出:[1,2,4,8]


          解題


          動(dòng)態(tài)規(guī)劃的思路,要求出最大整除的子集,首先拆分問題,從一個(gè)最小的子集再擴(kuò)大為最大的子集,假設(shè)有一個(gè)最小整除子集[a,b](a<b)則滿足是b倍數(shù)的整數(shù)同樣滿足整除條件,此時(shí)子集被擴(kuò)大為[a,b,c](c為b的倍數(shù)),以此類推,由此得出的dp數(shù)組為1,2,3
          例如 [2,4,7,8,9,12,16,18] 在該題的dp數(shù)組為[1, 2, 1, 3,1, 3, 4, 2]


          class Solution {
              public List<Integer> largestDivisibleSubset(int[] nums) {
                  int len = nums.length;
                  Arrays.sort(nums);

                  // 第 1 步:動(dòng)態(tài)規(guī)劃找出最大子集的個(gè)數(shù)、最大子集中的最大整數(shù)
                  int[] dp = new int[len];
                  Arrays.fill(dp, 1);
                  int maxSize = 1;
                  int maxVal = dp[0];
                  for (int i = 1; i < len; i++) {
                      for (int j = 0; j < i; j++) {
                          // 題目中說「沒有重復(fù)元素」很重要
                          if (nums[i] % nums[j] == 0) {
                              dp[i] = Math.max(dp[i], dp[j] + 1);
                          }
                      }

                      if (dp[i] > maxSize) {
                          maxSize = dp[i];
                          maxVal = nums[i];
                      }
                  }

                  // 第 2 步:倒推獲得最大子集
                  List<Integer> res = new ArrayList<Integer>();
                  if (maxSize == 1) {
                      res.add(nums[0]);
                      return res;
                  }
                  
                  for (int i = len - 1; i >= 0 && maxSize > 0; i--) {
                      if (dp[i] == maxSize && maxVal % nums[i] == 0) {
                          res.add(nums[i]);
                          maxVal = nums[i];
                          maxSize--;
                      }
                  }
                  return res;
              }
          }


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

          上期推文:

          LeetCode1-360題匯總,希望對你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)361:轟炸敵人
          LeetCode刷題實(shí)戰(zhàn)362:敲擊計(jì)數(shù)器
          LeetCode刷題實(shí)戰(zhàn)363:矩形區(qū)域不超過 K 的最大數(shù)值和
          LeetCode刷題實(shí)戰(zhàn)364:加權(quán)嵌套序列和 II
          LeetCode刷題實(shí)戰(zhàn)365:水壺問題
          LeetCode刷題實(shí)戰(zhàn)366:尋找二叉樹的葉子節(jié)點(diǎn)
          LeetCode刷題實(shí)戰(zhàn)367:有效的完全平方數(shù)

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

          手機(jī)掃一掃分享

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

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  国产精品一区人妻精品阁在线 | 午夜激情网站 | 18禁麻豆 | 黄色视频网站观看 | 精品一区二区三区四 |