๐ŸŽฏ Coding Test

๋ฐฑ์ค€ 1946๋ฒˆ : ์‹ ์ž…์‚ฌ์›

์—ฐ_์šฐ๋ฆฌ 2021. 10. 26. 02:27
๋ฐ˜์‘ํ˜•


๋ฌธ์ œ ์ดํ•ดํ•˜๊ธฐ

์„œ๋ฅ˜์ ์ˆ˜์™€ ๋ฉด์ ‘์ ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

์ง€์›์ž๋“ค๋ผ๋ฆฌ ๋น„๊ตํ–ˆ์„ ๋•Œ, ์„œ๋ฅ˜์ ์ˆ˜์™€ ๋ฉด์ ‘์ ์ˆ˜๊ฐ€ ๋‘˜๋‹ค ๋‚ฎ์œผ๋ฉด ๋ถˆํ•ฉ๊ฒฉ์ด๋‹ค.

์„œ๋ฅ˜์ ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ , ๋ฉด์ ‘์ ์ˆ˜๋ฅผ ๋น„๊ตํ•ด๋ณด์ž.

 

ํ…Œ์ŠคํŠธ์ผ€์ด์Šค 1๋ฒˆ

    (1, 4)  ์„œ๋ฅ˜1๋“ฑ : ํ•ฉ๊ฒฉ
    (2, 3)  ํ•ฉ๊ฒฉํ•œ 1๋“ฑ๋ณด๋‹ค ์„œ๋ฅ˜๋“ฑ์ˆ˜ ๋‚ฎ์Œ, ๋ฉด์ ‘๋“ฑ์ˆ˜ ๋†’์Œ : ํ•ฉ๊ฒฉ
    (3, 2)  ํ•ฉ๊ฒฉํ•œ 2๋“ฑ๋ณด๋‹ค ์„œ๋ฅ˜๋“ฑ์ˆ˜ ๋‚ฎ์Œ, ๋ฉด์ ‘๋“ฑ์ˆ˜ ๋†’์Œ : ํ•ฉ๊ฒฉ
    (4, 1)  ํ•ฉ๊ฒฉํ•œ 3๋“ฑ๋ณด๋‹ค ์„œ๋ฅ˜๋“ฑ์ˆ˜ ๋‚ฎ์Œ, ๋ฉด์ ‘๋“ฑ์ˆ˜ ๋†’์Œ : ํ•ฉ๊ฒฉ
    (5, 5)  ํ•ฉ๊ฒฉํ•œ 4๋“ฑ๋ณด๋‹ค ์„œ๋ฅ˜๋“ฑ์ˆ˜ ๋‚ฎ์Œ, ๋ฉด์ ‘๋“ฑ์ˆ˜ ๋‚ฎ์Œ : ๋ถˆํ•ฉ๊ฒฉ

ํ…Œ์ŠคํŠธ์ผ€์ด์Šค 2๋ฒˆ
    (1, 4)  ์„œ๋ฅ˜1๋“ฑ : ํ•ฉ๊ฒฉ
    (2, 5)  ํ•ฉ๊ฒฉํ•œ 1๋“ฑ๋ณด๋‹ค ์„œ๋ฅ˜๋“ฑ์ˆ˜ ๋‚ฎ์Œ, ๋ฉด์ ‘๋“ฑ์ˆ˜ ๋‚ฎ์Œ : ๋ถˆํ•ฉ๊ฒฉ
    (3, 6)  ํ•ฉ๊ฒฉํ•œ 1๋“ฑ๋ณด๋‹ค ์„œ๋ฅ˜๋“ฑ์ˆ˜ ๋‚ฎ์Œ, ๋ฉด์ ‘๋“ฑ์ˆ˜ ๋‚ฎ์Œ : ๋ถˆํ•ฉ๊ฒฉ
    (4, 2)  ํ•ฉ๊ฒฉํ•œ 1๋“ฑ๋ณด๋‹ค ์„œ๋ฅ˜๋“ฑ์ˆ˜ ๋‚ฎ์Œ, ๋ฉด์ ‘๋“ฑ์ˆ˜ ๋†’์Œ : ํ•ฉ๊ฒฉ 
    (5, 7)  ํ•ฉ๊ฒฉํ•œ 4๋“ฑ๋ณด๋‹ค ์„œ๋ฅ˜๋“ฑ์ˆ˜ ๋‚ฎ์Œ, ๋ฉด์ ‘๋“ฑ์ˆ˜ ๋‚ฎ์Œ : ๋ถˆํ•ฉ๊ฒฉ
    (6, 1)  ํ•ฉ๊ฒฉํ•œ 4๋“ฑ๋ณด๋‹ค ์„œ๋ฅ˜๋“ฑ์ˆ˜ ๋‚ฎ์Œ, ๋ฉด์ ‘๋“ฑ์ˆ˜ ๋†’์Œ : ํ•ฉ๊ฒฉ
    (7, 3)  ํ•ฉ๊ฒฉํ•œ 6๋“ฑ๋ณด๋‹ค ์„œ๋ฅ˜๋“ฑ์ˆ˜ ๋‚ฎ์Œ, ๋ฉด์ ‘๋“ฑ์ˆ˜ ๋‚ฎ์Œ : ๋ถˆํ•ฉ๊ฒฉ

 

