23.10.06 20:25 작성
·
216
0
package GreedyAlgorithm;
import java.util.*;
public class SsireumSelection {
static Scanner sc = new Scanner(System.in);
static ArrayList<Body> athlete = new ArrayList<>();
static int n = sc.nextInt();
static int answer;
static int maxWeight=Integer.MIN_VALUE;
public static void main(String[] args) {
for (int i = 0; i < n; i++) {
athlete.add(new Body(sc.nextInt(), sc.nextInt()));
}
Collections.sort(athlete);
for (Body b : athlete) {
if (b.weight > maxWeight) {
answer++;
maxWeight=b.weight;
}
}
System.out.println(answer);
}
public static class Body implements Comparable<Body> {
public int height;
public int weight;
public Body(int height, int weight) {
this.height = height;
this.weight = weight;
}
@Override
public int compareTo(Body o) {
return o.height - this.height;
}
}
}
90ms~126ms, 26mb 나옵니다
답변 2
0
0
2023. 10. 07. 20:58
안녕하세요^^
클래스의 이름을 Main으로 해야 합니다.
import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static ArrayList<Body> athlete = new ArrayList<>();
static int n = sc.nextInt();
static int answer;
static int maxWeight=Integer.MIN_VALUE;
public static void main(String[] args) {
for (int i = 0; i < n; i++) {
athlete.add(new Body(sc.nextInt(), sc.nextInt()));
}
Collections.sort(athlete);
for (Body b : athlete) {
if (b.weight > maxWeight) {
answer++;
maxWeight=b.weight;
}
}
System.out.println(answer);
}
public static class Body implements Comparable<Body> {
public int height;
public int weight;
public Body(int height, int weight) {
this.height = height;
this.weight = weight;
}
@Override
public int compareTo(Body o) {
return o.height - this.height;
}
}
}