leetcode - 2830. Maximize the Profit as the Salesman

这篇具有很好参考价值的文章主要介绍了leetcode - 2830. Maximize the Profit as the Salesman。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Description

You are given an integer n representing the number of houses on a number line, numbered from 0 to n - 1.

Additionally, you are given a 2D integer array offers where offers[i] = [starti, endi, goldi], indicating that ith buyer wants to buy all the houses from starti to endi for goldi amount of gold.

As a salesman, your goal is to maximize your earnings by strategically selecting and selling houses to buyers.

Return the maximum amount of gold you can earn.

Note that different buyers can’t buy the same house, and some houses may remain unsold.

Example 1:

Input: n = 5, offers = [[0,0,1],[0,2,2],[1,3,2]]
Output: 3
Explanation: There are 5 houses numbered from 0 to 4 and there are 3 purchase offers.
We sell houses in the range [0,0] to 1st buyer for 1 gold and houses in the range [1,3] to 3rd buyer for 2 golds.
It can be proven that 3 is the maximum amount of gold we can achieve.

Example 2:

Input: n = 5, offers = [[0,0,1],[0,2,10],[1,3,2]]
Output: 10
Explanation: There are 5 houses numbered from 0 to 4 and there are 3 purchase offers.
We sell houses in the range [0,2] to 2nd buyer for 10 golds.
It can be proven that 10 is the maximum amount of gold we can achieve.

Constraints:

1 <= n <= 10^5
1 <= offers.length <= 10^5
offers[i].length == 3
0 <= starti <= endi <= n - 1
1 <= goldi <= 10^3

Solution

A classical weighted interval scheduling problem, see here for more details.

A better and easier solution with same problem, see 1235. Maximum Profit in Job Scheduling

Sort the offers by end, and then dp. dp[i] denotes the largest gold we can get when choosing offer[i]. Transformation equation is:
d p [ i ] = max ⁡ ( d p [ i − 1 ] , o f f e r s [ i ] [ 2 ] + d p [ j ] ) dp[i] = \max(dp[i - 1], offers[i][2] + dp[j]) dp[i]=max(dp[i1],offers[i][2]+dp[j])
where o f f e r [ j ] [ 1 ] < o f f e r [ i ] [ 0 ] offer[j][1] < offer[i][0] offer[j][1]<offer[i][0], that is, offer[j] is the largest index that we can choose offer[i]. Since we sort the end from small to large, use binary search here.

Time complexity: o ( n log ⁡ n ) o(n\log n) o(nlogn)
Space complexity: o ( n ) o(n) o(n)文章来源地址https://www.toymoban.com/news/detail-663724.html

Code

class Solution:
    def maximizeTheProfit(self, n: int, offers: List[List[int]]) -> int:
        offers.sort(key=lambda x: x[1])
        # p list contains the largest indexs that we could choose offer[i]
        p = [-1] * len(offers)
        ends = list(item[1] for item in offers)
        for i in range(1, len(p)):
            index = bisect.bisect_left(ends, offers[i][0])
            p[i] = index - 1
        dp = [0] * len(offers)
        dp[0] = offers[0][2]
        for i in range(1, len(dp)):
            dp[i] = max(dp[i - 1], offers[i][2] + (dp[p[i]] if p[i] >= 0 else 0))
        return max(dp)

到了这里,关于leetcode - 2830. Maximize the Profit as the Salesman的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Unity解决动画不可用:The AnimationClip ‘XXX‘ used by the Animation component ‘XXX‘ must be marked as Legacy.

    在Unity 2019.4.38.f1c1以上的版本,创建的创建Animation Clip无法使用,作为组件的默认Clip的话,那么游戏运行的时候这个Clip其实是播不出来的,而且Console会报一条 “The AnimationClip ‘XXX’ used by the Animation component ‘XXX’ must be marked as Legacy.” 的警告信息,以及一条 “Default clip co

    2023年04月08日
    浏览(43)
  • Run the Docker daemon as a non-root user (Rootless mode)

    rootless 简介 rootless模式是指以非root用户身份运行Docker守护程序和容器。那么为什么要有rootless mode呢?因为在root用户下安装启动的容器存在安全问题。存在的安全问题具体来说是容器内的root用户就是宿主机的root用户,容器内uid=1000的用户就是宿主机uid=1000的用户,docker的守护

    2024年02月10日
    浏览(37)
  • WARNING: Running pip as the ‘root‘ user can result in broken permissions

    解决方法如下,依次运行下面的两个代码,第一个是先验条件,第二个代码块是自己要实现的目标。

    2024年02月16日
    浏览(45)
  • ssh : The term ‘ssh‘ is not recognized as the name of a cmdlet, function, script file, or opera

    废了很长时间才解决这问腿。在PowerShell中输入ssh报: ssh : The term ‘ssh’ is not recognized as the name of a cmdlet, function, script file, or operable programssh:术语“ssh”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。 复盘一下是问题是因为装hightec,需要装java,配置java环境变量,

    2024年02月07日
    浏览(38)
  • YOLO7报错:indices should be either on cpu or on the same device as the indexed tensor (cpu)

    当我们的数据有部分在GPU上运行,有部分在CPU上运行时会报这个错, 一般有GPU的话都会选择在GPU上面跑模型,但要注意将其他定义的对象也放在GPU上面,否则应该默认是在CPU上面。 如图所示, x是从GPU中传过来的,但idx不是,idx是我们自己生成的,它默认放在CPU中,所以我们

    2024年02月12日
    浏览(47)
  • WARNING: Running pip as the ‘root‘ user can result in broken permissions and

    WARNING: Running pip as the \\\'root\\\' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv Linux pip安装报错解决方案 使用 pip安装、更新python库时,提示以“root”用户身份运行 pip 可能会导致权限损

    2024年02月12日
    浏览(47)
  • 【论文阅读——Profit Allocation for Federated Learning】

    由于更为严格的数据管理法规,如《通用数据保护条例》(GDPR),传统的机器学习服务生产模式正在转向联邦学习这一范式。联邦学习允许多个数据提供者在其本地保留数据的同时,协作训练一个共享模型。推动联邦学习实际应用的关键在于如何将联合模型产生的利润公平地

    2024年04月13日
    浏览(48)
  • mysql报错解决方式:1449 - The user specified as a definer (‘root‘@‘%‘) does not exist

    创建视图报错:1449-the user specified as a definer(ywsd\\\'0\\\"%\\\" does not exist 从一个数据库数据迁移到本地localhost 程序在调用到数据库的视图时报错,直接在数据库中打开视图时也报错,类似: mysql 1449 : The user specified as a definer (‘root’@‘%’) does not exist 经查询是权限问题,解决办法:

    2024年02月10日
    浏览(43)
  • 连接mysql:1449, “The user specified as a definer (‘mysql.infoschema‘@‘localhost‘) does not exist

           📝个人主页:五敷有你        🔥系列专栏:DeBug ⛺️稳中求进,晒太阳 前一天连接,一点问题没有,今早起来直接连接不上用命令行也是提示权限不够,啥也不能干。尝试过网上了的各种赋予权限的方法 某天,运行使用navcat打开服务器的数据库,报错: 主要

    2024年04月09日
    浏览(45)
  • 解决npm run build 打包出现XXXX.js as it exceeds the max of 500KB.

    问题描述: npm run build 时出现下面的问题: 在项目的根目录加粗样式下找到 .babelrc 文件或者babel.config.js文件,增加 “compact”: false ,如: 如果不存在则手动创建该文件,并填写内容如:

    2024年02月09日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包