์ฝ”๋“œ

์ง€์›์ž ๊ฐ์ฒด : { ์„œ๋ฅ˜์ ์ˆ˜, ๋ฉด์ ‘์ ์ˆ˜ }

์ง€์›์ž ๋ฐฐ์—ด : [ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ][ ์ผ€์ด์Šค๋ณ„์ง€์›์ž์ˆ˜ ]

์ง€์›์ž ๊ฐ์ฒด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ์„œ๋ฅ˜์ ์ˆ˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ 

์ฐจ๋ก€๋Œ€๋กœ ํ•ฉ๊ฒฉ์ž์˜ ๋ฉด์ ‘์ ์ˆ˜์™€ ์ง€์›์ž์˜ ๋ฉด์ ‘์ ์ˆ˜๋ฅผ ๋น„๊ตํ•œ๋‹ค.

 

๐Ÿ’› Comparator์— ๋Œ€ํ•œ ์ •๋ณด๋Š” ๋งํฌ์—์„œ!

 

์ž๋ฐ” ๋ฐฐ์—ด, ๊ฐ์ฒด ์ •๋ ฌํ•˜๊ธฐ : Comparable, Comparator ์ธํ„ฐํŽ˜์ด์Šค (+๋‹ค์ค‘์ •๋ ฌ ์˜ˆ์‹œ)

[๋ชฉ์ฐจ] ๐ŸŸข ๊ธฐ๋ณธํ˜•๋ณ€์ˆ˜, Wrapperํด๋ž˜์Šค ๋ฐฐ์—ด ์ •๋ ฌ  Arrays.sort(๋ฐฐ์—ด๋ช…) : ๋ฐฐ์—ด ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ  Arrays.sort(๋ฐฐ์—ด๋ช…, Collections.reverseOrder()) : ๋ฐฐ์—ด ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ ๐ŸŸข ๊ฐ์ฒด ์ •๋ ฌ : Comparable๊ณผ Comparato..

lotuus.tistory.com

import java.io.*;
import java.util.*;

class Applicant{
    private int document;
    private int interview;
    public int getDocument() { return document;  }
    public int getInterview() { return interview; }
    public String getAllPrint() { return "("+document+", "+interview+")"; }
    public Applicant(int document, int interview) {
        this.document = document;
        this.interview = interview;
    }
}

class NewEmployee{
    int testcase;
    int applicants_count;
    Applicant[][] applicants;

    void scan_input(String inputed) {
        InputStream in = new ByteArrayInputStream(inputed.getBytes());
        System.setIn(in);
    }

    void scan() throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        //ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ˆ˜ ์ž…๋ ฅ
        st = new StringTokenizer(br.readLine());
        testcase = Integer.parseInt(st.nextToken());
        applicants = new Applicant[testcase][];

