【leetcode】1235. Maximum Profit in Job Scheduling
题目如下:
We have
n
jobs, where every job is scheduled to be done fromstartTime[i]
toendTime[i]
, obtaining a profit ofprofit[i]
.You're given the
startTime
,endTime
andprofit
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 timeX
.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: 6Constraints:
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)
原文地址:https://www.cnblogs.com/seyjs/p/11730827.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 杭电60题--part 1 HDU1003 Max Sum(DP 动态规划)
- js的的的图片随屏幕滚动而滑入滑出的效果(万 万。。。字长文)
- Codeforce-CodeCraft-20 (Div. 2)-B. String Modification (找规律+模拟)
- Codeforce-CodeCraft-20 (Div. 2)-A. Grade Allocation
- Cypress系列(69)- route() 命令详解
- Codeforce-Ozon Tech Challenge 2020-D. Kuroni and the Celebration(交互题+DFS)
- Codeforce 263D Cycle in Graph 搜索 图论 哈密尔顿环
- codeforce 266c Below the Diagonal 矩阵变换 (思维题)
- Codeforce-Ozon Tech Challenge 2020-C. Kuroni and Impossible Calculation(鸽笼原理)
- 探索ParNew和CMS垃圾回收器
- Codeforce-Ozon Tech Challenge 2020-B. Kuroni and Simple Strings(贪心)
- 第K短路+严格第K短路
- PostgreSQL异常重启postmaster.pid处理
- Codeforce-Ozon Tech Challenge 2020-A. Kuroni and the Gifts
- 洛谷P3360偷天换日(树形DP)