博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode-630-Course Schedule III]
阅读量:5954 次
发布时间:2019-06-19

本文共 1991 字,大约阅读时间需要 6 分钟。

There are n different online courses numbered from 1 to n. Each course has some duration(course length) t and closed on dth day. A course should be taken continuously for t days and must be finished before or on the dth day. You will start at the 1st day.

Given n online courses represented by pairs (t,d), your task is to find the maximal number of courses that can be taken.

Example:

Input: [[100, 200], [200, 1300], [1000, 1250], [2000, 3200]]Output: 3Explanation: There're totally 4 courses, but you can take 3 courses at most:First, take the 1st course, it costs 100 days so you will finish it on the 100th day, and ready to take the next course on the 101st day.Second, take the 3rd course, it costs 1000 days so you will finish it on the 1100th day, and ready to take the next course on the 1101st day. Third, take the 2nd course, it costs 200 days so you will finish it on the 1300th day. The 4th course cannot be taken now, since you will finish it on the 3300th day, which exceeds the closed date.

 

Note:

  1. The integer 1 <= d, t, n <= 10,000.
  2. You can't take two courses simultaneously.

思路:

代码参考自:

struct cmp {    inline bool operator() (const vector
& c1, const vector
& c2) { return c1[1] < c2[1]; }};class Solution {public: int scheduleCourse(vector
>& courses) { sort(courses.begin(), courses.end(), cmp()); vector
best(1, 0); for (const auto& course : courses) { int t = course[0], d = course[1]; if (t > d) continue; // impossible to even take this course int m = best.size(); if (best[m-1]+t<=d) best.push_back(best[m-1]+t); for (int i = m-1; i>0; --i) { if (best[i-1] + t <= d) { best[i] = min(best[i], best[i-1] + t); } } } return best.size() - 1; }};

 

转载于:https://www.cnblogs.com/hellowooorld/p/7076528.html

你可能感兴趣的文章
设置Eclipse编码方式
查看>>
分布式系统唯一ID生成方案汇总【转】
查看>>
并查集hdu1232
查看>>
Mysql 监视工具
查看>>
从前后端分离到GraphQL,携程如何用Node实现?\n
查看>>
Linux Namespace系列(09):利用Namespace创建一个简单可用的容器
查看>>
博客搬家了
查看>>
Python中使用ElementTree解析xml
查看>>
jquery 操作iframe、frameset
查看>>
解决vim中不能使用小键盘
查看>>
jenkins权限管理,实现不同用户组显示对应视图views中不同的jobs
查看>>
我的友情链接
查看>>
CentOS定时同步系统时间
查看>>
批量删除用户--Shell脚本
查看>>
如何辨别android开发包的安全性
查看>>
Eclipse Java @Override 报错
查看>>
知道双字节码, 如何获取汉字 - 回复 "pinezhou" 的问题
查看>>
linux中cacti和nagios整合
查看>>
Parallels Desktop12推出 新增Parallels Toolbox
查看>>
Python高效编程技巧
查看>>