题目如下:

解题思路:贪心算法。首先把intervals按第二个元素从小到大排序,然后遍历intervals,如果集合S和intervals[i]没有交集,那么把intervals[i]的最大值和次大值加入集合S;如果交集只有一个元素,则把最大值加入S。

代码如下:

class Solution(object):
def intersectionSizeTwo(self, intervals):
"""
:type intervals: List[List[int]]
:rtype: int
"""
import bisect
def cmpf(l1,l2):
if l1[1] != l2[1]:
return l1[1] - l2[1]
return l2[0] - l1[0]
intervals.sort(cmp=cmpf)
res = None
for low,high in intervals:
if low == 29 and high == 37:
pass
if res == None:
res = [high-1,high]
elif res[-1] < low:
#res[1] = low + 1
res.append(high-1)
res.append(high)
elif res[-1] == low:
res.append(high)
elif bisect.bisect_right(res,high) - bisect.bisect_left(res,low) == 1:
res.append(high)
#print res
#print intervals
#print res
return len(res)

最新文章

  1. ecshop不同样式文章页调用不同文章模板
  2. selenium+testng+ant+jenkins 手记
  3. Zend Guard Loader/Zend Loader是干什么的
  4. linux挂载共享文件夹
  5. powerdesigner中怎么给一主键设为自增型auto increme
  6. JS常用的设计模式(14)—— 备忘录模式
  7. lighttpd 介绍及安装
  8. freemaker分页备忘
  9. LabVIEW系列——合并错误(VI)的用法
  10. werkzeug源码阅读笔记(二) 下
  11. python3 scrapy+Crontab部署过程
  12. luogu2402 奶牛隐藏
  13. Struts2中的struts.multipart.saveDir的配置
  14. SmartCode 使用常见问题
  15. sublime text 自定义插件,自动插入署名,自定义插入日期,自动生成头部注释
  16. springboot:Java模板引擎Thymeleaf介绍
  17. C#日期转换(转载)
  18. Linux 小知识翻译 - 「别名」
  19. [BZOJ5427]最长上升子序列/[BZOJ4282]慎二的随机数列
  20. kubernetes 的pod控制器

热门文章

  1. python爬取“美团美食”汕头地区的所有店铺信息
  2. 51nod 1298:圆与三角形(计算几何)
  3. mybatis框架之装饰模式
  4. Angular JS - 3 - Angular JS 双向数据绑定
  5. 媲美5G的Wifi网速、“备战”资产一键领……揭秘双11小二背后的保障力量
  6. Apache Flink 的迁移之路,2 年处理效果提升 5 倍
  7. ceph优化记录 ceph.conf优化详解
  8. 图与例解读Async/Await
  9. HDU 6058 Kanade&#39;s sum —— 2017 Multi-University Training 3
  10. [CSP-S模拟测试]:God Knows(线段树维护单调栈)