c# solution : convert to jagged array, then sort, greedy


  • 0
    N

    ···
    public class Solution {
    public int FindMinArrowShots(int[,] points) {
    if(points == null || points.Length == 0) return 0;
    var arr = Convert(points);
    Array.Sort(arr, (a,b)=> a[0] != b[0] ? a[0].CompareTo(b[0]) : a[1].CompareTo(b[1]));

        var count = 1;
        var limit = arr[0][1];
        for(var i=1;i<arr.Length;i++){
            if(arr[i][0]<= limit){
                limit = Math.Min(limit, arr[i][1]);
            }
            else {
                count++;
                limit = arr[i][1];
            }
        }
        
        return count;
        
    }
    
    public int[][] Convert(int [,] arr) {
        int[][] ret = new int[arr.GetLength(0)][];
        
        for(var i=0;i<ret.Length;i++){
            ret[i] = new int[arr.GetLength(1)];
            for(var j=0;j<ret[0].Length;j++){
                ret[i][j] = arr[i,j];
            }
        }
        
        return ret;
    }
    

    }
    ···


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.