leetcode/1599-240101-pass/main.cpp
2024-01-01 12:32:38 +08:00

77 lines
2.5 KiB
C++

#include<stdcpp.h>
using namespace std;
class Solution {
public:
int minOperationsMaxProfit(vector<int>& customers, int boardingCost, int runningCost) {
if(boardingCost * 4 < runningCost) return -1;
int waiting = 0;
int profit = 0;
int max_profit= -1;
int wheel = 0;
int ans = wheel;
for(int i = 0 ; i < customers.size();i++){
waiting += customers[i];
if(waiting <= 4){
profit += waiting * boardingCost - runningCost;
waiting = 0;
}else{
profit += 4 * boardingCost - runningCost;
waiting -= 4;
}
cout<<waiting<<' '<<profit<<endl;
wheel++;
if(max_profit < profit){
ans = wheel;
max_profit = profit;
}
}
profit += waiting / 4 * ( 4 * boardingCost - runningCost);
wheel += waiting / 4;
if(max_profit < profit){
ans = wheel;
max_profit = profit;
}
cout<<wheel<<' '<<profit<<endl;
profit += waiting % 4 * boardingCost - runningCost;
wheel++;
if(max_profit < profit){
ans = wheel;
max_profit = profit;
}
cout<<wheel<<' '<<profit<<endl;
// while(waiting){
// if(waiting <= 4){
// profit += waiting * boardingCost - runningCost;
// waiting = 0;
// }else{
// profit += 4 * boardingCost - runningCost;
// waiting -= 4;
// }
// wheel++;
// cout<<waiting<<' '<<profit<<endl;
// if(max_profit < profit){
// ans = wheel;
// max_profit = profit;
// }
// }
if(max_profit <= 0 ) return -1;
return ans;
}
};
int main(){
Solution sol;
vector<int> customer_ex1 = {8,3};
int boardingCost = 5, runningCost = 6;
cout<<sol.minOperationsMaxProfit(customer_ex1,boardingCost,runningCost)<<endl;
vector<int> customer_ex2 = {10,9,6};
boardingCost = 6, runningCost = 4;
cout<<sol.minOperationsMaxProfit(customer_ex2,boardingCost,runningCost)<<endl;
vector<int> customer_ex3 = {3,4,0,5,1};
boardingCost = 1, runningCost = 92;
cout<<sol.minOperationsMaxProfit(customer_ex3,boardingCost,runningCost)<<endl;
vector<int> customer_ex4 = {2};
boardingCost = 2, runningCost = 4;
cout<<sol.minOperationsMaxProfit(customer_ex4,boardingCost,runningCost)<<endl;
return 0;
}