lintcode:数飞机
数飞机
给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机?
如果多架飞机降落和起飞在同一时刻,我们认为降落有优先权。
对于每架飞机的起降时间列表:[[1,10],[2,3],[5,8],[4,7]], 返回3。
解题
利用HashMap记录每个时刻的飞机数量
利用set记录时间点
最后对set 的每个时间点,找出连续时间点飞机数量和最大的那个时间段
/**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
*/ class Solution {
/**
* @param intervals: An interval array
* @return: Count of airplanes are in the sky.
*/
public int countOfAirplanes(List<Interval> airplanes) {
// write your code here
if(airplanes == null || airplanes.size() == 0)
return 0;
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
Set<Integer> set = new TreeSet<Integer>();// 记录此时刻的飞机数量
for(int i =0;i<airplanes.size();i++){
int s = airplanes.get(i).start;
int e = airplanes.get(i).end;
if(map.containsKey(s))
map.put(s,map.get(s) + 1);// 空中的飞机 + 1
else
map.put(s,1);
if(map.containsKey(e))
map.put(e,map.get(e) - 1);// 空中飞机 - 1
else
map.put(e,-1);
set.add(s);// 该时间点的飞机数 set是升序排序的
set.add(e);
}
Iterator it = set.iterator();
int maxAirNo = Integer.MIN_VALUE;
int curcount = 0;
Iterator<Integer> tmp = set.iterator();
while(tmp.hasNext()){
curcount += map.get(tmp.next()); // 从最小时间点开始找出连续最长的飞机数和
maxAirNo = Math.max(maxAirNo, curcount);
} return maxAirNo;
}
}
改成这样的好理解点
public class Solution {
/**
* @param n n pairs
* @return All combinations of well-formed parentheses
*/
public ArrayList<String> generateParenthesis(int n) {
// Write your code here
ArrayList<String> result = new ArrayList<String>();
if(n<=0)
return result;
int left = 0,right=0;
String str="";
generateParenthesis(n,left,right,str,result);
return result;
}
public void generateParenthesis(int n,int left,int right,String str ,ArrayList<String> result){
// left == n 的时候只能加右括号
if(left == n){
for(int i=0;i< n- right;i++)
str+=")";
result.add(str);
return;
}
// left >= right 左括号 右括号都可以添加
if(left>=right){
generateParenthesis(n, left+1, right,str+"(",result); // left 长度加一
generateParenthesis(n, left, right+1,str+")",result); // right 长度加一
}else // left< right 失败
return;
}
}
lintcode:数飞机的更多相关文章
- lintcode-【中等】数飞机
题目: 给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机? 样例: 对于每架飞机的起降时间列表:[[1,10],[2,3],[5,8],[4,7]], ...
- H5数飞机
当时进入民航大培训前做过一系列的测试,一共是8个小游戏,主要测试情景意识.反应能力.场面控制之类的,有几个还记忆犹新,这个数飞机只是其中之一,今天没事用JavaScript做了一遍. 原理 逻辑比较简 ...
- LintCode 391: Count Of Airplanes
LintCode 391: Count Of Airplanes 题目描述 给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机? 样例 对于每架飞机的起 ...
- lintcode算法周竞赛
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- <Win32_20>纯c语言版的打飞机游戏出炉了^_^
经过昨天的苦战,终于完成了纯C版的打飞机游戏——使用微信打飞机游戏的素材,不过玩法有些不同,下面会有详述 一.概述游戏的玩法.实现效果 1. 游戏第一步,简单判断一下,给你一个准备的时间: 2.选择& ...
- 7九章算法强化班全解--------Hadoop跃爷Spark
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- lintcode 落单的数(位操作)
题目1 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 链接:http://www.lintcode.com/zh-cn/problem/single ...
- [Lintcode two-sum]两数之和(python,双指针)
题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个 ...
- [LintCode/LeetCode]——两数和、三数和、四数和
LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号: ...
随机推荐
- webstorm 添加文件模板
Ctrl+Shift+A 搜索设置 File Teamplate 添加 File Teamplate
- c#键盘鼠标钩子
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- C# WinForm设置TreeView选中节点
这里假定只有两级节点,多级方法类似.遍历节点,根据选中节点文本找到要选中的节点.treeView.SelectedNode = selectNode; /// <summary> /// ...
- zip生成
生成zip文件官方网站:http://www.phpconcept.net/pclzip/ 用法一: 1 <?php 2 include_once('pclzip.lib.php'); ...
- Translation001——android
请尊重原创,转载请注明出处: Author:KillerLegend Link:http://www.cnblogs.com/killerlegend/ BEGIN****************** ...
- 客户端访问WebService和PageMethod
客户端访问WebService 客户端访问WebService和后台访问WebService没什么不同,注意的地方是要在ScriptManager中添加 <Services> ...
- WPF 实现QQ抖动
//wpf中实现类似于qq的抖动窗效果 //前段页面 <Window x:Class="WpfApplication4.MainWindow" xmlns="htt ...
- 【Web学习日记】——C#引用WebService,从配置文件改变引用地址
开发环境:Win7 32位,开发工具:VS2013,.Net:4.0 初用WebService,很多地方都搞不清楚怎么回事,但稍作研究之后,也就用上了,根本就没有考虑后续事情. 但是,随着项目的进行, ...
- MySQL 库大小、表大小、索引大小查询命令
1.进去指定schema 数据库(存放了其他的数据库的信息) mysql> use information_schema; 2.查询所有数据的大小 mysql> sele ...
- sql 对一张表进行按照不同条件进行多次统计
最近一直在做数据统计,在此过程中,遇到过好多种情况都是对一张表按照不同的条件进行多次统计,以前的做法是统计几次按照不同的条件left join 几次,虽然也能得到想要的结果,但是效率太低,反映在页面就 ...