做试用的网站,网站建设作业有哪些,中小型网站有哪些,网络规划设计师大纲是不是变了3妹#xff1a;“打个中国结#xff0c;再系个红腰带#xff0c; 愿善良的人们天天好运来, 你勤劳生活美, 你健康春常在, 你一生的忙碌为了笑逐颜开。” 2哥 : 3妹#xff0c;元旦快乐啊。 3妹#xff1a;2哥元旦快乐~。 2哥#xff1a;祝新的一年#xff0c;3妹技术突飞…
3妹“打个中国结再系个红腰带 愿善良的人们天天好运来, 你勤劳生活美, 你健康春常在, 你一生的忙碌为了笑逐颜开。” 2哥 : 3妹元旦快乐啊。 3妹2哥元旦快乐~。 2哥祝新的一年3妹技术突飞猛进工资涨涨涨。 3妹祝新的一年2哥能够找到女朋友哈哈哈 2哥新年新气象走带你出去玩哥买单。 3妹好啊好啊我想去锦江乐园玩摩天轮。 2哥说到摩天轮今天的每日一题还没有完成我这里有一首关于摩天轮的题目让我们做完再出去玩吧~ 3妹哼好吧
题目
你正在经营一座摩天轮该摩天轮共有 4 个座舱 每个座舱 最多可以容纳 4 位游客 。你可以 逆时针 轮转座舱但每次轮转都需要支付一定的运行成本 runningCost 。摩天轮每次轮转都恰好转动 1 / 4 周。
给你一个长度为 n 的数组 customers customers[i] 是在第 i 次轮转下标从 0 开始之前到达的新游客的数量。这也意味着你必须在新游客到来前轮转 i 次。每位游客在登上离地面最近的座舱前都会支付登舱成本 boardingCost 一旦该座舱再次抵达地面他们就会离开座舱结束游玩。
你可以随时停下摩天轮即便是 在服务所有游客之前 。如果你决定停止运营摩天轮为了保证所有游客安全着陆将免费进行所有后续轮转 。注意如果有超过 4 位游客在等摩天轮那么只有 4 位游客可以登上摩天轮其余的需要等待 下一次轮转 。
返回最大化利润所需执行的 最小轮转次数 。 如果不存在利润为正的方案则返回 -1 。
示例 1 输入customers [8,3], boardingCost 5, runningCost 6 输出3 解释座舱上标注的数字是该座舱的当前游客数。
8 位游客抵达4 位登舱4 位等待下一舱摩天轮轮转。当前利润为 4 * $5 - 1 * $6 $14 。3 位游客抵达4 位在等待的游客登舱其他 3 位等待摩天轮轮转。当前利润为 8 * $5 - 2 * $6 $28 。最后 3 位游客登舱摩天轮轮转。当前利润为 11 * $5 - 3 * $6 $37 。 轮转 3 次得到最大利润最大利润为 $37 。 示例 2
输入customers [10,9,6], boardingCost 6, runningCost 4 输出7 解释
10 位游客抵达4 位登舱6 位等待下一舱摩天轮轮转。当前利润为 4 * $6 - 1 * $4 $20 。9 位游客抵达4 位登舱11 位等待2 位是先前就在等待的9 位新加入等待的摩天轮轮转。当前利润为 8 * $6 - 2 * $4 $40 。最后 6 位游客抵达4 位登舱13 位等待摩天轮轮转。当前利润为 12 * $6 - 3 * $4 $60 。4 位登舱9 位等待摩天轮轮转。当前利润为 * $6 - 4 * $4 $80 。4 位登舱5 位等待摩天轮轮转。当前利润为 20 * $6 - 5 * $4 $100 。4 位登舱1 位等待摩天轮轮转。当前利润为 24 * $6 - 6 * $4 $120 。1 位登舱摩天轮轮转。当前利润为 25 * $6 - 7 * $4 $122 。 轮转 7 次得到最大利润最大利润为$122 。 示例 3
输入customers [3,4,0,5,1], boardingCost 1, runningCost 92 输出-1 解释
3 位游客抵达3 位登舱0 位等待摩天轮轮转。当前利润为 3 * $1 - 1 * $92 -$89 。4 位游客抵达4 位登舱0 位等待摩天轮轮转。当前利润为 7 * $1 - 2 * $92 -$177 。0 位游客抵达0 位登舱0 位等待摩天轮轮转。当前利润为 7 * $1 - 3 * $92 -$269 。5 位游客抵达4 位登舱1 位等待摩天轮轮转。当前利润为 11 * $1 - 4 * $92 -$357 。1 位游客抵达2 位登舱0 位等待摩天轮轮转。当前利润为 13 * $1 - 5 * $92 -$447 。 利润永不为正所以返回 -1 。 提示
n customers.length 1 n 10^5 0 customers[i] 50 1 boardingCost, runningCost 100
思路 模拟 假设每位游客需要支付的费用是 boardingCost一次轮转有 curCustomers 位游客登上座舱每次轮转的运行成本是 runningCost则摩天轮当前一次轮转的利润是 boardingCost×curCustomers−runningCost其中 boardingCos 和 runningCost的值是已知的curCustomers的值由正在等摩天轮的游客的数量和座舱可以容纳的游客的数量中的最小值决定其中座舱可以容纳的游客的数量为 4。
java代码
class Solution {public int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {int ans -1;int maxProfit 0;int totalProfit 0;int operations 0;int customersCount 0;int n customers.length;for (int i 0; i n; i) {operations;customersCount customers[i];int curCustomers Math.min(customersCount, 4);customersCount - curCustomers;totalProfit boardingCost * curCustomers - runningCost;if (totalProfit maxProfit) {maxProfit totalProfit;ans operations;}}if (customersCount 0) {return ans;}int profitEachTime boardingCost * 4 - runningCost;if (profitEachTime 0) {return ans;}if (customersCount 0) {int fullTimes customersCount / 4;totalProfit profitEachTime * fullTimes;operations fullTimes;if (totalProfit maxProfit) {maxProfit totalProfit;ans operations;}int remainingCustomers customersCount % 4;int remainingProfit boardingCost * remainingCustomers - runningCost;totalProfit remainingProfit;if (totalProfit maxProfit) {maxProfit totalProfit;operations;ans;}}return ans;}
}