C# algorithm - find least number of objects necessary

后端 未结 6 1050
孤街浪徒
孤街浪徒 2021-01-14 03:19

Let\'s say I have the following code.

var numberToGetTo = 60; 
var list = new[] {10, 20, 30, 40, 50};

I want to be able to return 50 &

相关标签:
6条回答
  • 2021-01-14 04:06

    This problem as currently stated is actually trivial. The easiest way to to get "equal or greater" than the target is to find the largest number A in the list, and stick it in the answer list N times, where N is the lowest N such that N * A > target.

    I suspect this is not what the original poster really wants however. If the problem is restated to somehow measure the "closeness" of various answers, and make a distinction as to whether answers that are "closer" or answers that have less numbers are "better" then it becomes tougher. For example, if the target is 100, is an answer of [55,55] better or worse than an answer of [20,20,20,20,20] ?

    0 讨论(0)
  • 2021-01-14 04:08

    Here's an implementation that uses Linq to be as clean as possible. It makes no attempt to optimize for performance over large inputs.

    I'm assuming that you wouldn't use this algorithm for large inputs anyway, since the problem is NP-Complete, and therefore clarity is the right goal. My algorithm is O(n^2), and recursive at that.

        static IEnumerable<int> Knapsack(IEnumerable<int> items, int goal)
        {
            var matches = from i in items
                          where i <= goal
                          let ia = new[] {i}
                          select i == goal ? ia : Knapsack(items, goal - i).Concat(ia);
    
            return matches.OrderBy(x => x.Count()).First();
        }
    
    0 讨论(0)
  • 2021-01-14 04:08

    This sounds similar to the Subset sum problem, which can be solved in a reasonable amount of time for smallish sets using dynamic programming. It's not an easy or common problem, so you won't find a helpful Linq extension method for it :)

    0 讨论(0)
  • 2021-01-14 04:12

    Knapsack Problem, this may give you a clue.

    http://en.wikipedia.org/wiki/Knapsack_problem

    I'd say that you could create a lambda expression containing the actual alogrithm, but you'll need to use C#. Using 'just linq' will not be enough.

    0 讨论(0)
  • 2021-01-14 04:15

    I just hacked this together and I'm sure someone could improve. But does something like this work?

    public class App
    {
        static void Main(string[] eventArgs)
        {
            var list = new[] {10, 20, 30, 40, 50};
            var whatYouNeed = GetWhatYouNeed(list, 60, 60);
            //what you need will contain 50 & 10
    
           //whatYouNeed = GetWhatYouNeed(list, 105,105);
            //what you need will contain (50,50, 10)
        }
    
        private static IList<int> _whatYouNeed = new List<int>();
    
    
        static IEnumerable<int> GetWhatYouNeed(IEnumerable<int> list, int goal, int amountRequired)
        {   //this will make sure 20 is taken over 10, if the goal is 15. highest wins
            var intYouNeed = list.OrderBy(x => Math.Abs(amountRequired - x)).FirstOrDefault(x => x > amountRequired);
            if (intYouNeed == 0) intYouNeed = list.OrderBy(x => Math.Abs(amountRequired - x)).FirstOrDefault();
            _whatYouNeed.Add(intYouNeed);
    
            if (_whatYouNeed.Sum() < goal)
            {
                int howMuchMoreDoYouNeed = goal - _whatYouNeed.Sum();
                GetWhatYouNeed(list, goal, howMuchMoreDoYouNeed);
            }
    
            return _whatYouNeed;
        }
    }
    

    I was a bit lazy passing in two values to GetWhatYouNeed but you get the point.

    0 讨论(0)
  • 2021-01-14 04:16

    This is an NP-complete problem called the knapsack problem. That means, that your best method is not going to be in polynomial time. You may have to brute-force a solution.

    alt text

    0 讨论(0)
提交回复
热议问题