Easy java solution beating 95%


  • 0
    Q

    Easy java solution beating 95%

    public class Solution {
        long sum = 0;
        float mid = 0;
        public boolean isReflected(int[][] points) {
            if (points.length<=1) return true;
            int min = points[0][0];
            int max = points[0][0];
            for (int[] p : points){
                min = Math.min(min,p[0]);
                max = Math.max(max,p[0]);
            }
            sum = min+max;
            mid = min;
            mid += max;
            mid /= 2;
            Arrays.sort(points, new myCompare());
            
            int left = 0, right = points.length -1;
            while (left < right){
                if (points[left][0]+points[right][0] != sum)
                    return false;
                else if (points[left][1] != points[right][1])
                    break;
                left++;
                right --;
                while(left < points.length - 1 ) {
                    if(points[left][0] == points[left-1][0] && points[left][1] == points[left-1][1]){
                        left ++;
                    }else{
                        break;
                    }
                    
                }
                while(right > 0) {
                    
                    if(points[right][0] == points[right+1][0] && points[right][1] == points[right+1][1]){
                        right--;
                    }else{
                        break;
                    }
                }
            }
    
            
    
            while (left <= right) {
                if(points[left][0] != mid) {
                    return false;
                }
                left ++;
            }
            return true;
        }
        public class myCompare implements Comparator<int[]>{
                @Override
                public int compare(int[] p1, int[] p2){
                    if (p1[0]!=p2[0])
                        return Integer.compare(p1[0],p2[0]);
                    if (p1[0] <= mid)
                        return Integer.compare(p1[1],p2[1]);
                    return Integer.compare(p2[1],p1[1]);
                }
            }
    }
    
    

Log in to reply
 

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