public class Solution {
    public int FindMinArrowShots(int[,] points)
        {
            // multidimensional array cannot be sorted directly - copy to objects
            Pt[] pts = new Pt[points.GetLength(0)];
            for (int i = 0; i < points.GetLength(0); i++)
            {
                pts[i] = new Pt(points[i, 0], points[i, 1]);
            }
            Array.Sort(pts, (a, b) => a.start.CompareTo(b.start));

            int cnt = 0;
            Pt prev = null;
            for (int i = 0; i < pts.Length; i++)
            {
                if (prev == null || prev.end < pts[i].start)
                {
                    cnt++;
                    prev = pts[i];
                }
                else if (pts[i].end < prev.end)
                {
                    prev.end = pts[i].end;
                }
            }
            return cnt;
        }

        public class Pt
        {
            public int start;
            public int end;
            public Pt(int s, int e) { start = s; end = e; }
        }
}

https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/#/solutions

posted on 2017-06-09 08:31  Sempron2800+  阅读(124)  评论(0编辑  收藏  举报