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
| using System;
using System.Linq;
using System.Collections.Generic;
public class Solution {
public int solution(int[] A, int[] B)
{
int answer = 0;
SortedDictionary<int, int> dictA = new SortedDictionary<int, int>();
SortedSet<int> keys = new SortedSet<int>();
for (int i = 0; i < B.Length; i++)
{
if (dictA.ContainsKey(B[i]))
dictA[B[i]]++;
else
dictA.Add(B[i], 1);
keys.Add(B[i]);
}
for (int i = 0; i < A.Length; i++)
{
if (dictA.Count == 0) break;
int key;
// A[i]보다 큰 값 찾기
var view = keys.GetViewBetween(A[i] + 1, int.MaxValue);
if (view.Count > 0)
{
key = view.Min;
answer++;
}
else
key = keys.Min; // A[i]보다 큰 값이 없으면 가장 작은 값 대입
if (dictA[key] == 1)
{
dictA.Remove(key);
keys.Remove(key);
}
else
dictA[key]--;
}
return answer;
}
}
|