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

亚麻 SDE II OA

🔗
匿名用户-HAAXD  2025-2-19 09:53:04 |倒序浏览

2025(1-3月) 码农类General 硕士 全职@amazon - 猎头 - 在线笔试  | 🙁 Negative 😣 Hard | Fail | 在职跳槽

本帖子中包含更多资源

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

x

评分

参与人数 3大米 +8 收起 理由
ophunter + 1 很有用的信息!
匿名用户-EIF6A + 5 欢迎分享你知道的情况,会给更多大米奖励!
howardx2017 + 2 给你点个赞!

查看全部评分


上一篇:亚麻 SDE II OA
下一篇:Atlassian Karat 挂经
全局:

  1. from typing import List
  2. from heapq import heapify, heappop, heappush

  3. class Solution:
  4.     def cal_min_trucks(
  5.         self,
  6.         quantities: List[int],
  7.         truck_cap: int,
  8.         max_parcel_per_type: int,
  9.     ) -> int:
  10.         hq = [-q for q in quantities]
  11.         heapify(hq)
  12.         res = 0
  13.         while hq:
  14.             load = 0
  15.             tmp_for_used_type = []
  16.             while load < truck_cap and hq:
  17.                 # choose the type with the most parcel to put into the current truck
  18.                 curr_type_cnt = heappop(hq)
  19.                 to_put = min(-curr_type_cnt, truck_cap - load, max_parcel_per_type)
  20.                 type_remaining = -curr_type_cnt - to_put
  21.                 # cache the remainings and reput them back for the next round, you can not put them back to hq now to avoid next round of the hq push pick this type back again
  22.                 if type_remaining:
  23.                     tmp_for_used_type.append(-type_remaining)
  24.                 # update the status of parcel and truck
  25.                 load += to_put
  26.             # now we finished loading the current truck, update the parcel status and truck status
  27.             res += 1
  28.             for i in tmp_for_used_type:
  29.                 heappush(hq, i)
  30.         return res
  31. if __name__ == '__main__':
  32.     size_q = int(input().strip())
  33.     quantities = []
  34.     for _ in range(size_q):
  35.         quantities.append(int(input().strip()))
  36.     truck_cap = int(input().strip())
  37.     max_parcel_per_type = int(input().strip())
  38.     print(Solution().cal_min_trucks(quantities, truck_cap, max_parcel_per_type))



  39.          
复制代码
回复

使用道具 举报

全局:
Should be able to pass the test on screenshot
回复

使用道具 举报

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

本版积分规则

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