3025 - Find the Number of Ways to Place People I
info
- 문제 보기: 3025 - Find the Number of Ways to Place People I
- 소요 시간: 19분 51초
- 풀이 언어:
java - 체감 난이도: 2️⃣~3️⃣
- 리뷰 횟수: ✅
풀이 키워드
스포주의
정렬 수학
풀이 코드
info
- 메모리: 44520 KB
- 시간: 7 ms
class Solution {
int[][] points;
boolean check(int A, int B) {
// check if others are in the A-B area
int sx = points[A][0];
int ex = points[B][0];
int sy = points[B][1];
int ey = points[A][1];
for (int K = A+1; K < B; ++K) {
int x = points[K][0];
int y = points[K][1];
if (sx <= x && x <= ex && sy <= y && y <= ey) return false;
}
return true;
}
public int numberOfPairs(int[][] points) {
this.points = points;
Arrays.sort(points, (a, b) -> {
// x ASC(➡️) y DESC(⬇️)
int ax = a[0];
int ay = a[1];
int bx = b[0];
int by = b[1];
if (ax != bx) return ax - bx;
else return by - ay;
});
int ans = 0;
for (int A = 0; A < points.length-1; ++A) {
for (int B = A+1; B < points.length; ++B) {
if (points[A][1] < points[B][1]) continue; // A is not upper left
if (check(A, B)) ++ans;
}
}
return ans;
}
}