题目如下:

解题思路:贪心算法。首先把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的更多相关文章

  1. 【LeetCode】952. Largest Component Size by Common Factor 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...

  2. 【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 ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  5. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  6. 【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 ...

  7. 【leetcode】 First Missing Positive

    [LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...

  8. 【Leetcode】104. 二叉树的最大深度

    题目 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,null,15,7 ...

  9. 【LeetCode】面试题13. 机器人的运动范围

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

随机推荐

  1. Django学习——collectstatic错误

    Error fetching command 'collectstatic': You're using the staticfiles app without having set the STAT ...

  2. HTML基础知识笔记摘要

    HTML互联网三大基石:1.HTML:显示数据2.HTTP:传输数据 http传输协议3.URL:定位数据协议://ip地址或主机名:端口/网络中的内容... HTML(hyper text make ...

  3. Buuctf | sqli-labs

    这个是赵师傅给我们提供的训练靶场,最好都打一遍,但是出于找flag的角度,特此记录一下,flag在哪里[没错,我就是喜欢我的蓝变红,哈] ?id=1' :报错,说明就是用这个闭合的 ?id=0' un ...

  4. Jdk1.8 之 Integer类源码浅析

    先看一下它的继承.实现关系: public final class Integer extends Number implements Comparable<Integer> Number ...

  5. MySQL按首字母查询

    DELIMITER $$ CREATE /*[DEFINER = { user | CURRENT_USER }]*/ ))) CHARSET utf8 BEGIN ); ); )); SET V_R ...

  6. mysql中or和in,in和exists的效率问题

     mysql中or和in的效率问题      在网上一直看到的是or和in的效率没啥区别,一直也感觉是这样,前几天刚好在看<mysql数据库开发的36条军规>的文章,里面提到了or和in的 ...

  7. Xcode 10如何打包ipa包?

    参考: https://www.jianshu.com/p/0421b3fd2470 前置条件 首先导入证书和配置文件 具体操作步骤: product>>Archive 如图所示,选择Di ...

  8. Python Django 编写一个简易的后台管理工具1-安装环境

    安装python环境 MAC 一般都会自带 Python2.x版本 的环境,你也可以在链接 https://www.python.org/downloads/mac-osx/ 上下载最新版安装. 安装 ...

  9. Javascript在ajax提交过程中页面显示加载中,请等待效果,并在提交过程中限制确定按钮防止多次提交,提交完成后,解除提交限制

    加载中,请等待div: <div id="load" class="center-in-center" style="display:none; ...

  10. java常用排序

    1.冒泡排序 public static int[] bubble(int[] a){ for(int i=0;i<a.length-1;i++){ int tmp=0; for(int j=0 ...