当前位置: 首页 > news >正文

各大免费推广网站浙江百度代理公司

各大免费推广网站,浙江百度代理公司,选择锦州网站建设,app开发费用大概多少通过万岁!!! 题目:就是一个摩天轮,一共有4个仓位,一个仓位中最多可以做4个人。然后每次上一个人boardingCost钱,但是我们转动1/4圈,需要的成本是runningCost。然后给我们一个数组cu…

通过万岁!!!

  • 题目:就是一个摩天轮,一共有4个仓位,一个仓位中最多可以做4个人。然后每次上一个人boardingCost钱,但是我们转动1/4圈,需要的成本是runningCost。然后给我们一个数组customers,数组中是人数,而下标i表示我们转动多少次,也就是说我们转动i次的时候,会来customers[i]个人。如果坐满了,那么多余的人只能等待下一批,也就是i+1的时候,而且这是时候也会来customers[i+1]个人。但是题目中有个地方有点迷惑人,假设我们在某个位置决定停止营业,则需要将上面所有的人都送下来才行。问我们第几次转动的盈利是最大的。
  • 基础思路:首先看一下我说的迷惑人的地方,可以发现,其实我们不用考虑把人送下来,因为我们如果停止营业,把人送下来,那么送下来的过程一定是亏本的。那么盈利最大值的肯定在此之前。然后再说一下我们的思路,就是模拟这个过程就好了。首先我们需要遍历数组,并且需要记录一下剩余的人数,如果两者之和大于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;}
}
  • 总结:题目不是特别难,我最开始主要是被我说的迷惑的地方绕进去了。
http://www.yayakq.cn/news/742540/

相关文章:

  • 做网站为什么要去工厂现代网站开发技术
  • 泸州作网站建设联系电话瓯北网站制作
  • 网站建设合同合同期限谷歌seo搜索优化
  • 全国建造师查询网站智慧团建登录平台入口
  • 个人 网站 备案专业的营销网站建设公司排名
  • 西安营销型网站制作以百度云做网站空间
  • 新乡网站建设哪家公司好wordpress段间距
  • 网站是否需要备案佛山市网站开发
  • 过年做那个网站致富诸暨做网站公司
  • 外贸网站建设上海智慧团建系统官方网站
  • 上海行业门户网站建设技术建站之星安装模板失败
  • 带注册登录的网站模板吉林省吉林市龙潭区
  • 杭州网站建设V芯ee8888e用第三方做网站
  • 仿淘宝网站源码 php网站描述在关键字前可以吗
  • 建设营销型网站多少钱大连建设培训网
  • 建筑营销型网站接网站建设单子注意事项
  • 公司建设网站费用025网站建设
  • 网站风格趋势wordpress 主页面错乱
  • 网站建设仟金手指六六14网络销售推广
  • 微信手机网站制作广州穗科建设管理有限公司网站
  • 做网站百度一下app开发搭建
  • 专业定制网站苏州微网站建设公司
  • 上海网站建设公司服务有哪些wordpress limit
  • 电子商务网站的建设与规划公司官网怎么维护
  • 网站制作的前期主要是做好什么工作网站建设 开票税率
  • 深圳网站设计收费如何注册新账号
  • 成都企业网站维护阿里服务器怎么做网站服务器吗
  • 专业推广网站焦作网站建设价格
  • 汕头网站制作找哪家设计网站的软件
  • 网站被黑客攻击怎么办泉州网站建设解决方案