๋ฌธ์ ์ดํดํ๊ธฐ
์๋ฅ์ ์์ ๋ฉด์ ์ ์๊ฐ ์๋ค.
์ง์์๋ค๋ผ๋ฆฌ ๋น๊ตํ์ ๋, ์๋ฅ์ ์์ ๋ฉด์ ์ ์๊ฐ ๋๋ค ๋ฎ์ผ๋ฉด ๋ถํฉ๊ฒฉ์ด๋ค.
์๋ฅ์ ์๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ณ , ๋ฉด์ ์ ์๋ฅผ ๋น๊ตํด๋ณด์.
ํ ์คํธ์ผ์ด์ค 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์ ๋ํ ์ ๋ณด๋ ๋งํฌ์์!
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();
}
}
'๐ฏ Coding Test' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
SWEA 1204. [S/W ๋ฌธ์ ํด๊ฒฐ ๊ธฐ๋ณธ] 1์ผ์ฐจ - ์ต๋น์ ๊ตฌํ๊ธฐ (0) | 2022.02.10 |
---|---|
๋ฐฑ์ค 1026๋ฒ : ๋ณด๋ฌผ (0) | 2021.10.27 |
๋ฐฑ์ค 2217๋ฒ : ๋กํ (์๊ฐ์ด๊ณผ ํด๊ฒฐ๊ณผ์ ) (0) | 2021.10.25 |
๋ฐฑ์ค 2839๋ฒ : ์คํ ๋ฐฐ๋ฌ (2) | 2021.10.23 |
๋ฐฑ์ค 19941๋ฒ : ํ๋ฒ๊ฑฐ ๋ถ๋ฐฐ (0) | 2021.10.21 |