当前位置:网站首页>Dynamic subset division problem
Dynamic subset division problem
2022-04-23 13:45:00 【Du Xiaorui】
Today's daily question is still a dynamic planning question .
Title Description

Title address : The largest divisible subset
Dynamic programming
If violence law is considered, how should this problem be done , The easiest way to think of is to enumerate each element as the beginning , Then traverse the other elements . But this method is not reliable , Take an example :
[ 9 , 18 , 54 , 90 , 108 , 180 , 360 , 540 , 720 ] [9,18,54,90,108,180,360,540,720] [9,18,54,90,108,180,360,540,720] Such an array , from 9 If you start, you'll get [ 9 , 18 , 54 , 108 , 540 ] [9,18,54,108,540] [9,18,54,108,540], But there are actually longer results [ 9 , 18 , 90 , 180 , 360 , 720 ] [9,18,90,180,360,720] [9,18,90,180,360,720], The title requires us to return the longest result , This method is obviously not working .
Let's start thinking about how to use dynamic programming to solve this problem . This problem is a sequence DP problem , Each state depends on the relationship with the previous state , in other words , The current element nums[i] Can you connect it to nums[j] Back , Depending on nums[i] % nums[j] Whether the result is 0. Understand this , So let's define the array dp[],dp[i] Indicates the position of the current element in the sequence . Let's give you an example :
| nums | 2 | 4 | 7 | 8 | 9 | 12 | 16 | 20 |
|---|---|---|---|---|---|---|---|---|
| dp | 1 | 2 | 1 | 3 | 1 | 3 | 4 | 3 |
Defined dp after , How to get dp[i] Well . First, we have to arrange the array from small to large , Because to make nums[i] % nums[j] be equal to 0,nums[i] It must be better than nums[j] Great talent . And then in turn i The previous numbers are % operation , If the result is 0, that dp[i] Is equal to dp[j]+1, That is, in position j After the element of . But because there is more than one element ahead , So we need to update dp[i], Choose the biggest dp[i].
obtain dp Array result , That is, after the results in the table above , Let's go through the table backwards and take out the longest string .
class Solution:
def largestDivisibleSubset(self, nums: List[int]) -> List[int]:
n = len(nums)
nums.sort()
dp = [1] * n
maxSize = 1
maxVal = dp[0]
for i in range(1,n):
for j in range(i):
if nums[i] % nums[j] == 0:
dp[i] = max(dp[i], dp[j]+1)
if dp[i] > maxSize:
maxSize = dp[i]
maxVal = nums[i]
res = []
if maxSize==1:return [nums[0]]
k = n-1
while k >= 0:
if dp[k] == maxSize and maxVal % nums[k] == 0:
res.append(nums[k])
maxVal = nums[k]
maxSize = maxSize-1
k = k-1
return res
maxSize The function of is to record the length of the longest sequence , When the result array is finally obtained, each element is added maxSize Just subtract one .maxVal What is recorded is the maximum value of the maximum sequence , Because when you finally get the result, there may be dp Multiple positions of the array are the same number , It needs to be used at this time maxVal Conduct % operation , The result is 0 Is the element of this result sequence .
Last , Because it's traversing backwards , So the final returned results are in reverse order .
版权声明
本文为[Du Xiaorui]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230556585176.html
边栏推荐
- Remove the status bar
- Oracle database recovery data
- Oracle RAC database instance startup exception analysis IPC send timeout
- Common types and basic usage of input plug-in of logstash data processing service
- pycharm Install packages failed
- Launcher hides app icons that do not need to be displayed
- RAC environment alert log error drop transient type: systp2jw0acnaurdgu1sbqmbryw = = troubleshooting
- Detailed explanation of ADB shell top command
- Troubleshooting of expdp export error when Oracle table has logical bad blocks
- Oracle clear SQL cache
猜你喜欢

Detailed explanation of ADB shell top command
![[point cloud series] foldingnet: point cloud auto encoder via deep grid deformation](/img/c0/cc29ae6948dbe42954cd9da326ef77.png)
[point cloud series] foldingnet: point cloud auto encoder via deep grid deformation

Common types and basic usage of input plug-in of logstash data processing service

JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes

OSS cloud storage management practice (polite experience)

SAP ui5 application development tutorial 72 - trial version of animation effect setting of SAP ui5 page routing

On the bug of JS regular test method

Usereducer basic usage

Lenovo Saver y9000x 2020

Logstash数据处理服务的输入插件Input常见类型以及基本使用
随机推荐
Processing of ASM network not automatically started in 19C
Failure to connect due to improper parameter setting of Rac environment database node. Troubleshooting
[indicators] precision, recall
Detailed explanation of constraints of Oracle table
面试官给我挖坑:URI中的 “//” 有什么用?
Oracle job scheduled task usage details
At the same time, the problems of height collapse and outer margin overlap are solved
Test on the time required for Oracle to delete data with delete
[multi screen interaction] realize dual multi screen display II: startactivity mode
Oracle index status query and index reconstruction
10g database cannot be started when using large memory host
What does the SQL name mean
Utilisation de GDB
Explanation of input components in Chapter 16
SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置试读版
Interface idempotency problem
[machine learning] Note 4. KNN + cross validation
Window analysis function last_ VALUE,FIRST_ VALUE,lag,lead
联想拯救者Y9000X 2020
pycharm Install packages failed