中级农民
- 积分
- 104
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-12-11
- 最后登录
- 1970-1-1
|
You are working in Samara, Russia for a few days, Each day has a new pay per unit of work and a new cost per unit of food. Working 1 unit costs 1 unit of energy, and eating 1 unit of food adds 1 unit of energy. Here are some specifications of your employment:
+You arrive with no money, but with energy. You can never have more energy than you arrive with, and it can never be negative.
+You can do any amount of work every day (possibly not do any work at all), limited only by your energy. You cannot work when your energy is zero.
+You can eat any amount of food every day (possibly not have any food at all), limited by the money you have. You cannot eat when the money you have is zero.
+You can eat food at the end of the day, and cannot return to work after eating. You can return to work on the next day. Your true goal is to return home with as much money as possible. Compute the maximum amount of money you can take home.
For example, consider a 3 day stay where pay per unit work for each day is as follows: earning=[1, 2, 4]. The cost of food is cost=[1, 3, 6]. You start with e=5 units of energy.
*First day: 1 unit work is worth 1, and 1 unit food costs 1. There is no financial incentive to go to work this day.
*Second day: 1 unit work earns 2, and 1 unit food costs 3, Thus you spend more to eat than total earning so there is no financial incentive to go to work on this day.
*Third day: 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, collect your pay: 5 x 4 = 20 units of money and go home without buying dinner.
Function Description Complete the function calculateProfit in the editor below. The function must return an integer that represents the maximum earnings that can be taken home at the end of your stay.
Python code:
n = 3
earning = [7, 2, 4]
costing = [7, 3, 6]
e = 5
def maxProfit(n , earning, costing, e):
dp = [[0]*(2*n - 1) for _ in range(e + 1)] #dp is (e+1)*(2*n - 1), odd column represents after work, even column represents after eat
for j in range(2*n - 1): #j represents column
for I in range(e + 1): #I represents row, I = 0 represents energy 5
if j == 0: #the first day work
dp[i][j] = i*earning[j]
elif j % 2 != 0: #the day eat
potential_max = [] #different after work energy can go to same energy after eat
for k in range(I, e + 1):
if dp[k][j - 1] >= (k - i)*costing[j//2]: #make sure having enough money to buy food
potential_max.append(dp[k][j - 1] - (k - i)*costing[j//2])
dp[i][j] = max(potential_max)
elif j % 2 == 0: #the day work
potential_max = [] #different after eat energy can go to the same after work energy
for k in range(e + 1 - i):
potential_max.append(dp[k][j - 1] + (e - k - i)*costing[j//2])
dp[e - i][j] = max(potential_max)
return dp[-1][-1] |
|