Skip to content

Commit

Permalink
#16 10816번 숫자 카드2
Browse files Browse the repository at this point in the history
  • Loading branch information
leejw-lu committed Dec 23, 2023
1 parent cbf9117 commit 729753f
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions 이분탐색/숫자카드2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package 이분탐색;
import java.io.*;
import java.util.*;
//10816번 숫자 카드2
public class 숫자카드2 {
public static void main(String[] args) throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb= new StringBuilder();

int n= Integer.parseInt(br.readLine());
int[] arr= new int[n]; //현재 가지고 있는
StringTokenizer st= new StringTokenizer(br.readLine());
for (int i=0;i<n;i++)
arr[i]=Integer.parseInt(st.nextToken());
Arrays.sort(arr);

int m= Integer.parseInt(br.readLine());
st= new StringTokenizer(br.readLine());
for (int i=0;i<m;i++){
int key= Integer.parseInt(st.nextToken());
sb.append(upperBound(arr,key)- lowerBound(arr,key)).append(' ');
}
System.out.println(sb);
}

private static int lowerBound(int[] arr, int key) {
int left=0;
int right=arr.length;

while(left<right){
int mid=(left+right)/2;
if (key<=arr[mid]) right=mid;
else left=mid+1;
}
return left;
}

private static int upperBound(int[] arr, int key) {
int left=0;
int right=arr.length;

while(left<right){
int mid=(left+right)/2;
if (key<arr[mid]) right=mid;
else left=mid+1;
}
return left;
}

}

0 comments on commit 729753f

Please sign in to comment.