📣 黑五网一特惠|VIP一年通行证5折,蓝莓、Offer多多同步特惠
楼主: xiubiubong
跳转到指定楼层
上一主题 下一主题
收起左侧

Airbnb电面Skype面

🔗
yucheyang2 2016-10-21 05:48:25 | 只看该作者
全局:
楼主想问下第二题,a,d,2 的2 是connection的意思吗,那每张票是不是还要给出价格呀?还是说2 是价格,那a - d - b 只有三个城市没有到4 啊
回复

使用道具 举报

🔗
yvenica 2016-10-22 02:13:04 | 只看该作者
全局:
楼主问一下floor and ceiling那个是哪题呀?
回复

使用道具 举报

全局:
飞机票那题感觉DP不能做啊,因为可能有环啊。。。
回复

使用道具 举报

🔗
twfx1123 2016-11-2 06:29:46 | 只看该作者
全局:
connection次数应该是小于等于k次吧。
回复

使用道具 举报

🔗
twfx1123 2016-11-2 07:56:28 | 只看该作者
全局:
个人感觉DP做不了那个飞机票的题目。面试官是不是期待dp做记录中间结果呢?不知道。。。掐表写了个graph DFS的,找到所有路径-》计算cost-》找到最小 写完了已经43分钟了。。。

  1. import java.util.*;

  2. public class flightMinCost {
  3.        
  4.         public int minCost(String[] input, char source, char dest, int k){
  5.                 HashMap<Character, HashMap<Character, Integer>> graph = new HashMap<>();
  6.                
  7.                 //build a graph
  8.                 for(String s : input){
  9.                         String [] arr = s.split(",");
  10.                         char start = arr[0].charAt(0);
  11.                         char end = arr[0].charAt(3);
  12.                         int cost = Integer.parseInt(arr[1]);
  13.                        
  14.                         if(!graph.containsKey(start)){
  15.                                 HashMap<Character, Integer> map = new HashMap<Character, Integer>();
  16.                                 map.put(end, cost);
  17.                                 graph.put(start, map);
  18.                         }
  19.                         else{
  20.                                
  21.                                 HashMap<Character, Integer> m = graph.get(start);
  22.                                 m.put(end, cost);
  23.                         }
  24.                 }
  25.                 List<Character> path = new ArrayList<>();
  26.                 List<List<Character>> allPath = new ArrayList<>();
  27.                 HashSet<Character> visited = new HashSet<>();
  28.                
  29.                 helper(source, dest, path, allPath, graph, visited);
  30.                
  31.                 for(char c : graph.keySet()){
  32.                         HashMap<Character, Integer> m = graph.get(c);
  33.                         for(Character nei : m.keySet()){
  34.                                 System.out.println("source is: " + c +" dest is:" + nei + " cost is: " + m.get(nei));
  35.                         }
  36.                 }
  37.                
  38.                 int minCost = Integer.MAX_VALUE;
  39.                
  40.                 for(List<Character> p : allPath){
  41.                         if(p.size() < k){
  42.                                 int cost = getCost(graph, p);
  43.                                 minCost = Math.min(cost, minCost);
  44.                         }
  45.                 }
  46.                
  47.                 return minCost;
  48.         }
  49.        
  50.         int getCost(HashMap<Character, HashMap<Character, Integer>> graph, List<Character> p){
  51.                 int sum = 0;
  52.                
  53.                 for(int i=0; i<p.size()-1; i++){
  54.                         char start = p.get(i);
  55.                         char dest = p.get(i+1);
  56.                         sum += graph.get(start).get(dest);
  57.                 }
  58.                 return sum;
  59.         }
  60.        
  61.         void helper(Character source, Character dest,List<Character> path,List<List<Character>> allPath,
  62.                         HashMap<Character, HashMap<Character, Integer>> graph, HashSet<Character> visited ){
  63.                
  64.                 visited.add(source);
  65.                 path.add(source);
  66.                
  67.                 if(source == dest){
  68.                         allPath.add(new ArrayList<>(path));
  69.                 }
  70.                 else{
  71.                         Set<Character> neis = graph.get(source).keySet();
  72.                          
  73.                         for(char n : neis){
  74.                                 if(!visited.contains(n)){
  75.                                         helper(n, dest, path, allPath, graph, visited);
  76.                                 }
  77.                         }
  78.                 }
  79.                
  80.                 path.remove(path.size()-1);
  81.                 visited.remove(source);
  82.         }
  83.        
  84.        
  85.        

  86.         public static void main(String[] args) {
  87.                  flightMinCost fmc = new flightMinCost();
  88.                  String [] input = {
  89.                                 "A->B,8",
  90.                                 "B->C,4",
  91.                                 "A->D,1",
  92.                                 "D->E,2",
  93.                                 "E->B,3"
  94.                  };
  95.                  
  96.                  System.out.println(fmc.minCost(input, 'A', 'C', 12));
  97.                  

  98.         }

  99. }
复制代码
回复

使用道具 举报

🔗
Alyssa_ 2016-11-3 06:16:44 | 只看该作者
全局:
gy21 发表于 2016-11-2 07:56
个人感觉DP做不了那个飞机票的题目。面试官是不是期待dp做记录中间结果呢?不知道。。。掐表写了个graph DF ...

DP bellman ford算法 别的面经帖子里大牛给的解法
回复

使用道具 举报

无效楼层,该帖已经被删除
🔗
twfx1123 2016-11-4 01:00:04 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

🔗
Alyssa_ 2016-11-4 01:05:39 | 只看该作者
全局:
gy21 发表于 2016-11-4 01:00
求链接。想看看这题。谢谢!

就这个帖子里的第二个题 没代码 你搜一下bellman-ford正好符合题目要求,这个是dp的
回复

使用道具 举报

🔗
twfx1123 2016-11-4 01:17:35 | 只看该作者
全局:
http://www.cnblogs.com/gaochundong/p/bellman_ford_algorithm.html
机票那题根据ls的反馈,找到了这个算法。空气床啊,空气床,考这些你有想过工作中实际需要么!?!
回复

使用道具 举报

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

本版积分规则

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