活跃农民
- 积分
- 559
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-4-4
- 最后登录
- 1970-1-1
|
写了个dp,但是加了个丑陋的去重,所以时间复杂度也不低
- package dropbox;
- import java.util.*;
- public class Soda{
- public static void main(String[] args){
- System.out.println(solve(new int[]{1,6,12}, 12));
- }
- private static List<List<Integer>> solve(int[] sizes, int total){
- Map<Integer, List<List<Integer>>> hash = new HashMap<>();
- for (int i = 0; i <= total; i++){
- hash.put(i, new ArrayList<List<Integer>>());
- }
- for (int i = 1; i <= total; i++){
- List<List<Integer>> container = hash.get(i);
- for (int size : sizes){
- if (size <= i){
- if (hash.get(i - size).size() == 0){
- List<Integer> temp = new ArrayList<>();
- temp.add(size);
- container.add(temp);
- }
- else{
- for (List<Integer> list : hash.get(i - size)){
- List<Integer> temp = new ArrayList<>();
- temp.add(size);
- temp.addAll(list);
- if (!hasDup(container, temp)){
- container.add(temp);
- }
-
- //System.out.println("i: " + i + " size: " + size);
- //System.out.println("container: " + container);
- }
- }
- }
- }
- }
-
- //System.out.println(hash.get(total));
-
- return hash.get(total);
- }
-
- private static boolean hasDup(List<List<Integer>> container, List<Integer> checker){
- for (List<Integer> list : container){
- if (list.containsAll(checker)) return true;
- }
-
- return false;
- }
- }
复制代码 |
|