        //ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋งŒํผ ๋ฐ˜๋ณต
        for (int i = 0; i < testcase; i++) {

            //์ผ€์ด์Šค๋ณ„ ์ง€์›์ž์ˆ˜ ์ž…๋ ฅ
            st = new StringTokenizer(br.readLine());
            applicants_count = Integer.parseInt(st.nextToken());
            applicants[i] = new Applicant[applicants_count];

            //์ž…๋ ฅ๊ฐ’ ๊ฐ์ฒด์— ์ž…๋ ฅ
            for (int j = 0; j < applicants_count; j++) {
                st = new StringTokenizer(br.readLine());
                applicants[i][j] = new Applicant(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
            }
        }
    }

    void DocumentSort(){
        //ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋งŒํผ ๋ฐ˜๋ณต
        for (Applicant[] applicant : applicants) {
            //๊ฐ์ฒด์˜ ๊ฐ’ ์ •๋ ฌ
            Arrays.sort(applicant, new Comparator<Applicant>() {
                @Override
                public int compare(Applicant o1, Applicant o2) {
                    if(o1.getDocument() > o2.getDocument()){       return 1;     }
                    else if(o1.getDocument() < o2.getDocument()){  return -1;    }
                    else{                                           return 0;    }
                }
            });
        }
    }

    void process() {

        //์„œ๋ฅ˜์ ์ˆ˜ ์˜ค๋ฆ„์ฐจ์ˆœ์ •๋ ฌ
        DocumentSort();

        //ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋งŒํผ ๋ฐ˜๋ณต
        for (int i = 0; i < testcase; i++) {
            int result = 1;
            //์„œ๋ฅ˜์ ์ˆ˜ 1๋“ฑํ•œ ์ž์˜ ๋ฉด์ ‘๋“ฑ์ˆ˜
            int itvRank = applicants[i][0].getInterview();

            //์ผ€์ด์Šค๋ณ„ ์ง€์›์ž์ˆ˜๋งŒํผ ๋ฐ˜๋ณต
            for (int j = 1; j < applicants[i].length; j++) {
                //System.out.print(itvRank + " : " + applicants[i][j].getInterview());

                if(itvRank > applicants[i][j].getInterview()){
                    //ํ•ฉ๊ฒฉํ•œ์ž์˜ ๋ฉด์ ‘๋“ฑ์ˆ˜ > ์ง€์›์ž์˜ ๋ฉด์ ‘๋“ฑ์ˆ˜
                    //System.out.println("    ํ•ฉ๊ฒฉ");
                    itvRank = applicants[i][j].getInterview();
                    result++;

                }else{
                    //ํ•ฉ๊ฒฉํ•œ์ž์˜ ๋ฉด์ ‘๋“ฑ์ˆ˜ < ์ง€์›์ž์˜ ๋ฉด์ ‘๋“ฑ์ˆ˜
                    //System.out.println("    ๋ถˆํ•ฉ๊ฒฉ");
                }
            }

            System.out.println(result);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        NewEmployee r = new NewEmployee();
        try {
            r.scan();
        } catch (IOException e) {
            e.printStackTrace();
        }
        r.process();
    }
}
import org.junit.jupiter.api.Test;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*;

class MainTest {

    @Test
    void Test1(){
        NewEmployee r = new NewEmployee();
        r.scan_input("2\n" +
                "5\n" +
                "3 2\n" +
                "1 4\n" +
                "4 1\n" +
                "2 3\n" +
                "5 5\n" +
                "7\n" +
                "3 6\n" +
                "7 3\n" +
                "4 2\n" +
                "1 4\n" +
                "5 7\n" +
                "2 5\n" +
                "6 1");
        try {
            r.scan();
        } catch (IOException e) {
            e.printStackTrace();
        }
        r.process();
    }
}

 

๋ฐ˜์‘ํ˜•
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ํŠธ์œ„ํ„ฐ ๊ณต์œ ํ•˜๊ธฐ
  • ๊ตฌ๊ธ€ ํ”Œ๋Ÿฌ์Šค ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜คํ†ก ๊ณต์œ ํ•˜๊ธฐ