当前位置: 首页 > news >正文

LeetCode -- Minimum Number of Arrows to Burst Balloons

题目描述:
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.


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.


Example:


Input:
[[10,16], [2,8], [1,6], [7,12]]


Output:
2


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 = 11 (bursting the other two balloons).


用最少数量的箭打完气球。气球的输入以二维数组point[,]表示,其中气球points[i,0]和points[i,1]表示气球横坐标的起始位置。


实现思路:
对气球位置进行排序,从左向右依次打,每次以当前气球最右边的横坐标进行射击。
遍历每个气球,如果当前气球没被打中,更新气球射击位置。


实现代码:


public class Solution {
    public int FindMinArrowShots(int[,] points) 
    {
        if(points == null){
            return 0;
        }
        var len = points.GetLength(0);
    	if(len == 0){
    	    return 0;
    	}
    	
    	var balls = new List<Balloon>(len);
    	for (var i = 0;i < len; i++){
    		balls.Add(new Balloon(points[i,0], points[i,1]));
    	}
    	
    	balls = balls.OrderBy(x=>x.X1).ThenBy(x=>x.X2).ToList();
    	//Console.WriteLine(balls);
    	var bulletCount = 1;
    	var selection = balls.First();
    	var bulletX = selection.X2;
    	for (var i = 1;i < balls.Count; i++){
    		if(balls[i].X1 <= bulletX){
    			bulletX = Math.Min(bulletX, balls[i].X2);
    		}else{
    			bulletX = balls[i].X2;
    			bulletCount++;
    		}
    	}
    	
    	//Console.WriteLine(balls);
    	
    	
    	return bulletCount;
    }
    


    public class Balloon{
    	public int X1;
    	public int X2;
    	public Balloon(int x1, int x2){
    		X1 = x1;
    		X2 = x2;
    	}
    }
}


相关文章:

  • 反醒反醒
  • LeetCode -- Arranging Coins
  • Bing在中国不会成功
  • LeetCode -- First Unique Character in a String
  • 搜狗输入法,无心插柳柳成荫
  • LeetCode -- Wildcard Matching
  • 弥平“第三道鸿沟”:3G运营商必须承担的社会责任
  • 使用面向对象重构之-从过程式设计到面向对象
  • Bing API初体验
  • 使用面向对象重构之-继承中的抽象—模板方法
  • www.hellocpp.net开发日记:网站性能优化之文件服务器分离技术
  • 使用面向对象重构之-使用接口完成行为抽象
  • Flex与.NET互操作(十):FluorineFx.Net的及时通信应用(ApplicationAdapter)(一)
  • 使用面向对象重构之-使用接口抽象完成不同维度的扩展
  • Flex与.NET互操作(十一):FluorineFx.Net的及时通信应用(Remote Procedure Call)(二)
  • 【技术性】Search知识
  • Apache的80端口被占用以及访问时报错403
  • CentOS从零开始部署Nodejs项目
  • emacs初体验
  • ES6简单总结(搭配简单的讲解和小案例)
  • JavaScript/HTML5图表开发工具JavaScript Charts v3.19.6发布【附下载】
  • Java精华积累:初学者都应该搞懂的问题
  • Java新版本的开发已正式进入轨道,版本号18.3
  • leetcode98. Validate Binary Search Tree
  • MyEclipse 8.0 GA 搭建 Struts2 + Spring2 + Hibernate3 (测试)
  • 闭包--闭包之tab栏切换(四)
  • 表单中readonly的input等标签,禁止光标进入(focus)的几种方式
  • 从地狱到天堂,Node 回调向 async/await 转变
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 机器学习 vs. 深度学习
  • 漂亮刷新控件-iOS
  • 如何使用 OAuth 2.0 将 LinkedIn 集成入 iOS 应用
  • 什么是Javascript函数节流?
  • 试着探索高并发下的系统架构面貌
  • 我与Jetbrains的这些年
  • 一道面试题引发的“血案”
  • 在Unity中实现一个简单的消息管理器
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • MPAndroidChart 教程:Y轴 YAxis
  • 阿里云重庆大学大数据训练营落地分享
  • ​力扣解法汇总946-验证栈序列
  • ​人工智能书单(数学基础篇)
  • ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
  • #我与Java虚拟机的故事#连载16:打开Java世界大门的钥匙
  • (附源码)springboot车辆管理系统 毕业设计 031034
  • (附源码)计算机毕业设计SSM保险客户管理系统
  • (论文阅读31/100)Stacked hourglass networks for human pose estimation
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (转)http协议
  • (转)shell调试方法
  • (转)如何上传第三方jar包至Maven私服让maven项目可以使用第三方jar包
  • .java 9 找不到符号_java找不到符号
  • .NET CORE使用Redis分布式锁续命(续期)问题
  • .net(C#)中String.Format如何使用
  • .NET中 MVC 工厂模式浅析