📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
楼主: Tianwen
跳转到指定楼层
上一主题 下一主题
收起左侧

Airbnb onsite 跪经

🔗
 楼主| Tianwen 2019-2-18 11:38:26 | 只看该作者
全局:
本人粗鄙的想法。


  1. def optimize_requests(requests: list):
  2.         if not requests:
  3.                 return 0, []

  4.         requests = sorted(requests, key=lambda x: (x[0], -x[1]))
  5.         print(requests)

  6.         # end time for current plan, -total_profit current plan, path
  7.         pq = [(requests[0][1], requests[0][0] - requests[0][1], [requests[0]])]

  8.         for idx, item in enumerate(requests[1:], 1):
  9.                 if requests[idx][0] == requests[idx - 1][0] and requests[idx][1] <= requests[idx - 1][1]:
  10.                         continue

  11.                 start, end = item
  12.                 profit, path = 0, []
  13.                 if pq[0][0] <= start:
  14.                         _, profit_old, path_old = pq[0]
  15.                         profit, path = profit_old, path_old
  16.                         heapq.heappop(pq)

  17.                 profit += -(end - start)
  18.                 path.append((start, end))
  19.                 heapq.heappush(pq, (end, profit, path))

  20.         for plan in pq:
  21.                 print(plan)

  22.         print("Final answer:\n")
  23.         _, profit, path = min(pq, key=lambda x: x[1])
  24.         print(-profit, path)


  25. inputs = [(1, 3), (2, 5), (10, 17), (8, 11), (16, 21), (10, 11), (16, 17), (23, 26), (25, 30)]
  26. optimize_requests(inputs)

复制代码
回复

使用道具 举报

🔗
 楼主| Tianwen 2019-2-18 11:39:15 | 只看该作者
全局:
本人粗鄙的想法。其实是meeting room的变种,用PQ记/选出来所有可能的安排计划,然后遍历一遍找到最大的的安排
回复

使用道具 举报

全局:
第五题难道不是sort一下,过一遍就行了嘛,比较一下conflict的interval t2-t1的和与较大的那个interval,取大的那个? 你们说的我怎么都看不懂。
回复

使用道具 举报

🔗
lijing2441 2019-2-19 12:51:32 | 只看该作者
全局:
可以请问一下楼主system design那一轮具体的题目吗?
回复

使用道具 举报

🔗
 楼主| Tianwen 2019-2-19 21:56:23 | 只看该作者
全局:
lijing2441 发表于 2019-2-19 12:51
可以请问一下楼主system design那一轮具体的题目吗?

不好意思,时间有点久远,细节记不清了,不过就是设计各种表格去存user和listing,然后对应的搜索。
回复

使用道具 举报

🔗
joanff 2019-2-20 04:35:47 | 只看该作者
全局:
看看 谢谢分享
回复

使用道具 举报

🔗
chipmunks123 2019-2-20 12:22:01 | 只看该作者
全局:
第五题应该用dp来解吧.
例如lz的例子,按照lz的代码跑出来的结果是 15 [(1, 3), (8, 11), (16, 21), (25, 30)]
但是实际上应该是  16 [25,30] [16,21] [8,11] [2,5]
回复

使用道具 举报

🔗
rongxiantian 2019-2-22 14:06:38 | 只看该作者
全局:
同认为是dp,第一个区间应该是[2,5]
回复

使用道具 举报

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

使用道具 举报

🔗
cencenseven 2019-6-9 07:17:06 | 只看该作者
全局:
dfs
  1. class solution:
  2.     def dfs(self, start_pos, profit, prev_end):
  3.         #find next eligible request
  4.         while start_pos < len(self.requests) and prev_end > self.requests[start_pos][0]:
  5.             start_pos += 1

  6.         #if no eligible session left can be added: track the profits
  7.         if start_pos >= len(self.requests):
  8.             if profit > self.max_profit:
  9.                 self.ans = self.cur[:]
  10.                 self.max_profit = profit
  11.         else:
  12.             #select this interval
  13.             new_profit = profit + self.requests[start_pos][1] - self.requests[start_pos][0]
  14.             self.cur.append(self.requests[start_pos])
  15.             self.dfs(start_pos+1, new_profit, self.requests[start_pos][1])
  16.             self.cur.pop()

  17.             #not select this interval
  18.             self.dfs(start_pos+1, profit, prev_end)


  19.     def optimize_requests(self, requests: list):
  20.         self.requests = sorted(requests)
  21.         #for each request, we have options to have it or not have it
  22.         self.ans = []
  23.         self.cur = []
  24.         self.max_profit = 0
  25.         self.dfs(0, 0, 0)
  26.         print("max profit is {}".format(self.max_profit))
  27.         return self.ans
复制代码
回复

使用道具 举报

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

本版积分规则

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