查看: 7681| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

a code puzzle: missing mail

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
any idea for:


You are the manager of a mail room which is frequently subject to theft. A period of NN days is about to occur, such that on the iith day, the following sequence of events will occur in order:
A package with a value of V_i dollars will get delivered to the mail room (unless V_i = 0, in which case no package will get delivered).
You can choose to pay CC dollars to enter the mail room and collect all of the packages there (removing them from the room), and then leave the room
With probability SS, all packages currently in the mail room will get stolen (and therefore removed from the room).
Note that you're aware of the delivery schedule V_{1..N} , but can only observe the state of the mail room when you choose to enter it, meaning that you won't immediately be aware of whether or not packages were stolen at the end of any given day.
Your profit after the NNth day will be equal to the total value of all packages which you collected up to that point, minus the total amount of money you spent on entering the mail room.
Please determine the maximum expected profit you can achieve (in dollars).
Note: Your return value must have an absolute or relative error of at most 10^{-6}
  to be considered correct.

Constraints
1. 1 ≤ N ≤ 4,000
2. 0 ≤ Vi ≤ 1,000
3. 1≤ C ≤ 1,000
4.  0.0 ≤ S ≤ 1.0  

上一篇:刷题打卡,克服游戏瘾!!!
下一篇:2021刷题打卡
推荐
desgnd 2022-3-16 03:52:25 | 只看该作者
全局:
The DP solution is O(N^2) and can be written iteratively in bottom up form by keeping a memo[(day, value_in_mail_room)] and starting from the last day. The optimal substructure is based on everything relying on the day (and V) and the value in the mail room prior. It is possible to precompute every possible value in the mail room for all days in N^2 time by taking advantage of the fact that you need to take everything every time you enter the mail room. Then you iterate and keep the max and fill in your memo from the ground up . The main loop is for day in N: for value in possible_mail_room_values[day]: ...
possible_mail_room_values[day] is length N so the time complexity in O(N^2) total.

That being said, the iterative approach and the memoized recursive approach above both have the same time complexity. To pass the portal's test cases however you need to optimize the special case S==0 to run in O(N) time by returning max(0, sum(V) - C) early. Bit of hack. Took three days to realize lol.
回复

使用道具 举报

推荐
RocShow 2021-12-11 13:34:36 | 只看该作者
全局:
brute force with cache. got 23/24 passed. the failed one hit some memory limit i guess.

  1. from typing import List
  2. from functools import lru_cache
  3. # Write any import statements here

  4. def getMaxExpectedProfit(N: int, V: List[int], C: int, S: float) -> float:
  5.   # Write your code here
  6.   sys.setrecursionlimit(8100)
  7.   @lru_cache(None)
  8.   def helper(day: int, value_in_mail_room: float):
  9.     if day >= N:
  10.       return 0
  11.     score = 0
  12.     if value_in_mail_room + V[day] > C:
  13.       score = value_in_mail_room + V[day] - C + helper(day + 1, 0)      
  14.     score = max(score, helper(day + 1, (value_in_mail_room + V[day]) * (1 - S)))
  15.     return score
  16.   return helper(0, 0.0)
复制代码
回复

使用道具 举报

🔗
jcarlos 2022-1-5 21:04:44 | 只看该作者
全局:
Have you solved this problem? I would like to see the solution :(
回复

使用道具 举报

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

本版积分规则

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