java算法(11)
来源:互联网 发布:手机基站定位软件 编辑:程序博客网 时间:2024/05/16 14:02
原题:
/** * Created by gouthamvidyapradhan on 28/06/2017. * <p> * There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 104 balloons. * <p> * An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons. * <p> * Example: * <p> * Input: * [[10,16], [2,8], [1,6], [7,12]] * <p> * Output: * 2 * <p> * Explanation: * One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x */
答案:
public class BurstBalloons { /** * Main method * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { int[][] baloons = {{10, 16}, {2, 8}, {1, 6}, {7, 12}}; System.out.println(new BurstBalloons().findMinArrowShots(baloons)); } public int findMinArrowShots(int[][] points) { if (points.length == 0) return 0; Arrays.sort(points, ((o1, o2) -> o1[1] - o2[1])); int count = 0; int leftMost = points[0][1]; for (int i = 1; i < points.length; i++) { if (leftMost < points[i][0]) { count++; leftMost = points[i][1]; } } return count + 1; }}
阅读全文
0 0
- java算法(11)
- Java与算法(11)
- 行列式算法(java)
- 算法总结(java)
- Java算法(一)
- 递归算法(java)
- 银行家算法(java)
- 排序算法(java)
- 冒泡算法(java)
- 自学习算法(JAVA)
- MD5算法(Java)
- 递归算法(java)
- 发牌算法(java)
- 决策树算法(JAVA)
- 算法(Java)
- 排序算法(java)
- 算法--java(1)
- 算法--java(2)
- JAVA文件读写输入输出流关闭问题、JAVA垃圾回收
- zookeeper集群的安装
- 欢迎使用CSDN-markdown编辑器
- 第四周-项目二 建立单链表算法库
- 数据结构——第七讲、图(中)(1)
- java算法(11)
- 统计学习
- 基于sqlserver的常用查询、视图、触发器、存储过程用法
- Ruby基础知识学习
- Codeforces Round #440(div2)
- 点击列表跳转网页(webView)
- 贪心算法(1)
- nodejs将word文档转为html文件(脚本)--获取word文档内容
- java.util.concurrent包中的BlockingQuene