123
返回列表 发新帖
楼主: heronalps
跳转到指定楼层
上一主题 下一主题
收起左侧

亚马逊OA - 订单匹配问题

全局:
heronalps 发表于 2018-3-18 13:38
这段写得真棒,我之前也把这个问题想复杂了。有一点小小的改进,用一个start变量剪枝一下,可以减少循环 ...

哈哈 确实 都忘了3sum里面也有这个避免重复的套路
回复

使用道具 举报

全局:
另外我发现我代码里面有个bug,在最后一个函数里面

  1.     public static int kindOfCombos(Map<String, Integer> combos) {
  2.         int count = 0;
  3.         for(int i = 1; i <= combos.size(); i++) {
  4.             if (combos.getOrDefault("bom" + i, 0) != 0) {
  5.                 count++;
  6.             }
  7.         }
  8.         return count;
  9.     }
复制代码


要遍历传进来的这个Map的话,不应该用int i = 1然后递增的办法,而应该是用combos.keySet()的办法。
回复

使用道具 举报

🔗
 楼主| heronalps 2018-3-19 09:01:48 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
legendava 2018-3-20 08:21:36 | 只看该作者
全局:
你好, 我也找朋友在组内找到opening 内推了一下,可以问问你这个是什么组吗?
回复

使用道具 举报

🔗
 楼主| heronalps 2018-3-21 04:10:52 | 只看该作者
全局:
legendava 发表于 2018-3-19 19:21
你好, 我也找朋友在组内找到opening 内推了一下,可以问问你这个是什么组吗?

RedShift
回复

使用道具 举报

🔗
zouying594 2018-3-26 10:50:30 | 只看该作者
全局:
有利特扣的638感觉有点相似
回复

使用道具 举报

🔗
kungfu 2018-3-27 01:28:41 | 只看该作者
全局:
亚麻又开始抽奖了?
回复

使用道具 举报

🔗
stms11 2018-10-6 17:33:04 | 只看该作者
全局:
发一个号理解得递归:

  1.     public static List<Integer> match(int[] item, Map<Integer,int[]> patterns){
  2.         List<Integer> bestMatched = new ArrayList<>();
  3.         matchHelper(bestMatched, item, patterns);
  4.         return bestMatched;
  5.     }

  6.     static int matchHelper(List<Integer> matchedRes, int[] item, Map<Integer, int[]> patterns){
  7.         //get data from mem.
  8.         long minCategory = Arrays.stream(item).filter(v->v>0).count();
  9.         if (minCategory==0){
  10.             return 0;
  11.         }
  12.         List<Integer> bestMatched = new ArrayList<>();
  13.         for (Map.Entry<Integer, int[]> pt: patterns.entrySet()) {
  14.             List<Integer> currentMatched = new ArrayList<>();
  15.             int[] newItem = reduce(item, pt.getValue());
  16.             if (newItem!=null) {
  17.                 currentMatched.add(pt.getKey());
  18.                 int restCategory = matchHelper(currentMatched, newItem, patterns);
  19.                 if (minCategory>restCategory){
  20.                     minCategory = restCategory;
  21.                     bestMatched = new ArrayList<>();
  22.                     bestMatched.addAll(currentMatched);
  23.                 }
  24.                 if (minCategory == restCategory){
  25.                     if (bestMatched!=null && currentMatched.size()<bestMatched.size()){
  26.                         bestMatched = new ArrayList<>();
  27.                         bestMatched.addAll(currentMatched);
  28.                     }
  29.                 }
  30.                 currentMatched.remove(currentMatched.size()-1);
  31.             }
  32.         }
  33.         matchedRes.addAll(bestMatched);
  34.         //mem result
  35.         return Long.valueOf(minCategory).intValue();
  36.     }

  37.     private static int[] reduce(int[] item, int[] value) {
  38.         int[] nitem = new int[item.length];
  39.         for (int i=0;i<item.length;i++){
  40.             int newval = item[i] - value[i];
  41.             if (newval<0){
  42.                 return null;
  43.             }
  44.             nitem[i] = newval;
  45.         }
  46.         return nitem;
  47.     }
复制代码
回复

使用道具 举报

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

本版积分规则

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