LeetCode 1599. Maximum Profit of Operating a Centennial Wheel【数组,模拟】中等

news/2024/4/20 21:54:37/文章来源:https://blog.csdn.net/myRealization/article/details/129375786

本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。

为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库。在这一仓库中,你不仅可以看到LeetCode原题链接、题解代码、题解文章链接、同类题目归纳、通用解法总结等,还可以看到原题出现频率和相关企业等重要信息。如果有其他优选题解,还可以一同分享给他人。

由于本系列文章的内容随时可能发生更新变动,欢迎关注和收藏征服LeetCode系列文章目录一文以作备忘。

You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars.

You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again.

You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation.

Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.

Example 1:

Input: customers = [8,3], boardingCost = 5, runningCost = 6
Output: 3
Explanation: The numbers written on the gondolas are the number of people currently there.
1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14.
2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28.
3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37.
The highest profit was $37 after rotating the wheel 3 times.

Example 2:

Input: customers = [10,9,6], boardingCost = 6, runningCost = 4
Output: 7
Explanation:
1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20.
2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40.
3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60.
4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80.
5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100.
6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120.
7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122.
The highest profit was $122 after rotating the wheel 7 times.

Example 3:

Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92
Output: -1
Explanation:
1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89.
2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177.
3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269.
4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357.
5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447.
The profit was never positive, so return -1.

Constraints:

  • n == customers.length
  • 1 <= n <= 105
  • 0 <= customers[i] <= 50
  • 1 <= boardingCost, runningCost <= 100

题意:作为一个有4个座舱 gondolas(每个座舱可容纳四个人)的摩天轮 Centennial Wheel 的操作员,你能让摩天轮逆时针旋转,每次轮转都需 runningCost 运行成本。给出长度为 n 的数组 customerscustomers[i] 是在第 i 次轮转(从0开始)前到达的新游客数量,这意味着你必须在新游客到来前旋转摩天轮 i 次。或者说,customers[i] 个游客到达后,才旋转第 i + 1 次。

你不能在摩天轮座舱还有空间时让游客等待,即能上多少游客就要上多少游客。每个游客登上座舱时给你带来 boardingCost 收入。一旦座舱到达地面,游客就离开。如果有超过四个游客在等待,则只有四个游客能登上座舱,其他游客等待下次轮转。

你可以在任何时间停止摩天轮,包括在服务所有游客之前 before serving all customers,即不开了、我们不提供服务。 如果你决定停止服务游客,为了使所有游客安全下地,所有后续轮转都是免费的。**返回能最大化利润的最小轮转次数。如果没有正利润方案,则返回 -1


解法 模拟+数组

轮转摩天轮分成两个阶段。第一阶段是前 nnn 次轮转,每次轮转之前都可能有新游客到达。如果在第一阶段结束之后,还有剩余的游客在等摩天轮,则进入第二阶段,将剩余的游客轮转完毕。无论在第一阶段还是第二阶段,我们都可以随时停止摩天轮,只要我们已经获得了最大利润,就可以立刻停下来、不继续轮转。而且,停止后如果还有游客在摩天轮上,则后续所有轮转都是无成本的、也没有利润(不让新游客登上摩天轮)。

对于第一阶段的每次轮转,需要首先计算该次轮转时正在等摩天轮的游客的数量,然后计算该次轮转的利润以及总利润,同时维护最大利润与最大利润的最小轮转次数。具体而言,第 iii 次轮转的流程如下:

  1. customers[i]\textit{customers}[i]customers[i] 是在第 iii 次轮转之前到达的新游客的数量(iii000 开始),使用 customers[i]\textit{customers}[i]customers[i] 的值,更新正在等摩天轮的游客的数量;
  2. 计算登上座舱的游客的数量 curCustomerscurCustomerscurCustomers ,为「正在等摩天轮的游客的数量」和「座舱可以容纳的游客的数量」的最小值;
  3. 从正在等摩天轮的游客的数量中减去登上座舱的游客的数量;
  4. 计算该次轮转的利润,并计算到该次轮转的总利润,假设每位游客需要支付的费用是 boardingCostboardingCostboardingCost ,一次轮转有 curCustomerscurCustomerscurCustomers 位游客登上座舱,每次轮转的运行成本是 runningCostrunningCostrunningCost ,则摩天轮当前一次轮转的利润是 curCustomers×boardingCost−runningCostcurCustomers \times boardingCost - runningCostcurCustomers×boardingCostrunningCost
  5. 如果总利润大于最大利润,则更新最大利润与最大利润的最小轮转次数(为 i+1i + 1i+1 ,因为 customers[i]customers[i]customers[i] 是在第 iii 次轮转前到来的新游客数目,现在完成了第 iii 次轮转,实际上进行了 i+1i + 1i+1 次轮转)。

