https://softeer.ai/practice/6289
문제 풀이법
- 무게들을 저장하는 int 배열과, 사람 수 크기의 boolean 배열 생성(그리고 모두 true로 초기화)
- A > B면 boolean 배열에 B 인덱스를 false
A == B면 boolean 배열에 A, B 인덱스를 false
A < B면 boolean 배열에 A 인덱스를 false
즉, 한 번이라도 친구들끼리 무게에서 지거나 비겼으면 false로 바꿈 - 마지막으로 boolean 배열에 true의 개수를 리턴
코드
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine());
int numOfWeight = Integer.parseInt(st.nextToken());
int numOfFriend = Integer.parseInt(st.nextToken());
int[] arrOfWeight = new int[numOfWeight + 1];
boolean[] isBest = new boolean[numOfWeight + 1];
st = new StringTokenizer(in.readLine());
for (int i = 1; i <= numOfWeight; i++) {
arrOfWeight[i] = Integer.parseInt(st.nextToken());
isBest[i] = true;
}
for (int j = 1; j <= numOfFriend; j++) {
st = new StringTokenizer(in.readLine());
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
if (arrOfWeight[A] < arrOfWeight[B]) {
isBest[A] = false;
} else if (arrOfWeight[A] == arrOfWeight[B]) {
isBest[A] = false;
isBest[B] = false;
} else if (arrOfWeight[A] > arrOfWeight[B]) {
isBest[B] = false;
}
}
int answer = 0;
for (boolean best : isBest) {
if (best) {
answer++;
}
}
System.out.println(answer);
}
}
'Development > CodingTest' 카테고리의 다른 글
[이분 탐색] 프로그래머스 Level 3 입국심사 Java 풀이 (1) | 2024.03.07 |
---|---|
[String] [한양대 HCPC 2023] X marks the Spot Java 풀이 ( + 시간 초과 해결) (0) | 2024.02.01 |
[Greedy] Softeer(소프티어) level 2 진정한 효도 Java 풀이 (0) | 2024.01.31 |
[Greedy] Softeer(소프티어) level 2 연탄의 크기 java 풀이 (1) | 2024.01.30 |
[Greedy] Softeer(소프티어) level 3 강의실 배정 java 풀이 (+Comparator에 관하여) (1) | 2024.01.29 |