프로그래밍/알고리즘 풀이
[알고리즘] 백준 10816번: 숫자 카드2 JAVA
방구석개발자
2021. 4. 3. 22:25
반응형
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception{
int N,M=0;
int [] plusNum=new int[10000001];
int [] minusNum=new int[10000001];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
String[]strNum=br.readLine().split(" ");
M = Integer.parseInt(br.readLine());
String[]strmNum=br.readLine().split(" ");
StringBuilder sb = new StringBuilder();
for(int i=0;i<N;i++) { //카운트 정렬 세팅
int chk=Integer.parseInt(strNum[i]);
if(chk>=0) {
plusNum[chk]++;
}else {
minusNum[chk*(-1)]++;
}
}
for(int i=0;i<M;i++) {
int chk=Integer.parseInt(strmNum[i]);
if(chk>=0) {
sb.append(plusNum[chk]+" ");
}else {
sb.append(minusNum[chk*(-1)]+" ");
}
}
System.out.print(sb);
}
}
카운트 정렬을 이용하여 풀었습니다.
배열을 두개로 만들지 않고 한개로 만들어서 구현하면 더 좋을거 같습니다.
반응형