第一阶段结束后,如果没有剩余的游客在等摩天轮,则直接返回最大利润的最小轮转次数。如果还有剩余的游客在等摩天轮,只有当剩余的游客带来的利润为正时,才需要考虑第二阶段,可能获得更多的利润

由于每位游客需要支付的费用是正数,因此当座舱满舱时(即座舱上有 444 位游客,达到最大容纳数量),可以达到一次轮转的最大利润。

  • 如果一次轮转的最大利润不为正,则第二阶段的利润一定不为正,因此直接返回第一阶段的最大利润的最小轮转次数。
  • 如果一次轮转的最大利润为正,则每次座舱满舱的轮转的利润都为正,因此计算全部满舱轮转之后的总利润,如果大于最大利润,则更新最大利润与最大利润的最小轮转次数。最后可能剩下少于 444 位游客,需要进行最后一次非满舱的轮转,在最后一次轮转之后计算总利润,如果总利润大于最大利润,则更新最大利润与最大利润的最小轮转次数。

代码如下所示:

  • 时间复杂度:O(n)O(n)O(n)
  • 空间复杂度:O(1)O(1)O(1)
class Solution {
public:int minOperationsMaxProfit(vector<int>& customers, int boardingCost, int runningCost) {int profit = 0, maxProfit = 0, maxTimes = -1, remains = 0; for (int i = 0, n = customers.size(); i < n || remains; ++i) {if (i < n) remains += customers[i];int t = (remains > 4) ? 4 : remains;remains -= t;profit += t * boardingCost - runningCost;if (profit > maxProfit) {maxProfit = profit;maxTimes = i + 1; // 注意是i+1}}return maxTimes;}
};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_267634.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[ 攻防演练演示篇 ] 利用 shiro 反序列化漏洞获取主机权限

&#x1f36c; 博主介绍 &#x1f468;‍&#x1f393; 博主介绍&#xff1a;大家好&#xff0c;我是 _PowerShell &#xff0c;很高兴认识大家~ ✨主攻领域&#xff1a;【渗透领域】【数据通信】 【通讯安全】 【web安全】【面试分析】 &#x1f389;点赞➕评论➕收藏 养成习…

ATool软件使用实验(22)

实验目的 1、学习ATool软件监控主机行为的原理&#xff1b; 2、学习利用ATool软件监控可疑进程的行为&#xff1b; 3、学习利用ATool软件实现对本机进行文件、注册表管理&#xff1b; 4、学习利用ATool软件实现对本机进行内核模块信息和HOOK信息查看。 预备知识 ATool是针…

测试按方向的分类

按方向分(都是在系统测试阶段测试的) 功能测试&#xff1a;举例说明什么是功能 性能测试 ①压力测试&#xff1a;不断地增加压力&#xff0c;从而找到系统的极限 ②负载测试&#xff1a;系统在极限工作条件下&#xff0c;最多能持续多久——可能发生内存泄漏/溢出&#xff0c;导…

Appium+Python连接真机、跳过登录页、Unexpected error while obtaining UI hierarchy问题

Appium连接真机 使用数据线连接电脑&#xff0c;然后选择文件传输方式 打开手机设置拉至底部&#xff0c;点击关于手机&#xff0c;连续点击7次版本号打开开发者模式 点击设置中的系统与更新&#xff0c;找到开发者选项----> 打开USB调试即可 在终端中输入adb devices确定…

案例解读| 从集中告警平台发展趋势看城商行如何落地数字化转型(二)

上期我们以具体案例入手&#xff0c;分享了集中告警平台到底应该与集中监控平台解耦还是紧绑定等问题。这一期依旧从具体案例切入&#xff0c;跟大家一起探索下告警与服务台的对接过程&#xff0c;以及这个过程中可能产生的问题。上期内容&#xff0c;一键回顾不迷路→案例解读…

angular技术(持续更新)

css类绑定[class.color-blue]"isBlue()" 如果isBlue()返回为true 这里使用color-blue的class样式style样式绑定[style.background-color]"canclick ? blue: red" 组件与模块模块的元数据*declarations: 用于指定属于这个模块的视图类&#xff08;View Cla…

YOLOV5中添加CBAM模块详解——原理+代码

目录一、前言二、CAM1. CAM计算过程2. 代码实现3. 流程图三、SAM1. SAM计算过程2. 代码实现3. 流程图四、YOLOv5中添加CBAM模块参考文章一、前言 由于卷积操作通过融合通道和空间信息来提取特征&#xff08;通过NNNNNN的卷积核与原特征图相乘&#xff0c;融合空间信息&#xff…

代码随想录-51-110.平衡二叉树

目录前言题目1.求高度和深度的区别节点的高度节点的深度2. 本题思路分析&#xff1a;3. 算法实现4. pop函数的算法复杂度5. 算法坑点前言 在本科毕设结束后&#xff0c;我开始刷卡哥的“代码随想录”&#xff0c;每天一节。自己的总结笔记均会放在“算法刷题-代码随想录”该专…

学习笔记:基于SpringBoot的牛客网社区项目实现(二)之Spring MVC入门

1.1 函数的返回值为空&#xff0c;因为可以使用response对象向浏览器返回数据。声明了request对象和response对象&#xff0c;dispatcherservlet自动将这两个对象传入 RequestMapping("/http")public void http(HttpServletRequest request, HttpServletResponse re…

不会吧,难道真的有程序员不知道怎么接单赚钱吗?

随着大环境逐渐转好&#xff0c;跳槽、新工作、兼职等等机会都浮出水面。抛开跳槽、新工作不谈&#xff0c;今天就专门来说说程序员接单赚钱有哪些靠谱的平台。 首先分享一波关于接私活有哪些注意事项&#xff0c;给大家提个醒&#xff0c;避免盲目入坑。 一、程序员接单须知…

深度学习知识点全面总结_深度学习总结

深度学习知识点全面总结_深度学习总结 神经网络与深度学习结构(图片选自《神经网络与深度学习》一邱锡鹏) 目录 常见的分类算法 一、深度学习概念 1.深度学习定义 2.深度学习应用 3.深度学习主要术语 二、神经网络基础 1. 神经网络组成 感知机 多层感知机 3.前向传播…

复位和时钟控制(RCC)

目录 复位 系统复位 电源复位 备份区复位 时钟控制 什么是时钟&#xff1f; 时钟来源 二级时钟源: 如何使用CubeMX配置时钟 复位 系统复位 当发生以下任一事件时&#xff0c;产生一个系统复位&#xff1a;1. NRST引脚上的低电平(外部复位) 2. 窗口看门狗计数终止(WWD…

项目实战典型案例27——单表的更新接口有9个之多

单表的更新接口有9个之多一&#xff1a;背景介绍环境准备引入pom依赖配置数据库连接mybatis配置文件Mybatis的配置类编写通用的更新语句可以覆盖的更新接口暂时无法覆盖的接口测试四&#xff1a;总结五&#xff1a;升华一&#xff1a;背景介绍 本篇博客是对项目开发中出现的单…

197.Spark(四):Spark 案例实操,MVC方式代码编程

一、Spark 案例实操 1.数据准备 电商网站的用户行为数据,主要包含用户的 4 种行为:搜索,点击,下单,支付 样例类: 2. Top10 热门品类 先按照点击数排名,靠前的就排名高;如果点击数相同,再比较下单数;下单数再相同,就比较支付数。 我们有多种写法,越往后性能越…

k8s学习之路 | k8s 工作负载 ReplicaSet

文章目录1. ReplicaSet 基础概念1.1 RS 是什么&#xff1f;1.2 RS 工作原理1.3 什么时候使用 RS1.4 RS 示例1.5 非模板 Pod 的获得1.6 编写 RS1.7 使用 RS1.8 RS 替代方案2. ReplicaSet 与 ReplicationController2.1 关于 RS、RC2.2 两者的选择器区别2.3 总结1. ReplicaSet 基础…

yii2项目使用frp https2http插件问题

yii2内网项目&#xff0c;使用frp进行内网穿透&#xff0c;使用 https2http插件把内网服务器http流量转成https&#xff0c;会存在一个问题&#xff1a;当使用 $this->redirect(...) 或 $this->goHome() &#xff08;其实用的也是前者&#xff09;等重定向时&#xff0c;…

物联网毕设 -- 智能厨房监测系统(改)

前言 在家庭生活中&#xff0c;厨房是必不可少的&#xff0c;所以厨房的安全问题关乎着我们大家的生命&#xff0c;所以提出智能厨房监测系统&#xff0c;目的就是为我们减少不必要的安全问题 ⚠️⚠️&#xff08;本文章仅提供思路和实现方法&#xff0c;并不包含代码&#x…

javaWeb在线考试系统

一、项目简介 本项目是一套javaWeb在线考试系统&#xff0c;主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的Java学习者。 包含&#xff1a;项目源码、数据库脚本等&#xff0c;该项目附带全部源码可作为毕设使用。 项目都经过严格调试&#xff0c;eclipse 确保…

DBeaver连接mysql、oracle数据库

1. DBeaver连接mysql 1&#xff09; 下载DBeaver https://dbeaver.io/download/&#xff0c;并安装 2) 新建数据库连接 3&#xff09;选择mysql驱动程序 4&#xff09;填写连接设置内容 5&#xff09;点击 “编辑驱动设置”&#xff0c;并填写相关信息 6&#xff09;选择本地…

厦大纪老师chatgpt相关讲座3.7

在线更新数据&#xff0c;迭代学习训练&#xff0c;进而提高模型性能。 比较明显的是API部分&#xff0c;这一步学习的就是intruction,实现人机写作的复杂系统工程 数据充足&#xff0c;维基类似于百度百科 transformer结构更有优势&#xff0c;预测下一个字&#xff0c;模型越…