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

Google MTV 20160921 onsite.

🔗
Romeobaby 2016-9-27 08:50:33 | 只看该作者
全局:
搭车同求最大假期。。。马克
回复

使用道具 举报

🔗
huanyunshi 2016-9-29 02:01:24 | 只看该作者
全局:
最大假期问题,我按照我的理解写了一段测试代码,欢迎评论:
  1. class City(object):
  2.     def __init__(self, id, dstcity, weekholidays):
  3.         self.cityid = id
  4.         # key:city_id,value-distance to the destination
  5.         self.dstcity = dstcity
  6.         # x weeks holidays, index means the number of weeks
  7.         self.weekholidays = weekholidays

  8. class MaxHoliday(object):
  9.     def dfs(self, citys, start_city, week, maxweek, memhash, maxdistance):
  10.         if week == maxweek:
  11.             return 0
  12.         # if we have searched before, directly return
  13.         key = str(week) + '-' + str(start_city.cityid)
  14.         if key in memhash:
  15.             return memhash[key]
  16.         maxdays = 0
  17.         for cityid in start_city.dstcity:
  18.             if start_city.dstcity[cityid] <= maxdistance:
  19.                 maxdays = max(maxdays, self.dfs(citys, citys[cityid], week + 1, maxweek, memhash, maxdistance))
  20.         maxdays += start_city.weekholidays[week]
  21.         memhash[key] = maxdays
  22.         return maxdays

  23.     def maxholidayweeks(self, citys, maxweek, maxdistance):
  24.         # key: weeky+cityid,value: maxholiday
  25.         memhash = {}
  26.         return self.dfs(citys, citys['A'], 0, maxweek, memhash, maxdistance)


  27. cityA = City('A', {'B': 6, 'C': 2, 'D': 30}, [2, 0, 3])
  28. cityB = City('B', {'A': 6, 'C': 20, 'D': 7}, [1, 1, 0])
  29. cityC = City('C', {'A': 2, 'B': 20, 'D': 10}, [1, 1, 1])
  30. cityD = City('D', {'A': 20, 'B': 7, 'C': 10}, [0, 0, 2])
  31. citys = {'A': cityA, 'B': cityB, 'C': cityC, 'D': cityD}

  32. T = MaxHoliday()
  33. print T.maxholidayweeks(citys, 3, 2)
复制代码
回复

使用道具 举报

🔗
lzb700m 2016-10-1 03:21:51 | 只看该作者
全局:
第四题可以直接找就可以。
  1.         public String solve(String s, int k) {
  2.                 if (s == null || s.length() < k)
  3.                         throw new IllegalArgumentException();

  4.                 int n = s.length();
  5.                 int left = 0;
  6.                 int right = n - k;
  7.                 StringBuilder ansBuilder = new StringBuilder();
  8.                 while (right < n) {
  9.                         int best = left;
  10.                         for (int i = left; i <= right; i++) {
  11.                                 if (s.charAt(i) < s.charAt(best))
  12.                                         best = i;
  13.                         }
  14.                         ansBuilder.append(s.charAt(best));
  15.                         left = best + 1;
  16.                         right += 1;
  17.                 }
  18.                 return ansBuilder.toString();
  19.         }
复制代码
回复

使用道具 举报

🔗
lzb700m 2016-10-1 03:39:58 | 只看该作者
全局:
Iterator那个题目能用额外空间吗?
  1. public class P000_FunnyIterator implements Iterable<Integer> {
  2.         List<Integer> data;

  3.         P000_FunnyIterator(int[] input) {
  4.                 if (input == null || (input.length & 1) == 1)
  5.                         throw new IllegalArgumentException();
  6.                 int n = input.length;

  7.                 this.data = new ArrayList<>();
  8.                 for (int i = 0; i < n; i += 2) {
  9.                         for (int c = 0; c < input[i]; c++)
  10.                                 this.data.add(input[i + 1]);
  11.                 }
  12.         }

  13.         @Override
  14.         public Iterator<Integer> iterator() {
  15.                 return data.iterator();
  16.         }
  17. }
复制代码
回复

使用道具 举报

🔗
lzb700m 2016-10-1 03:57:51 | 只看该作者
全局:
不用额外空间的话也可以
  1. public class P000_FunnyIterator implements Iterator<Integer> {
  2.         int[] data;
  3.         int curPos = 0;
  4.         int curCount = 0;

  5.         P000_FunnyIterator(int[] input) {
  6.                 if (input == null || (input.length & 1) == 1)
  7.                         throw new IllegalArgumentException();
  8.                 for (int i = 0; i < input.length; i += 2)
  9.                         if (input[i] < 0)
  10.                                 throw new IllegalArgumentException();
  11.                 this.data = input;
  12.         }

  13.         @Override
  14.         public boolean hasNext() {
  15.                 while (curPos < data.length && data[curPos] == 0)
  16.                         curPos += 2;

  17.                 return curPos < data.length;
  18.         }

  19.         @Override
  20.         public Integer next() {
  21.                 int ans = data[curPos + 1];
  22.                 curCount += 1;
  23.                 if (curCount == data[curPos]) {
  24.                         curPos += 2;
  25.                         curCount = 0;
  26.                 }
  27.                 return ans;
  28.         }

  29.         @Override
  30.         public void remove() {
  31.                 throw new UnsupportedOperationException();
  32.         }
复制代码
回复

使用道具 举报

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

使用道具 举报

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

使用道具 举报

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

使用道具 举报

🔗
liurudahai 2016-10-3 05:12:12 | 只看该作者
全局:
第二题是直接暴力输出么?
回复

使用道具 举报

🔗
hxtang 2016-10-3 07:52:31 | 只看该作者
全局:
liurudahai 发表于 2016-10-3 05:10
突然看懂这题了,楼主说是BFS,但其实是DFS把,DFS可以算完一条路径的总时间之后再算另一条路径,比较清 ...

这个题飞行时间是不计在假期/工作时间里的,只是用来约束从一个城市能飞到哪些城市。
回复

使用道具 举报

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

本版积分规则

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