题目如下:

We have n jobs, where every job is scheduled to be done from startTime[i] to endTime[i], obtaining a profit of profit[i].

You're given the startTime , endTime and profit arrays, you need to output the maximum profit you can take such that there are no 2 jobs in the subset with overlapping time range.

If you choose a job that ends at time X you will be able to start another job that starts at time X.

Example 1:

Input: startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70]
Output: 120
Explanation: The subset chosen is the first and fourth job.
Time range [1-3]+[3-6] , we get profit of 120 = 50 + 70.

Example 2:

Input: startTime = [1,2,3,4,6], endTime = [3,5,10,6,9], profit = [20,20,100,70,60]
Output: 150
Explanation: The subset chosen is the first, fourth and fifth job.
Profit obtained 150 = 20 + 70 + 60.

Example 3:

Input: startTime = [1,1,1], endTime = [2,3,4], profit = [5,6,4]
Output: 6

Constraints:

  • 1 <= startTime.length == endTime.length == profit.length <= 5 * 10^4
  • 1 <= startTime[i] < endTime[i] <= 10^9
  • 1 <= profit[i] <= 10^4

解题思路:很容易看出应该用动态规划。假设dp[i]为第i个工作在所有选择的工作序列中排在最后一个时可以获得的最大利润,那么有dp[i] = max(dp[i],dp[j] + profit[i] )(i.startTime >= j.endTime)。但是这样时间复杂度是O(n^2),这无法被接受。继续寻找规律,对于dp[i]来说,我们要找的是所有endTime小于i.startTime的元素,所有我们可以按endTime升序排序,存储每个endTime可以获得的利润的最大值到一个列表中,这样的话,对于startTime来说,只要通过二分查找找到在endTime在列表中的位置,即可求得对应的最大值。

代码如下:

class Solution(object):
def jobScheduling(self, startTime, endTime, profit):
"""
:type startTime: List[int]
:type endTime: List[int]
:type profit: List[int]
:rtype: int
"""
import bisect
item_list = []
for (start,end,pro) in zip(startTime,endTime,profit):
item_list.append((start,end,pro))
def cmpf(v1,v2):
if v1[1] != v2[1]:return v1[1] - v2[1]
return v1[0] - v2[0]
item_list.sort(cmp=cmpf)
dp = [0] * len(item_list)
dp[0] = item_list[0][2] end_time_order = []
for i in range(len(item_list)):
end_time_order.append(item_list[i][1]) max_val_list = [dp[0]]
max_val = dp[0]
for i in range(1,len(item_list)):
dp[i] = item_list[i][2] start = item_list[i][0] #left = bisect.bisect_left(end_time_order,start)
right = bisect.bisect_right(end_time_order,start)
if right == len(max_val_list) and end_time_order[right-1] <= start:
dp[i] = max_val_list[-1] + item_list[i][2]
elif right < len(max_val_list) and right > 0:
dp[i] = max_val_list[right-1] + item_list[i][2]
max_val = max(max_val,dp[i])
max_val_list.append(max_val)
#dp[i] = max(dp[i],dp[i-1])
#print dp
#print max_val_list
return max(dp)

【leetcode】1235. Maximum Profit in Job Scheduling的更多相关文章

  1. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  2. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  3. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  4. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  5. 【Leetcode】164. Maximum Gap 【基数排序】

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  6. 【leetcode】1255. Maximum Score Words Formed by Letters

    题目如下: Given a list of words, list of  single letters (might be repeating) and score of every charact ...

  7. 【leetcode】1189. Maximum Number of Balloons

    题目如下: Given a string text, you want to use the characters of text to form as many instances of the w ...

  8. 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  9. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

随机推荐

  1. AKKA文档2.2(java)——术语,概念

    原文:http://doc.akka.io/docs/akka/2.3.6/general/terminology.html 译者:吴京润 本章我们试图建立一个通用的术语列表,用来定义有关并发和分布式 ...

  2. Java十大bug之——包冲突

    找bug就像破案,有的bug简单,有的bug复杂,还有的bug隐藏的令人难以发现. 一个逻辑上看起来一切都正常,结果确有问题,且怎么分析都感觉自己写的没问题的情况——包冲突 遇到这个bug最开始没有任 ...

  3. 【Spring 源码】ApplicationContext源码

    ApplicationConetxt体系

  4. kafka的错误日志log监控

    例如:需要监控下面的日志中的error 日志文件是这个 /data1/confluent-5.2.2/logs/connect/kafka-connect.log 1/ 参考上一篇安装zabbix_a ...

  5. C++学习 之 类中的特殊函数和this指针(笔记)

    1.构造函数 构造函数是一种特殊的函数,它在对象被创建时被调用,与类同名无返回类型,可以被重载.构造函数的可以在类内实现也可以在类外实现. 构造函数的声明类似于下面的代码: class Human { ...

  6. c++—简单的密码本实现

    主要实现功能有增删改查数据,本地读取保存. 数据存储设计 data.h data.cpp #pragma once #define 添加账户 1 #define 删除账户 2 #define 修改账户 ...

  7. 多个div排列在同一行而不换行

    有时候,我们可能会产生多个div标签横向排列而不换行的需求,具体有以下几种实现方法: 1. 同级div设置display:inline-block,父级div强制不换行例如: <html> ...

  8. Python和其他编程语言

    Python和其他编程语言 一.Python介绍 Python的创始人为吉多·范罗苏姆(Guido van Rossum),如下图,少数几个不秃头的语言创始人.1989年的圣诞节期间,Guido为了打 ...

  9. docopt 安装及基本应用

    什么是 docopt docopt是一种python 编写的命令行执行脚本的交互语言. 它是一种语言! 它是一种语言! 它是一种语言! 使用这种语言可以在自己的脚本中,添加一些规则限制,这样脚本在执行 ...

  10. 第五章 表表达式 T-SQL语言基础

    表表达式 Microsoft SQL Server支持4种类型的表表达式:派生表(derived table),公用表表达式(CTE,common table expression),视图,以及内联表 ...