ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 프로그래머스 - 광물 캐기
    제로부터 C# 코딩테스트 2023. 11. 17. 13:58
    728x90

    곡괭이와, 광물이 주어지고

    곡괭이로 광물들을 다 캐거나,

    더이상 광물을 캘 곡괭이가 없을때,

    작업을 끝내기 까지의 최소 피로도를 구하는 문제.

     

    필요 지식

    using System.Collections.Generic;

    List

    .Count

    Dictionary

    Math.celling()

    .GetRange()

    Math.Min()

    .Sort()

     

    (1).곡괭이의 최대 사용 횟수를 구합니다

    (2).곡괭이의 최대 사용수를 초과하는 광물은 더 이상 볼 필요가 없기때문에 잘라냅니다.

    (3).곡괭이를 한번 사용하면 피로도가 다 될때까지(5번), 중간에 곡괭이를 바꿔서 사용할수 없기 때문에 (2)번에서 잘라낸 광물배열의 /5번만 for문을 돌려주면 됩니다.

    (4).Dictionary 만들고 (2)번에서 만든 광물배열에서 5개씩 끊어서 Dictionary를 업데이트 시킨후  다이아 곡괭이를 사용했을때의 피로도와 은 곡괭이를 사용했을때 피로도, 돌곡괭이를 사용했을 때의 피로도를 배열로 resultArr에 넣습니다.

    (5).(4)번에서 만든 resultArr 을 돌곡괭이 기준으로 내림차순 해줍니다. 돌 곡괭이의 피로도 폭이 가장 크기 때문에 돌곡괭이를 사용했을때의 피로도가 가장 높을때 다이아곡괭이,은곡괭이를 사용해 줘야 합니다.

    (6). 마지막으로 resultArr 을 foreach 로 돌면서 다이아 곡괭이부터 차례로 확인하면서 피로도를 더해주면 됩니다.

    using System;
    using System.Collections.Generic;
    
    public class Solution {
        public int solution(int[] picks, string[] minerals) {
    
            int answer = 0;
    
            int maxPicksLen = 0;
            foreach(int i in picks){
                maxPicksLen += i *5;
            }
    
            List<string> mineralsList = new List<string>(minerals);
            mineralsList = mineralsList.GetRange(0,Math.Min(maxPicksLen,minerals.Length));
    
            int maxLen = (int)Math.Ceiling((float)mineralsList.Count /5);
    
            List<int[]> resultArr = new List<int[]>();
    
            for(int i =0;i<maxLen;i++){
             Dictionary<string,int> map = new Dictionary<string,int>(){
                 {"diamond", 0},
                 {"iron" , 0},
                 {"stone" , 0}
             };
    
                List<string> arr = mineralsList.GetRange(i*5,Math.Min(5,mineralsList.Count -i*5));
                foreach(string pic in arr){
                    map[pic]++;
                }
    
                resultArr.Add(new int[]{
                    map["diamond"]*1+map["iron"]*1 + map["stone"]*1,
                    map["diamond"]*5+map["iron"]*1 + map["stone"]*1,
                    map["diamond"]*25+map["iron"]*5 + map["stone"]*1,
                });
            }
    
            resultArr.Sort((a, b) => b[2] - a[2]);
    
            foreach (int[] e in resultArr)
            {
                if (picks[0] > 0)
                {
                    picks[0]--;
                    answer += e[0];
                }
                else if (picks[1] > 0)
                {
                    picks[1]--;
                    answer += e[1];
                }
                else if (picks[2] > 0)
                {
                    picks[2]--;
                    answer += e[2];
                }
            }
    
    
            return answer;
        }
    }
    728x90
Designed by Tistory.