가장 짧은 막대로 잘라나가다가 막대가 남지 않아을 때까지 컷팅되는 수를 구할 것.
해결방법.
받아온 스틱길이(arr배열)을 리스트화해서 남은 수가 0이 될 떄까지 반복하는 while를 돌려준다.
잘라나갈 사이즈를 스틱리스트에서 가장 작은 수로 계속 교체해나가면서 값을 감소시켜 0이 된 값은 지워버린다.
감소 시킬때마다 카운트를 증가시켜준다.
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution
{
// Complete the cutTheSticks function below.
static int[] cutTheSticks(int[] arr)
{
List<int> _arr = new List<int>(arr);
List<int> cut = new List<int>();
while (_arr.Count > 0)
{
int cutsize = _arr.Min();
int count = 0;
for (int i = _arr.Count - 1; i >= 0; i--)
{
_arr[i] -= cutsize;
if (_arr[i] == 0)
_arr.RemoveAt(i);
count += 1;
}
cut.Add(count);
}
return cut.ToArray();
}
static void Main(string[] args)
{
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int n = Convert.ToInt32(Console.ReadLine());
int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt32(arrTemp))
;
int[] result = cutTheSticks(arr);
textWriter.WriteLine(string.Join("\n", result));
textWriter.Flush();
textWriter.Close();
}
}