비트를 쪼개는 개발자

allen321@naver.com

코딩테스트

C# [프로그래머스] Lv.1 과일 장수

MozarTnT 2023. 12. 28. 21:15
728x90
반응형

using System;
using System.Collections.Generic;
using System.Linq;

public class Solution
{
    public int solution(int k, int m, int[] score)
    {
        List<int> cartlist = score.ToList(); // score 값을 가져올 리스트
        cartlist.Sort(); // 정렬
        cartlist.Reverse(); // 높은 수부터 

        int answer = 0;

        for (int i = 0; i < cartlist.Count / m; i++) 
        {
            List<int> newlist = new List<int>(); 

            for (int j = 0; j < m; j++)
            {
                newlist.Add(cartlist[i * m + j]); // 높은 수부터 정렬한 리스트에서 m 만큼 자름
            }

            int min = newlist.Min(); // 새로 잘라온 리스트에서 낮은 값을 확인
            answer += m * min; // 이를 박스 포장 과 곱해주면 끝
        }
        return answer;
    }
}

 

728x90
반응형