[leetcode] 3025. Find the Number of Ways to Place People I

AI·2025년 9월 3일

https://leetcode.com/problems/find-the-number-of-ways-to-place-people-i/description/

class Solution {
    public int numberOfPairs(int[][] points) {
        //x로 내림차순 -> y값이 같거나 큰게 없으면 pass, 있으면 비교 후 사각형 안에 다른 점이 들어가는지 확인
        int count = 0;
        
        Arrays.sort(points, (a, b) -> {
            int cmpX = Integer.compare(b[0], a[0]); // x 내림차순
            if (cmpX != 0) return cmpX;
            return Integer.compare(a[1], b[1]); // y 오름차순
        });
        
        for(int i=0;i<points.length;i++){
            for(int j=i+1;j<points.length;j++){
                if(points[i][1] > points[j][1]) continue;
                else{
                    // x,y로 범위 만들어서 범위 안에 다른 값이 들어가는지 확인 => 들어가면 확인 그만하고 아니면 count++
                    // 선택한 2점을 제외하고 포함되는지 확인
                    boolean blocked = false;
                    for(int k=i+1;k<j;k++){
                        if(inside(points[k][0],points[k][1], points[j][0], points[j][1], points[i][0], points[i][1])){
                            blocked = true;
                            break;
                        }
                    }
                    if(!blocked) count++;
                }
            }
            
        }

        return count;
    }

    // 확인할 좌표, j 좌표, i 좌표
    static boolean inside(int x, int y, int minX, int maxY, int maxX, int minY){
        if( (x>=minX && x<=maxX) && (y>=minY && y<=maxY) ) return true;
        else return false;
    }
}

0개의 댓글