포스트

숫자카드2(10816)

숫자카드2(10816)

기존에 HashMap을 이용하여 풀었던 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.*;
import java.util.*;

public class Main{
  public static void main(String[] args) throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    StringBuilder sb = new StringBuilder();
    
    int n = Integer.parseInt(br.readLine());
    StringTokenizer st = new StringTokenizer(br.readLine());

    int m = Integer.parseInt(br.readLine());
    StringTokenizer st2 = new StringTokenizer(br.readLine());

    Map<Integer, Integer> nMap = new HashMap<>();

    for(int i = 0; i < n; i++){
      int key = Integer.parseInt(st.nextToken());

      if(nMap.containsKey(key)) {
        nMap.put(key, nMap.get(key) + 1);
      } else {
        nMap.put(key, 1);
      }
    }
    
    while(m-- > 0){
      int key = Integer.parseInt(st2.nextToken());
      
      if(nMap.containsKey(key)) sb.append(nMap.get(key) + " ");
      else sb.append(0 + " ");
    }
    
    bw.write(sb + "");
    bw.flush();
    bw.close();
    br.close();
      
  }
}
  • 메모리 : 199332 KB
  • 시간 : 1260 ms

이분탐색을 이용한 풀이 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.io.*;
import java.util.*;

public class Main{
  public static void main(String[] args) throws IOException{
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
      StringBuilder sb = new StringBuilder();
  
      int n = Integer.parseInt(br.readLine());

      StringTokenizer st = new StringTokenizer(br.readLine());
      
      int[] nArr = new int[n];
      
      for(int i = 0; i < n; i++){
        nArr[i] = Integer.parseInt(st.nextToken());
      }
      
      int m = Integer.parseInt(br.readLine());
      
      st = new StringTokenizer(br.readLine());
      
      Arrays.sort(nArr);
      
      for(int i = 0; i < m; i++){
        int key = Integer.parseInt(st.nextToken());
        
        int stP = lowIndex(nArr, key);
        int enP = highIndex(nArr, key);
        
        sb.append(enP - stP + " ");
      }
      
      bw.write(sb + "");
      bw.flush();
      bw.close();
      br.close();
  }
  
  public static int lowIndex(int[] nArr, int key){
    int lo = 0;
    int hi = nArr.length;
    
    while(lo < hi){
      int mid = (lo + hi) / 2;
      
      if(key <= nArr[mid]){
        hi = mid;
      } else{
        lo = mid + 1;
      }
    }
    
    return lo;
  }
  
  public static int highIndex(int[] nArr, int key){
    int lo = 0;
    int hi = nArr.length;
    
    while(lo < hi){
      int mid = (lo + hi) / 2;
      
      if(key < nArr[mid]){
        hi = mid;
      } else {
        lo = mid + 1;
      }
    }
    
    return lo;
  }
}
  • 메모리 : 139460 KB
  • 시간 : 1600 ms