카테고리 없음

Comparable class + compreTo 오버라이딩

서버관리자 페페 2022. 12. 8. 04:05

참조

https://www.daleseo.com/java-comparable-comparator/

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

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

        // ISC - L1
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());

        // L2~
        mData[] A = new mData[N];
        for (int i = 0; i < N; i++) {
            A[i] = new mData(Integer.parseInt(br.readLine()), i);
        }
        Arrays.sort(A);

        // Operating marker
        int Max = 0;
        for (int i = 0; i < N; i++) {
            if (Max < A[i].index-i)
                Max = A[i].index-i;
        }
        
        // OEC
        System.out.println(Max+1);
    }
}

// External Class
class mData implements Comparable<mData> {

    int value;
    int index;

    public mData(int value, int index) {
        super();
        this.value = value;
        this.index = index;

    }

    @Override
    public int compareTo(mData o) {
        return this.value - o.value;
    }
}