如何最大化您作为推销员的利润? Python动态编程
给出了两个列表,您必须找到最佳的城市以最大程度地提高收入。
假设您从第0天开始
第一个列表
。Money_day = [[1,1],[99,2]]
表示您在特定城市的特定日期将赚钱的金额。金钱[i] [j]代表我在城市jeg Money_day [0] [0]赚钱的金额,将是您在城市0的第0天赚钱的金额。第二个列表
的道路youse_travel = [[[0,1],[none,0]]
表示您需要从城市到城市需要的天数。可能的_travel [i] [j]将是从城市到城市jeg stoble_travel [0] [1]需要多长时间的时间,这意味着要从城市0到城市1。从城市到城市J
- 在某个城市销售将需要一整天(如果您在第2天出售,您将无法在第3天之前再次出售或再次出售
- 您可以访问同一城市再次出售
- 旅行也将需要X天(如果您在第2天开始旅行2天,您将在第4天之前做任何事情)
有以下代码,这些代码似乎对更大的列表不起作用。
def most_money(start, money_day, possible_travel):
tab = [-10] * len(money_day)
for days in possible_travel[start]:
tracker = 0
if possible_travel[start][days] is None:
continue
else:
tab[possible_travel[start][days]] = max(money_day[days][possible_travel[start][days]] + tracker, tab[possible_travel[start][days]])
tracker += tab[possible_travel[start][days]]
return memo
目前,我 我也不确定递归是否是一种更好的方法。
Given two list of lists, you have to find the best cities to travel to to maximise your earnings.
Assume you start at day 0
The first list
money_day = [[ 1, 1], [ 99, 2]]
represents the amount of money you will make on a specific day at a particular city. money[i][j] represents the amount of money made on day i at city j. e.g. money_day[0][0] will be the amount of money you make on day 0 at city 0 which is £1.The second list
possible_travel= [[0, 1], [None, 0]]
represents the amount of days you need to travel to from city to city. possible_travel[i][j] will be how long it will take to go from city i to city j. e.g. possible_travel[0][1] means it will take 1 day to travel from city 0 to city 1. None means that there is no road from city i to city j
- Selling at a certain city will take one whole day (if you sell on day 2 you cannot travel or sell again until day 3
- You can visit the same city to sell again
- Travelling will also take x days (if you start travel on day 2 for 2 days you cannot do anything until day 4)
Currently I have the following code that doesn't seem to work for larger lists.
def most_money(start, money_day, possible_travel):
tab = [-10] * len(money_day)
for days in possible_travel[start]:
tracker = 0
if possible_travel[start][days] is None:
continue
else:
tab[possible_travel[start][days]] = max(money_day[days][possible_travel[start][days]] + tracker, tab[possible_travel[start][days]])
tracker += tab[possible_travel[start][days]]
return memo
I'm having trouble breaking the problem up into sub-problems then combining, I'm also not sure if recursion is a better approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以像这样分解它:每天,您要么呆在同一个城市,这意味着您在那天的利润使第二天的利润重新开始在同一城市,要么前往另一个城市,这意味着您旅行时间后,在那个城市重新开始。
在代码中,将
Money_day
和可能的
作为简单性的全局常数,并假设前往另一个城市的旅行时间为0:You could break it down like this: On each day, you either stay in the same city, which means you make that day's profits there and start again in the same city on the next day, or you travel to another city, which means you start over in that city after the travel time.
In code, treating
money_day
andpossible_travel
as global constants for simplicity, and assuming that no travel time to another city is 0: