알고리즘/자바(Java) 알고리즘 문제풀이 입문: 코딩테스트 대비

씨름 선수

보름달빵 2025. 7. 21. 18:07

 

내가 푼 풀이 
import java.io.*;
import java.util.StringTokenizer;

public class Q1_씨름선수 {

    static People[] arr;
    static int count=0;

    static class People{
        int height;
        int weight;

        People(int h,int w){
            this.height=h;
            this.weight=w;
        }
    }

    static void compare(People curr){
        boolean isValid=true;
        for (People people : arr) {
             if(curr.height<people.height){
                 if(curr.weight< people.weight){
                     isValid=false;
                     break;
                 }
             }
        }
        if(isValid) count++;
    }
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st;

        int n = Integer.parseInt(br.readLine()); // 인원수

        arr = new People[n];

        for(int i=0;i<n;i++){
            st = new StringTokenizer(br.readLine());
            int h = Integer.parseInt(st.nextToken());
            int w = Integer.parseInt(st.nextToken());

            arr[i]= new People(h, w);
        }

        for(People p:arr){
            compare(p);
        }

        System.out.println(count);
    }
}

 

  • 각각의 인원들의 정보를 배열로 저장
  • 각 인원 마다 compare함수를 호출해서 키와 몸무게 비교하기 
  • 비교 도중 break 문을 사용하여 불필요한 연산 줄이기 

 

 

 

풀이과정 

 

Comparable 인터페이스

static class People implements Comparable<People>{
    int height;
    int weight;

    People(int h,int w){
        this.height=h;
        this.weight=w;
    }

    @Override
    public int compareTo(People o) {
        return o.height-this.height; // 키 큰 순서대로 정렬하기 
    }
}

 

Comparable은 자기 자신과 매개변수 객체를 비교할 수 있도록 만들어주는 인터페이스이다. 이 인터페이스를 구현하면 compareTo메서드를 통하여 객체간에 비교가 가능해진다.

 

 

package 섹션9_Greedy;

import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Q1_씨름선수 {


    static class People implements Comparable<People>{
        int height;
        int weight;

        People(int h,int w){
            this.height=h;
            this.weight=w;
        }

        @Override
        public int compareTo(People o) {
            return o.height-this.height; // 키 큰 순서대로 정렬하기
        }
    }


    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st;

        int n = Integer.parseInt(br.readLine()); // 인원수

        People[] arr = new People[n];

        for(int i=0;i<n;i++){
            st = new StringTokenizer(br.readLine());
            int h = Integer.parseInt(st.nextToken());
            int w = Integer.parseInt(st.nextToken());

            arr[i]= new People(h, w);
        }

        // 키를 기준으로 내림차순 정렬
        Arrays.sort(arr);

        int max=0;
        int count=0;
        for(People p:arr){
           if(p.weight>max){
               max=p.weight;
               count++;
           }
        }

        System.out.println(count);
    }
}

 

 

  • 키를 기준으로 먼저 정렬을 해두면, 나의 풀이과정 처럼 키와 몸무게를 같이 비교할 필요 없이 몸무게만 비교하면 된다. 
  • 키를 기준으로 내림차순 정렬이기 때문에, 뒤에 있는 값은 항상 앞에 있는 값보다 키가 작다. 따라서 "키와 몸무게 모두 지원자 보다 큰 사람이 존재하면 탈락" 하기 때문에 앞 사람에 비해 몸무게가 크다면, 키는 작지만 몸무게는 큰 경우가 되기 때문에 탈락하지 않게 되므로 count값을 증가시켜 준다.