您正在俄罗斯萨马拉(Samara)工作几天。每天都有每单位工作的新工资和每单位食物的新费用。工作1单位需要1单位能量,而吃1单位食物则要增加1单位能量。以下是您工作的一些规格:
- 您抵达时一无所有,但精力充沛。
- 您永远不会拥有比到达的能量更多的能量,而且它永远不会是负面的。
- 您每天可以做任何数量的工作(可能根本不做任何工作),仅受您的精力限制。
- 当您的能量为零时,您将无法工作。您每天可以吃任何数量的食物(可能根本没有任何食物),这取决于您所拥有的资金。
- 当您的钱为零时,您将无法吃饭。您可以在一天结束时吃东西,也不能在吃完饭后重返工作岗位。
- 您第二天可以重新上班。
您的真实目标是带回尽可能多的钱。计算您可以带回家的最大金额。
例子:
Input: N = 3, Earning=[1, 2, 4], Cost[]=[1, 3, 6], E = 5
Output: 20
Explanation:
Day 1: 1 unit work is worth 1, and 1 unit food costs 1. There is no financial incentive to go to work this day.
Day 2: 1 unit work earns 2, and 1 unit food costs 3. Thus, you spend more to eat then the total earning so there is no financial incentive to go to work on this day.
Day 3: You earn 4 units per unit of work. The cost of food is irrelevant this day, as you are leaving for the home straight from work.
You spend all of your energy working, the pay is 5 × 4 = 20 units of money and go home without buying dinner.
Input: N=2, Earning=[1, 2], Cost=[1, 4], E=5
Output: Total Profit = 0 + 10 = 10
Explanation:
First Day: Skip
Second Day: 5*2=10
方法:方法是遍历给定的收入数组和成本数组,并计算每天的净利润。步骤如下:
- 对于收入[]和成本[]中的每个元素,请将收入[i]与成本[i]进行比较。
- 如果收入[i]小于或等于cost [i] ,则当费用成本大于收入时,请跳过当天的工作。因此,根本不赚钱。
- 如果Earning [i]大于cost [i] ,请通过将当日的收入乘以总能量单位来计算总收入,然后减去当日食品成本和能量单位的乘积来计算利润。那天。
- 如果有最后一个工作日,则将当日的收入乘以总能量单位来计算总收入,这将是您当日的利润,因为不需要更多的工作能量。
- 完成上述步骤后,打印总利润。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that calculates the profit
// with the earning and cost of expenses
int calculateProfit(int n, int* earnings,
int* cost, int e)
{
// To store the total Profit
int profit = 0;
// Loop for n number of days
for (int i = 0; i < n; i++) {
int earning_per_day = 0;
int daily_spent_food = 0;
// If last day, no need to buy food
if (i == (n - 1)) {
earning_per_day = earnings[i] * e;
profit = profit + earning_per_day;
break;
}
// Else buy food to gain
// energy for next day
if (cost[i] < earnings[i]) {
// Update earning per day
earning_per_day = earnings[i] * e;
daily_spent_food = cost[i] * e;
// Update profit with daily spent
profit = profit + earning_per_day
- daily_spent_food;
}
}
// Print the profit
cout << profit << endl;
}
// Driver Code
int main()
{
// Given days
int n = 4;
// Given earnings
int earnings[] = { 1, 8, 6, 7 };
// Given cost
int cost[] = { 1, 3, 4, 1 };
// Given energy e
int e = 5;
// Function Call
calculateProfit(n, earnings, cost, e);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function that calculates the profit
// with the earning and cost of expenses
static void calculateProfit(int n, int []earnings,
int []cost, int e)
{
// To store the total Profit
int profit = 0;
// Loop for n number of days
for(int i = 0; i < n; i++)
{
int earning_per_day = 0;
int daily_spent_food = 0;
// If last day, no need to buy food
if (i == (n - 1))
{
earning_per_day = earnings[i] * e;
profit = profit + earning_per_day;
break;
}
// Else buy food to gain
// energy for next day
if (cost[i] < earnings[i])
{
// Update earning per day
earning_per_day = earnings[i] * e;
daily_spent_food = cost[i] * e;
// Update profit with daily spent
profit = profit + earning_per_day -
daily_spent_food;
}
}
// Print the profit
System.out.print(profit + "\n");
}
// Driver Code
public static void main(String[] args)
{
// Given days
int n = 4;
// Given earnings
int earnings[] = { 1, 8, 6, 7 };
// Given cost
int cost[] = { 1, 3, 4, 1 };
// Given energy e
int e = 5;
// Function call
calculateProfit(n, earnings, cost, e);
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program for the above approach
# Function that calculates the profit
# with the earning and cost of expenses
def calculateProfit(n, earnings, cost, e):
# To store the total Profit
profit = 0;
# Loop for n number of days
for i in range(n):
earning_per_day = 0;
daily_spent_food = 0;
# If last day, no need to buy food
if (i == (n - 1)):
earning_per_day = earnings[i] * e;
profit = profit + earning_per_day;
break;
# Else buy food to gain
# energy for next day
if (cost[i] < earnings[i]):
# Update earning per day
earning_per_day = earnings[i] * e;
daily_spent_food = cost[i] * e;
# Update profit with daily spent
profit = (profit + earning_per_day -
daily_spent_food);
# Print the profit
print(profit);
# Driver Code
if __name__ == '__main__':
# Given days
n = 4;
# Given earnings
earnings = [ 1, 8, 6, 7 ];
# Given cost
cost = [ 1, 3, 4, 1 ];
# Given energy e
e = 5;
# Function call
calculateProfit(n, earnings, cost, e);
# This code is contributed by PrinciRaj1992
C#
// C# program for the above approach
using System;
class GFG{
// Function that calculates the profit
// with the earning and cost of expenses
static void calculateProfit(int n, int []earnings,
int []cost, int e)
{
// To store the total Profit
int profit = 0;
// Loop for n number of days
for(int i = 0; i < n; i++)
{
int earning_per_day = 0;
int daily_spent_food = 0;
// If last day, no need to buy food
if (i == (n - 1))
{
earning_per_day = earnings[i] * e;
profit = profit + earning_per_day;
break;
}
// Else buy food to gain
// energy for next day
if (cost[i] < earnings[i])
{
// Update earning per day
earning_per_day = earnings[i] * e;
daily_spent_food = cost[i] * e;
// Update profit with daily spent
profit = profit + earning_per_day -
daily_spent_food;
}
}
// Print the profit
Console.Write(profit + "\n");
}
// Driver Code
public static void Main(String[] args)
{
// Given days
int n = 4;
// Given earnings
int []earnings = { 1, 8, 6, 7 };
// Given cost
int []cost = { 1, 3, 4, 1 };
// Given energy e
int e = 5;
// Function call
calculateProfit(n, earnings, cost, e);
}
}
// This code is contributed by Rajput-Ji
70
时间复杂度: O(N),其中N是天数
辅助空间: O(1)