如何最大化您作为推销员的利润? Python动态编程

发布于 2025-01-26 07:27:54 字数 1051 浏览 2 评论 0原文

给出了两个列表,您必须找到最佳的城市以最大程度地提高收入。

假设您从第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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥︴琐丶欲为 2025-02-02 07:27:54

您可以像这样分解它:每天,您要么呆在同一个城市,这意味着您在那天的利润使第二天的利润重新开始在同一城市,要么前往另一个城市,这意味着您旅行时间后,在那个城市重新开始。

在代码中,将Money_day可能的作为简单性的全局常数,并假设前往另一个城市的旅行时间为0:

last_day = len(money_day) - 1
cities = set(range(len(possible_travel)))

def most_money(day, city):
    """Return how much money is left to be made, 
    if the current state is given by day, city."""
    if day > last_day:
        return 0
    if day == last_day:
        return money_day[day][city]  
    return max([money_day[day][city] + most_money(day + 1, city)] +
               [most_money(day + possible_travel[city][next_city], next_city) 
                for next_city in cities - {city}
                if possible_travel[city][next_city] is not None])

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 and possible_travel as global constants for simplicity, and assuming that no travel time to another city is 0:

last_day = len(money_day) - 1
cities = set(range(len(possible_travel)))

def most_money(day, city):
    """Return how much money is left to be made, 
    if the current state is given by day, city."""
    if day > last_day:
        return 0
    if day == last_day:
        return money_day[day][city]  
    return max([money_day[day][city] + most_money(day + 1, city)] +
               [most_money(day + possible_travel[city][next_city], next_city) 
                for next_city in cities - {city}
                if possible_travel[city][next_city] is not None])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文