【leetcode】757. Set Intersection Size At Least Two
2024-08-28 09:36:20
题目如下:
解题思路:贪心算法。首先把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)
最新文章
- ecshop不同样式文章页调用不同文章模板
- selenium+testng+ant+jenkins 手记
- Zend Guard Loader/Zend Loader是干什么的
- linux挂载共享文件夹
- powerdesigner中怎么给一主键设为自增型auto increme
- JS常用的设计模式(14)—— 备忘录模式
- lighttpd 介绍及安装
- freemaker分页备忘
- LabVIEW系列——合并错误(VI)的用法
- werkzeug源码阅读笔记(二) 下
- python3 scrapy+Crontab部署过程
- luogu2402 奶牛隐藏
- Struts2中的struts.multipart.saveDir的配置
- SmartCode 使用常见问题
- sublime text 自定义插件,自动插入署名,自定义插入日期,自动生成头部注释
- springboot:Java模板引擎Thymeleaf介绍
- C#日期转换(转载)
- Linux 小知识翻译 - 「别名」
- [BZOJ5427]最长上升子序列/[BZOJ4282]慎二的随机数列
- kubernetes 的pod控制器
热门文章
- python爬取“美团美食”汕头地区的所有店铺信息
- 51nod 1298:圆与三角形(计算几何)
- mybatis框架之装饰模式
- Angular JS - 3 - Angular JS 双向数据绑定
- 媲美5G的Wifi网速、“备战”资产一键领……揭秘双11小二背后的保障力量
- Apache Flink 的迁移之路,2 年处理效果提升 5 倍
- ceph优化记录 ceph.conf优化详解
- 图与例解读Async/Await
- HDU 6058 Kanade&#39;s sum —— 2017 Multi-University Training 3
- [CSP-S模拟测试]:God Knows(线段树维护单调栈)