강의

멘토링

커뮤니티

Inflearn Community Q&A

yungjoon22554's profile image
yungjoon22554

asked

Introduction to Java Algorithm Problem Solving: Coding Test Preparation

1. Wrestler

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

Written on

·

281

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 나옵니다

java코딩-테스트

Answer 2

0

yungjoon2님의 프로필 이미지
yungjoon2
Questioner

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

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

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

}
yungjoon22554's profile image
yungjoon22554

asked

Ask a question