当前位置:网站首页>368. Maximum divisible subset dynamic programming
368. Maximum divisible subset dynamic programming
2022-07-16 07:17:00 【Chenchen-】
The original title is :
Power button
Ideas : Dynamic programming + Memory search
inspiration :
Code :
package Algorithm . Dynamic programming .leetcode;
import java.util.*;
/**
* Union checking set
*/
public class leetcode368 {
public static void main(String[] args) {
int[] nums = {1,2,3,4,6,12,24};
List<Integer> list = largestDivisibleSubset(nums);
}
public static List<Integer> largestDivisibleSubset(int[] nums) {
// Prioritize
Arrays.sort(nums);
Map<String,List<Integer>> map = new HashMap<>();
// Dynamic planning results
List<Integer> list = getList(nums,nums.length-1,nums.length-2,map);
// Patch Don't think about it
if(nums[nums.length-1] % nums[nums.length-2] == 0){
list.add(nums[nums.length-1]);
}
// return
return list;
}
/**
*
* @param nums
* @param i Big Count
* j Small trees
* @return
*/
private static List<Integer> getList(int[] nums, int i, int j, Map<String,List<Integer>> map) {
// That means it's gone
if (j == -1||i==0) {
return new ArrayList<>();
}
// Memory search
StringBuilder key = new StringBuilder();
key.append(i);
key.append(j);
if (map.containsKey(key.toString())) {
List<Integer> old = new ArrayList<>();
old.addAll(map.get(key.toString()));
return old;
}
List<Integer> one = new ArrayList<>();
List<Integer> two = new ArrayList<>();
// If you can get rid of it, keep looking , Otherwise, the next number
if(nums[i] % nums[j] == 0){
one = getList(nums,j,j-1,map);
one.add(nums[j]);
}
two = getList(nums,i,j-1,map);
one = one.size()> two.size()? one:two;
List<Integer> newList = new ArrayList<>();
newList.addAll(one);
// Memory search
map.put(key.toString(),newList);
return one;
}
}
边栏推荐
猜你喜欢

Drawing of iterative fractal graphics

368. 最大整除子集 动态规划

Unity3d monobehavior base class

Leetcode lecture - 735 Planetary collision (difficulty: medium)

类型擦除&桥接方法

Unity基础到入门-导航系统(Navigation)

AVL tree - binary lookup tree with equilibrium condition

SAP DUMP CALLBACK_REJECTED_BY_WHITELIST - SE51, RSSCREENPAINTER

rocket目录

Use of rounding, rounding down, rounding up
随机推荐
Amd rDNA 3 Navi 31 flagship GPU is said to be packaged in 3D v-cache and up to 384mb cache
SAP ABAP BP 批量维护邮箱地址
Use the pointer to write a program to insert a character at any position of a string (the position of the inserted character is required to be input by the user from the keyboard).
MyCli之多行模式
Unity3d-小技巧
Three handshakes of TCP
HeadFirst 状态模式 源码
128. 最长连续序列
迭代分形图形的绘制
Drawing of iterative fractal graphics
Lvalue reference, lvalue reference and parameter passing
ThreadLocal源码解析
Hardware course design: MP3 music playback of multi-function player based on stm32
863. All Nodes Distance K in Binary Tree
Implementation of dynamic array vector class template
rocket目录
Gobang (2)
Knowledge of dark horse database
SAP DUMP CALLBACK_REJECTED_BY_WHITELIST - SE51, RSSCREENPAINTER
SAP t-code transaction code set (continuously updated)