namespace BOJ
{
class No_1092
{
static void Main()
{
int craneCount = InputToInt(); // 최대 50
List<int> craneList = InputToList(); // 크레인 리스트
int boxCount = InputToInt(); // 최대 10,000
List<int> boxList = InputToList(); // 박스 리스트
// 내림차순 정렬
craneList.Sort(Comparer<int>.Create((x, y) => y - x));
boxList.Sort(Comparer<int>.Create((x, y) => y - x));
// 첫 요소 비교
if (boxList.First() > craneList.First())
{
// 박스가 크레인보다 크면 옮길 수 없음
Console.WriteLine("-1");
return;
}
int answer = CalculateRounds(craneList, boxList);
Console.WriteLine(answer);
}
/// <summary> 몇번 옮겨야 되는지 반환하는 함수 </summary>
static int CalculateRounds(List<int> craneList, List<int> boxList)
{
// 총 옮기는 횟수
int rounds = 0;
while (boxList.Count > 0)
{
rounds++;
for (int craneIndex = 0; craneIndex < craneList.Count; craneIndex++)
{
int boxIndex = FindHeaviestBox(craneList[craneIndex], boxList);
if (boxIndex != -1)
{
boxList.RemoveAt(boxIndex);
}
}
}
return rounds;
}
/// <summary> 해당 크레인으로 옮길 수 있는 최대 무게의 상자 인덱스 반환 </summary>
static int FindHeaviestBox(int craneCapacity, List<int> boxList)
{
for (int i = 0; i < boxList.Count; i++)
{
if (craneCapacity >= boxList[i])
{
return i;
}
}
return -1;
}
static int InputToInt() => int.Parse(Console.ReadLine());
static List<int> InputToList() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse).ToList();
}
}
틀림
=> -1
을 고려하지 않음
ArgumentOutofRange Error
, 시간 초과
=> 반복문을 작성시 boxList.Count로 하지않고 boxCount로 입력해서 시간초과가 난 것같다. (boxList의 크기는 변화하지만 반복문에 적용하지 않음)
그리디 알고리즘
정렬