[알고리즘] 예산
![[no-alignment]](https://happyobo.github.io/assets/images/post-programmers/03.png)
구현 방식
- 이번에는 람다식을 이용해서 풀어보았다.
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;
}
}