【leetcode】1024. Video Stitching
题目如下:
You are given a series of video clips from a sporting event that lasted
T
seconds. These video clips can be overlapping with each other and have varied lengths.Each video clip
clips[i]
is an interval: it starts at timeclips[i][0]
and ends at timeclips[i][1]
. We can cut these clips into segments freely: for example, a clip[0, 7]
can be cut into segments[0, 1] + [1, 3] + [3, 7]
.Return the minimum number of clips needed so that we can cut the clips into segments that cover the entire sporting event (
[0, T]
). If the task is impossible, return-1
.Example 1:
Input: clips = [[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]], T = 10
Output: 3
Explanation:
We take the clips [0,2], [8,10], [1,9]; a total of 3 clips.
Then, we can reconstruct the sporting event as follows:
We cut [1,9] into segments [1,2] + [2,8] + [8,9].
Now we have segments [0,2] + [2,8] + [8,10] which cover the sporting event [0, 10].Example 2:
Input: clips = [[0,1],[1,2]], T = 5
Output: -1
Explanation:
We can't cover [0,5] with only [0,1] and [0,2].Example 3:
Input: clips = [[0,1],[6,8],[0,2],[5,6],[0,4],[0,3],[6,7],[1,3],[4,7],[1,4],[2,5],[2,6],[3,4],[4,5],[5,7],[6,9]], T = 9
Output: 3
Explanation:
We can take clips [0,4], [4,7], and [6,9].Example 4:
Input: clips = [[0,4],[2,8]], T = 5
Output: 2
Explanation:
Notice you can have extra video after the event ends.Note:
1 <= clips.length <= 100
0 <= clips[i][0], clips[i][1] <= 100
0 <= T <= 100
解题思路:由于选中的clip之间是允许有重叠的,因此尽量选择较长的clip,可以采用贪心算法。首先对clips按照start升序,end降序的方法排序。排序完成后的第一个元素是必选的,因为其start最小(并列)同时end最大。接下来再寻找和第一个元素有交集的区间,找出end最大的那个组成新的start,end区间。然后继续寻找end最大的区间直至clips遍历完成。最后判断start,end是否包含0,T区间即可。
代码如下:
class Solution(object):
def videoStitching(self, clips, T):
"""
:type clips: List[List[int]]
:type T: int
:rtype: int
"""
def cmpf(v1,v2):
if v1[0] != v2[0]:
return v1[0] - v2[0]
return v2[1] - v1[1]
clips.sort(cmp=cmpf)
#print clips start,end = clips[0][0],clips[0][1]
clips.pop(0)
res = 1
flag = True
while flag and len(clips) > 0 and end < T :
maxEnd = end
flag = False
while len(clips) > 0:
if end < clips[0][0]:
break
flag = True
maxEnd = max(maxEnd,clips.pop(0)[1])
res += 1
end = maxEnd
return res if (start == 0 and end >= T) else -1
【leetcode】1024. Video Stitching的更多相关文章
- 【LeetCode】1024. Video Stitching 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心 日期 题目地址:https://leetcod ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- 攻防世界 | hello_pwn
看样子是要让我们通过read(0, &unk_601068, 0x10uLL),读入 unk_601068 将 dword_60106C 覆盖 6c-68=4,所以: from pwn imp ...
- Node对象的一些方法
Node对象是什么提供了 DOM的标准规范提供了Node对象,该对象主要提供了解析DOM节点树结构的属性和方法,DOM树结构主要是依靠节点进行解析,称为DOM节点树结构.Node对象是解析DOM节点树 ...
- What is httpcontext
https://docs.microsoft.com/en-us/dotnet/api/system.web.httpcontext?view=netframework-4.8 Encapsulate ...
- Angular.js路由 简单小案例
代码案例: <html> <head> <meta charset="utf-8"> <title>AngularJS 路由实例&l ...
- C. Roads in Berland
题目链接: http://codeforces.com/problemset/problem/25/C 题意: 给一个最初的所有点与点之间的最短距离的矩阵.然后向图里加边,原有的边不变,问加边后的各个 ...
- HDU 2063 过山车 (匈牙利算法)
题目链接:HDU 2063 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩 ...
- Js类的静态方法与实例方法区分以及jQuery如何拓展两种方法
上学时C#老师讲到对象有两类方法,静态方法(Static)和实例方法(非Static),当时不理解静态是为何意,只是强记. 后来从事前端工作,一直在对类(即对象,Js中严格来说没有类的定义,虽众所周知 ...
- MySQL-极恶安装
1.官网下载地址:https://dev.mysql.com/downloads/mysql/ 2.安装包下载后解压,并创建my.ini配置文件 内容如下,注意两个第三个#:MySQL的安装目录,第四 ...
- Linux mysql 乱码
http://www.pc6.com/infoview/Article_63586.html http://itindex.net/detail/41748-linux-mysql-5.5 http: ...
- Using Keyboard Navigation
http://technet.microsoft.com/en-us/library/cc939835.aspx