Hyunebee

씨름선수선발하기 본문

Java/코테

씨름선수선발하기

Hyunebee 2022. 5. 19. 11:58

그리디 알고리즘 문제 

 

 

 

키와 몸무게를 class로 받아옴 

객체의 정렬은 람다식 or compareTo로 정렬

import java.util.*;

class Body implements Comparable<Body>{
    int height;
    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;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt(); // 배열의 크기
        ArrayList<Body> arrayList = new ArrayList<>();


        for(int i = 0; i < N; i++){
            int height = sc.nextInt();
            int weight = sc.nextInt();
            arrayList.add(new Body(height, weight));
        }

        Collections.sort(arrayList,(Body x, Body y) -> y.height - x.height);
        //Collections.sort(arrayList);

        Main T = new Main();
        System.out.println(T.solution(N,arrayList));
    }

    public int solution(int k, ArrayList<Body> arr){
        int max = 0;
        int cnt = 0;
        for(int i = 0 ; i < k; i++){
            if(arr.get(i).weight > max){
                max = arr.get(i).weight;
                cnt++;
            }
        }
      return cnt;
    }
}

'Java > 코테' 카테고리의 다른 글

동시에 몇명까지 가능할까?  (0) 2022.05.19
회의를 몇번 들어갈수있나?  (0) 2022.05.19
연속해서 가장 긴 수  (0) 2022.05.19
K가 되는 수  (0) 2022.05.18
배열 합치기  (0) 2022.05.18