【leetcode】757. Set Intersection Size At Least Two
题目如下:

解题思路:贪心算法。首先把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)
【leetcode】757. Set Intersection Size At Least Two的更多相关文章
- 【LeetCode】952. Largest Component Size by Common Factor 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...
- 【leetcode】952. Largest Component Size by Common Factor(Union find)
You are given an integer array of unique positive integers nums. Consider the following graph: There ...
- 【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】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- 【leetcode】Find All Anagrams in a String
[leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- 【Leetcode】104. 二叉树的最大深度
题目 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,null,15,7 ...
- 【LeetCode】面试题13. 机器人的运动范围
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
随机推荐
- 【leetcode】1023. Camelcase Matching
题目如下: A query word matches a given pattern if we can insert lowercaseletters to the pattern word so ...
- ROS基础学习总结
最近一周因为工程需要,把ROS的一些基础学习了一下,现在做一下总结. 学习资源: #创客智造整理的wiki上的ROS入门教程(中文)https://www.ncnynl.com/category/ro ...
- python 爬取<a>标签内href的方法及遇到的问题
原博客地址: https://www.cnblogs.com/dengyg200891/p/6060010.html # -*- coding:utf-8 -*- #python 2.7 #XiaoD ...
- jmeter.bat无法启动
jmeter.bat启动时提示:'findstr' 不是内部或外部命令,也不是可运行的程序或批处理文件. 学习中遇到的问题: 'findstr' 不是内部或外部命令,也不是可运行的程序或批处理文件. ...
- MariaDB强势席卷DB-Engines榜单后续,与阿里云达成全球独家战略合作
2018年10月份,DB-Engines 发布了月全球数据库排名,排名前三的一如既往还是Oracle.MySQL.Microsoft SQL Server.排名是重要指标,同时增长率的重要性也同样备受 ...
- oracle集合的应用
union:求并集,公共部分只包含一个 ABC 和AB都没有显示出来 2:union all 相同的数据会包含两个 3:交集 intersect 既包含A又包含B 4:求差集 A集合中的数据B集合中 ...
- java File类的使用以及一些函数
package file; import java.io.File; import java.io.IOException; import org.junit.jupiter.api.Test; /* ...
- react教程 — 组件
一.state使用: 1.什么时候不能 设置state(或没有必要设置): a.constructor. 2.默认的 state 值,一定要在初始化设置.因为,render 比 setState 早. ...
- 多级xml解析方案
package com.people.xmlToSql; import java.io.File; import java.io.IOException; import java.io.StringW ...
- MVC 入门
MVC是什么? MVC是一个框架模式,它用于把应用程序的输入.处理和输出进行强制性的分开.使用MVC应用程序被分成三个核心部件:模型.视图.控制器.它们各自处理自己的任务.最典型的MVC就是JSP+S ...