본문 바로가기

알고리즘/프로그래머스

[프로그래머스] (C#) 코딩테스트 연습 - 과일 장수

반응형
using System;
using System.Linq;

public class Solution {
    public int solution(int k, int m, int[] score) {
        var boxCount = score.Length / m;
        var orderedScore = score.OrderByDescending(x => x).ToList();
        int sum = 0;
        for(int i = 0; i < boxCount; i++) {
            int a = orderedScore.Skip(m*i).Take(m).Min();
            int b = a * m;
            sum += b;
        }
        return sum;
    }
}

 

단순히 점수의 배열을 내림차순 또는 오름차순으로 정렬한 후, m개씩 Take하면서 곱해주면서 더해주면 되는 간단한 문제입니다.

 

 

사실 Linq안쓰고 System.Array의 Array 클래스들을 쓰면 됩니다...

https://learn.microsoft.com/ko-kr/dotnet/api/system.array?view=net-8.0&redirectedfrom=MSDN

 

Array Class (System)

Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays in the common language runtime.

learn.microsoft.com

 

반응형