【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/maximum-length-of-pair-chain/description/
题目描述
You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.
Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion.
Given a set of pairs, find the length longest chain which can be formed. You needn’t use up all the given pairs. You can select pairs in any order.
Example 1:
Input: [[1,2], [2,3], [3,4]]
Output: 2
Explanation: The longest chain is [1,2] -> [3,4]
Note:
- The number of given pairs will be in the range [1, 1000].
题目大意
给出了很多区间,找出能够成的连续增长的区间的个数。区间是闭区间,不能有任何重叠。
解题方法
贪心算法
题目给的提示是DP,我用Python的DP竟然超时了。其实我第一想法是贪心算法,在机试指南中的39页,题目是《今年暑假不AC》,原题是能收看到的最多的电视节目的数量。
这题的做法是按照每个区间的右边界进行排序,我们优先选择结束时间最早的区间,这样的贪心做法能保证最后得到的区间的数目是最多的。
Python代码:
class Solution(object):
def findLongestChain(self, pairs):
"""
:type pairs: List[List[int]]
:rtype: int
"""
pairs.sort(key=lambda x: x[1])
currTime, ans = float('-inf'), 0
for x, y in pairs:
if currTime < x:
currTime = y
ans += 1
return ans
日期
2018 年 4 月 5 日 —— 清明节假期开始,小长假真好~~
【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)的更多相关文章
- LN : leetcode 646 Maximum Length of Pair Chain
lc 646 Maximum Length of Pair Chain 646 Maximum Length of Pair Chain You are given n pairs of number ...
- LC 646. Maximum Length of Pair Chain
You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...
- 646. Maximum Length of Pair Chain(medium)
You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...
- 646. Maximum Length of Pair Chain 最长的链条长度
[抄题]: You are given n pairs of numbers. In every pair, the first number is always smaller than the s ...
- 646. Maximum Length of Pair Chain
You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- LeetCode Maximum Length of Pair Chain
原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n ...
- [LeetCode] Maximum Length of Pair Chain 链对的最大长度
You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...
- [Swift]LeetCode646. 最长数对链 | Maximum Length of Pair Chain
You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...
随机推荐
- SQL-用到的数据库语句总结
0.SELECT * FROM CHARACTER_SETS LIMIT 0,10 #从CHARACTER_SETS表中,从第1行开始,提取10行[包含第1行] 1.SELECT * FROM ...
- 7. Minimum Depth of Binary Tree-LeetCode
难度系数:easy /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- 如何优雅地将printf的打印保存在文件中?
我们都知道,一般使用printf的打印都会直接打印在终端,如果想要保存在文件里呢?我想你可能想到的是重定向.例如: $ program > result.txt 这样printf的输出就存储在r ...
- C#数据库连接方式【简版】
using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using ...
- three.js很好玩
能用鼠标拉着转. https://files.cnblogs.com/files/blogs/714801/%E7%A9%BA%E9%97%B4%E5%87%A0%E4%BD%95.7z var po ...
- 13. 搭建arm-linux-gcc交叉编译环境
1.下载工具并解压 下载路径 http://www.arm9.net/download.asp 将 arm-linux-gcc-4.5.1-v6-vfp-20120301.tgz 拷贝到 Linux ...
- APK 反编译以及遇到的问题
APK反编译: https://www.cnblogs.com/geeksongs/p/10864200.html 遇到的问题 https://www.jianshu.com/p/55bf5f688e ...
- 为什么CTR预估使用AUC来评估模型?
ctr预估简单的解释就是预测用户的点击item的概率.为什么一个回归的问题需要使用分类的方法来评估,这真是一个好问题,尝试从下面几个关键问题去回答. 1.ctr预估是特殊的回归问题 ctr预估的目标函 ...
- Linux基础命令---elinks文本浏览器
elinks elinks指令是一个纯文本格式的浏览器,支持颜色.表格.鼠标.菜单操作. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 ...
- Linux学习 - 关机重启退出命令
一.shutdown 1 功能 关机.重启操作 2 语法 shutdown [-chr] [时间选项] -h 关机 -r 重启 -c 取消前一个关机命令 二.halt.poweroff(关机) 三 ...