楼主: wangmengcathy
跳转到指定楼层
上一主题 下一主题
收起左侧

新鲜YouTube Onsite面经

全局:
题目都不容易,感觉lz实力挺强的,答得也挺好的,

第二轮啤酒那题,我觉得lz的instinct是对的,应该是要用backtracking来做的,试着写了下,不知道对不对。

  1. public class BeerMachineCombination {

  2.     private static List<List<Integer>> result;
  3.     private static int lower, higher;
  4.     public static List<List<Integer>> combination(BeerRange[] buckets, int minVolume, int maxVolume) {
  5.         result = new ArrayList<List<Integer>>();
  6.         if (buckets == null || minVolume > maxVolume || buckets.length == 0) return result;
  7.         lower = minVolume;
  8.         higher = maxVolume;
  9.         helper(buckets, 0, 0, 0, new ArrayList<Integer>());
  10.         boolean found = false;
  11.         for (List<Integer> list : result) {
  12.             if (list.size() > 0) {
  13.                 found = true;
  14.                 System.out.println("TRUE");
  15.                 for (int d : list) {
  16.                     System.out.print(d + " ");
  17.                 }
  18.                 System.out.println();
  19.             }
  20.         }
  21.         if (!found) {
  22.             System.out.println("FALSE");
  23.         }
  24.         return result;
  25.     }
  26.    
  27.     private static void helper(BeerRange[] buckets, int start, int preMin, int preMax, List<Integer> temp) {
  28.         if (preMin >= lower && preMax <= higher) {
  29.             result.add(new ArrayList<Integer>(temp));
  30.             return;
  31.         }
  32.         for (int i = start; i < buckets.length; i++) {
  33.             int currMin = buckets[i].min, currMax = buckets[i].max;
  34.             if (preMin + currMin > higher) continue;
  35.             if (preMax + currMin > higher) continue;
  36.             temp.add(i);
  37.             helper(buckets, i, preMin + currMin, preMax + currMax, temp);
  38.             temp.remove(temp.size() - 1);
  39.         }
  40.     }
  41.    
  42.     public static void main(String[] args) {
  43.        // BeerRange[] beer = {new BeerRange(100,150), new BeerRange(200,250), new BeerRange(300,350)};
  44.         BeerRange[] beer = {new BeerRange(100,150), new BeerRange(200,300), new BeerRange(300,350)};
  45.         int minV = 300, maxV = 400;
  46.         combination(beer, minV, maxV);        
  47.     }
  48. }

  49. class BeerRange {
  50.     int min, max;
  51.     BeerRange(int min, int max) {
  52.         this.min = min;
  53.         this.max = max;
  54.     }
  55. }
复制代码
回复

使用道具 举报

🔗
jy_121 2016-5-5 01:37:32 | 只看该作者
全局:
javaprogrammer 发表于 2016-2-2 12:04
题目都不容易,感觉lz实力挺强的,答得也挺好的,

第二轮啤酒那题,我觉得lz的instinct是对的,应该是要 ...

你好,我想请教一下这段代码。为什么你的helper函数里每次for循环都要从start(上次选择的杯子)开始?谢谢
回复

使用道具 举报

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

本版积分规则

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