less than 1 minute read

[no-alignment]

구현 방식

  • 이번에는 람다식을 이용해서 풀어보았다. Count(Func<int,bool>) 을 이용했더니 for문을 쓰지 않아도 되어서 코드가 깔끔해졌다.
  • 앞으로 자주 써야겠다.
using System;
using System.Collections.Generic;
using System.Linq;

public class Solution {
    public int solution(int[] d, int budget)
    {
        List<int> dl = new List<int>();
        dl.AddRange(d);
        dl.Sort();
        int result = 0;
        int answer = dl.Count(
            x =>
            {
                if (result + x <= budget)
                {
                    result += x;
                    return true;
                }
                else
                    return false;
            }
            );
        return answer;
    }
}