Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
knapsack
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static int KnapSack(int capacity, int[] weight, int[] value, int itemsCount) { int[,] K = new int[itemsCount + 1, capacity + 1]; for (int i = 0; i <= itemsCount; ++i) { for (int w = 0; w <= capacity; ++w) { if (i == 0 || w == 0) K[i, w] = 0; else if (weight[i - 1] <= w) K[i, w] = Math.Max(value[i - 1] + K[i - 1, w - weight[i - 1]], K[i - 1, w]); else K[i, w] = K[i - 1, w]; } } return K[itemsCount, capacity]; } public static void Main(string[] args) { int[] value = { 10, 50, 70 }; int[] weight = { 10, 20, 30 }; int capacity = 40; int itemsCount = 3; int result = KnapSack(capacity, weight, value, itemsCount); Console.WriteLine(result); } } }
run
|
edit
|
history
|
help
1
asxasxdf
Evo Ivana da budes fancy :D
group by
UTC to Ireland time convertion v1.1
Lazy exception caching
Test
see all properties of given ldap node
Find median in a stream
reverseandcheck
MVC Learning