哪里做网站做的好,西地那非片的功能主治,阿里云添加网站,威海住房建设部官方网站通过万岁#xff01;#xff01;#xff01;
题目#xff1a;就是一个摩天轮#xff0c;一共有4个仓位#xff0c;一个仓位中最多可以做4个人。然后每次上一个人boardingCost钱#xff0c;但是我们转动1/4圈#xff0c;需要的成本是runningCost。然后给我们一个数组cu…通过万岁
题目就是一个摩天轮一共有4个仓位一个仓位中最多可以做4个人。然后每次上一个人boardingCost钱但是我们转动1/4圈需要的成本是runningCost。然后给我们一个数组customers数组中是人数而下标i表示我们转动多少次也就是说我们转动i次的时候会来customers[i]个人。如果坐满了那么多余的人只能等待下一批也就是i1的时候而且这是时候也会来customers[i1]个人。但是题目中有个地方有点迷惑人假设我们在某个位置决定停止营业则需要将上面所有的人都送下来才行。问我们第几次转动的盈利是最大的。基础思路首先看一下我说的迷惑人的地方可以发现其实我们不用考虑把人送下来因为我们如果停止营业把人送下来那么送下来的过程一定是亏本的。那么盈利最大值的肯定在此之前。然后再说一下我们的思路就是模拟这个过程就好了。首先我们需要遍历数组并且需要记录一下剩余的人数如果两者之和大于4则按照上4人的盈利标准来。否则按照现有的人数来计算。然后跟max的利润进行比较就好了。当我们遍历完数组之后我们还需要遍历剩余的人数将这些人安排好。在此过程中我们就可以找到最大值了。优化思路其实在遍历完数组以后针对剩余人数的计算可以进行优化的。如果上4人可以盈利的话那么我们将剩余人数/4*每次的盈利然后在针对不足4人的情况继续考虑。技巧模拟
java代码——基础
class Solution {public int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {int maxcurrProfit Integer.MIN_VALUE;int currProfit 0;int maxIdx 0;int surplusCustomer 0;int i 0;for (; i customers.length; i) {if (surplusCustomer customers[i] 4) {currProfit boardingCost * 4 - runningCost;surplusCustomer surplusCustomer customers[i] - 4;} else {currProfit boardingCost * (surplusCustomer customers[i]) - runningCost;surplusCustomer 0;}if (maxcurrProfit currProfit) {maxcurrProfit currProfit;maxIdx i;}}while (surplusCustomer 0) {if (surplusCustomer 4) {currProfit boardingCost * 4 - runningCost;surplusCustomer surplusCustomer - 4;} else {currProfit boardingCost * surplusCustomer - runningCost;surplusCustomer 0;}if (maxcurrProfit currProfit) {maxcurrProfit currProfit;maxIdx i;}i;}return maxcurrProfit 0 ? -1 : maxIdx 1;}
}java代码——优化
class Solution {public int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {int maxProfit Integer.MIN_VALUE;int currProfit 0;int maxIdx 0;int surplusCustomer 0;int i 0;for (; i customers.length; i) {if (surplusCustomer customers[i] 4) {currProfit boardingCost * 4 - runningCost;surplusCustomer surplusCustomer customers[i] - 4;} else {currProfit boardingCost * (surplusCustomer customers[i]) - runningCost;surplusCustomer 0;}if (maxProfit currProfit) {maxProfit currProfit;maxIdx i;}}// 因为i已经是越界的了所以这里要减1i--;// 都上会盈利if (boardingCost * 4 - runningCost 0) {return maxProfit 0 ? -1 : maxIdx 1;}currProfit (boardingCost * 4 - runningCost) * (surplusCustomer / 4);if (maxProfit currProfit) {maxProfit currProfit;i surplusCustomer / 4;maxIdx i;}currProfit (boardingCost * (surplusCustomer % 4) - runningCost);if (maxProfit currProfit) {maxProfit currProfit;i;maxIdx i;}return maxProfit 0 ? -1 : maxIdx 1;}
}总结题目不是特别难我最开始主要是被我说的迷惑的地方绕进去了。