📣 黑五网一特惠|VIP一年通行证5折,蓝莓、Offer多多同步特惠
123
返回列表 发新帖
楼主: 匿名
跳转到指定楼层
上一主题 下一主题
收起左侧

亚麻oa2-NG

🔗
liujunyu321 2020-9-5 03:21:36 | 只看该作者
全局:
轩轩要更勤快 发表于 2020-9-3 23:18
求问,衣伞耳是啥题目呀。感谢,好人一生平安

力扣去看一下不就知道了
回复

使用道具 举报

🔗
paulbro 2020-9-10 07:27:16 | 只看该作者
全局:
本帖最后由 paulbro 于 2020-9-10 08:01 编辑

拿着练练手:

在线:https://github.com/lanshunfang/l ... e%20max%20profit.js


如果有帮助,求赏米。

  1. /*
  2. [url]https://www.1point3acres.com/bbs/thread-661957-2-1.html[/url]

  3. 1 <= numSuppliers <= 10^5

  4. // items left at supplier#i,
  5. // e.g., [3, 2, 5] means
  6. // supplier #0 has 3 items, supplier#1 has 2 items, supplier#2 has 5 items
  7. // if we buy the first item from supplier#0
  8.     // we will have 3 unit profit
  9.     // the supplier will raise the price,
  10.     // so that when we buy the second item from the same supplier,
  11.     // we only have 2 unit profit
  12. // 0 <= i <= numSuppliers
  13. 1 <= inventory[i] <= 10^5

  14. // how many items we should buy
  15. 1 <= order <= sum of inventory
  16. */

  17. const findMaxProfit = (numSuppliers, inventory, order) => {
  18.     let supplierIdx = 1;
  19.     let maxProfit = 0;
  20.     inventory.sort(
  21.         (a, b) => b - a
  22.     );
  23.     inventory.push(0);
  24.     while (order >= 0 && supplierIdx < inventory.length) {
  25.         while (
  26.             supplierIdx < inventory.length
  27.             && inventory[supplierIdx - 1] === inventory[supplierIdx]
  28.             ) {
  29.             supplierIdx++;
  30.         }
  31.         if (inventory[supplierIdx - 1] === 0) {
  32.             break;
  33.         }

  34.         let supplierMultiplier = supplierIdx;
  35.         let diff = inventory[supplierIdx - 1] - inventory[supplierIdx];
  36.         let localCountToOrder = diff * supplierMultiplier;
  37.         let localProfit = inventory[supplierIdx - 1];
  38.         if (order < localCountToOrder) {
  39.             localCountToOrder = order;
  40.         }
  41.         order -= localCountToOrder;
  42.         while (localCountToOrder > 0 && localProfit >= inventory[supplierIdx]) {
  43.             const currentCountToTake = Math.min(supplierMultiplier, localCountToOrder);
  44.             maxProfit += localProfit * currentCountToTake
  45.             localProfit--;
  46.             localCountToOrder -= currentCountToTake;
  47.         }
  48.         supplierIdx++;
  49.     }

  50.     return maxProfit;
  51. }

  52. let res;
  53. res = findMaxProfit(3, [3, 2, 5], 4);
  54. console.log(res);

  55. res = findMaxProfit(3, [5, 5, 5], 6);
  56. console.log(res);

  57. res = findMaxProfit(2, [0, 0], 6);
  58. console.log(res);

  59. res = findMaxProfit(2, [5, 0], 6);
  60. console.log(res);
复制代码

评分

参与人数 1大米 +1 收起 理由
claireanais + 1 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
HayleyTGKX 2020-9-10 07:52:01 | 只看该作者
全局:
积分不够看不到隐藏内容,求大米
回复

使用道具 举报

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

本版积分规则

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