📣 独立日限时特惠: VIP通行证立减$68
12
返回列表 发新帖
楼主: Artifact
跳转到指定楼层
上一主题 下一主题
收起左侧

Dropbox Intern OA 电面 两轮

🔗
itgoujie2 2017-8-30 13:49:28 | 只看该作者
全局:
写了个dp,但是加了个丑陋的去重,所以时间复杂度也不低
  1. package dropbox;


  2. import java.util.*;

  3. public class Soda{

  4.         public static void main(String[] args){
  5.                 System.out.println(solve(new int[]{1,6,12}, 12));
  6.         }

  7.         private static List<List<Integer>> solve(int[] sizes, int total){
  8.                 Map<Integer, List<List<Integer>>> hash = new HashMap<>();

  9.                 for (int i = 0; i <= total; i++){
  10.                         hash.put(i, new ArrayList<List<Integer>>());
  11.                 }

  12.                 for (int i = 1; i <= total; i++){
  13.                         List<List<Integer>> container = hash.get(i);
  14.                         for (int size : sizes){
  15.                                 if (size <= i){
  16.                                         if (hash.get(i - size).size() == 0){
  17.                                                 List<Integer> temp = new ArrayList<>();
  18.                                                 temp.add(size);
  19.                                                 container.add(temp);
  20.                                         }
  21.                                         else{
  22.                                                 for (List<Integer> list : hash.get(i - size)){
  23.                                                         List<Integer> temp = new ArrayList<>();
  24.                                                         temp.add(size);
  25.                                                         temp.addAll(list);
  26.                                                         if (!hasDup(container, temp)){
  27.                                                                 container.add(temp);
  28.                                                         }
  29.                                                        
  30.                                                         //System.out.println("i: " + i + " size: " + size);
  31.                                                         //System.out.println("container: " + container);
  32.                                                 }
  33.                                         }
  34.                                 }
  35.                         }
  36.                 }
  37.                
  38.                 //System.out.println(hash.get(total));
  39.                
  40.                 return hash.get(total);
  41.         }
  42.        
  43.         private static boolean hasDup(List<List<Integer>> container, List<Integer> checker){
  44.                 for (List<Integer> list : container){
  45.                         if (list.containsAll(checker)) return true;
  46.                 }
  47.                
  48.                 return false;
  49.         }
  50. }
复制代码
回复

使用道具 举报

🔗
seasean 2017-11-27 09:21:12 | 只看该作者
全局:
这题应该是lc里的combination sum 而不是 combination sum II 吧
回复

使用道具 举报

🔗
zjy6 2017-12-16 15:47:06 | 只看该作者
全局:
seasean 发表于 2017-11-27 09:21
这题应该是lc里的combination sum 而不是 combination sum II 吧

对吧。combination sum 是可以用多次
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表