• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

씨름 O(n)으로 풀었는데 런타임 에러가 떠요

23.10.06 20:25 작성 조회수 160

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

yungjoon2님의 프로필

yungjoon2

질문자

2023.10.08

죄송합니다. 분명 Main으로 했었던것같은데... 아무튼 감사합니다...!!

0

안녕하세요^^

클래스의 이름을 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;
}
}

}