1.20 LeetCode总结(基本算法)_模拟类

编程总结

每每刷完一道题后,其思想和精妙之处没有地方记录,本篇博客用以记录刷题过程中的遇到的算法和技巧

1599. 经营摩天轮的最大利润

在这里插入图片描述
在这里插入图片描述

int maxi(int x, int y)
{
	return x > y ? x : y;
}
int minOperationsMaxProfit(int* customers, int customersSize, int boardingCost, int runningCost) 
{
	if (boardingCost * 4 <= runningCost) {
		return -1;
	}
	int cur    = 0; // 当前时间等待人数(登轮前)
	int profit = 0; //(当前总利润)
	int max    = 0; //(最大利润持续更新)
	int ans    = 0; //(返回的最大转动次数)
	//有人来的时间段先根据已有时间线按部就班进行转动
	for (int i = 0; i < customersSize; i++) {
		cur += customers[i];
		if (cur >= 4) { // 大于等于四个就四个一批处理
			profit += 4 * boardingCost - runningCost;
			cur = cur - 4;
		}
		else {          // 小于4则清空人数
			profit += cur * boardingCost - runningCost;
			cur = 0;
		}
		if (profit > max) {
			ans = i + 1; // 本次操作下来看利润能否增长,是则更新答案
		}
		max = maxi(profit, max);
	}
	//没有人来了以后,处理剩下等待的人
	int c = 0;//记录转动次数
	while (cur >= 4)//四个一批处理获取最大利润
	{
		profit += 4 * boardingCost - runningCost;
		cur = cur - 4;
		c++;
		if (profit > max)//更新结果
			ans = customersSize + c;
		max = maxi(profit, max);//更新最大利润
	}
	if (cur < 4 && cur > 0 && cur * boardingCost > runningCost) // 处理落单的1-3人,前提是能使利润正增长
	{
		profit += cur * boardingCost - runningCost;
		cur = 0;
		if (profit > max)
			ans = customersSize + c + 1;
		max = maxi(profit, max);
	}
	if (ans == 0)//没有使利润>0的情况,返回-1
		return -1;
	return ans;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值