diff --git a/README.md b/README.md index 492dd76..8bd8ff5 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [ContestID_W02]: https://contest.yandex.ru/contest/54016/?lang=en [ContestID_W03]: https://contest.yandex.ru/contest/54346/?lang=en [ContestID_W04]: https://contest.yandex.ru/contest/54765/?lang=en -[ContestID_W05]: https://contest.yandex.ru/contest//?lang=en +[ContestID_W05]: https://contest.yandex.ru/contest/55196/?lang=en [ContestID_W06]: https://contest.yandex.ru/contest//?lang=en [ContestID_W07]: https://contest.yandex.ru/contest//?lang=en [ContestID_W08]: https://contest.yandex.ru/contest//?lang=en @@ -17,7 +17,7 @@ [WarmUp_test_W02]: https://forms.gle/xdDXeFT1DCcB5myn7 [WarmUp_test_W03]: https://forms.gle/MrCR6BrWwGsKjqYx7 [WarmUp_test_W04]: https://forms.gle/Bp8e6XhezbHsiVWC8 -[WarmUp_test_W05]: https://forms.gle/ +[WarmUp_test_W05]: https://forms.gle/dWe6UWg4jmEAzt6v7 [WarmUp_test_W06]: https://forms.gle/ [WarmUp_test_W07]: https://forms.gle/ [WarmUp_test_W08]: https://forms.gle/ @@ -43,13 +43,13 @@ | 02 | Binary search | [Slides][Slides_W02] | [Test][WarmUp_test_W02] | [Contest][ContestID_W02] | 18.10.2023 19:00 UTC+3 | | 03 | Basic Data sturctures | [Slides][Slides_W03] | [Test][WarmUp_test_W03] | [Contest][ContestID_W03] | 25.10.2023 19:00 UTC+3 | | 04 | Dynamic programming | [Slides][Slides_W04] | [Test][WarmUp_test_W04] | [Contest][ContestID_W04] | 01.11.2023 19:00 UTC+3 | +| 05 | Knapsack problem | [Slides][Slides_W05] | [Test][WarmUp_test_W05] | [Contest][ContestID_W05] | 13.11.2023 19:00 UTC+3 | diff --git a/week05_knapsack/MSAI.2023.Algo.W05.slides.pdf b/week05_knapsack/MSAI.2023.Algo.W05.slides.pdf new file mode 100644 index 0000000..3e1d0e4 Binary files /dev/null and b/week05_knapsack/MSAI.2023.Algo.W05.slides.pdf differ diff --git a/week05_knapsack/knapsack.py b/week05_knapsack/knapsack.py new file mode 100644 index 0000000..b06b829 --- /dev/null +++ b/week05_knapsack/knapsack.py @@ -0,0 +1,47 @@ +W = 8 +N = 4 +wi = [3, 3, 5, 6] +ci = [3, 5, 10, 14] + +d = [[0] * (W + 1) for i in range(N + 1)] +for i in range(1, N + 1): + for w in range(1, W + 1): + if wi[i-1] > w: + d[i][w] = d[i-1][w] + else: + d[i][w] = max(d[i-1][w], d[i-1][w-wi[i-1]]+ci[i-1]) + +print('\n'.join([' '.join(map(str, d[i])) for i in range(N + 1)])) + + +# Python3 program to find maximum +# achievable value with a knapsack +# of weight W and multiple instances allowed. + +# Returns the maximum value +# with knapsack of W capacity +def unboundedKnapsack(W, n, val, wt): + # dp[i] is going to store maximum + # value with knapsack capacity i. + dp = [0 for i in range(W + 1)] + + ans = 0 + + # Fill dp[] using above recursive formula + for i in range(W + 1): + for j in range(n): + if (wt[j] <= i): + dp[i] = max(dp[i], dp[i - wt[j]] + val[j]) + print(dp) + return dp[W] + + +# Driver program +W = 5 +val = [10] +wt = [2] +n = len(val) + +print(unboundedKnapsack(W, n, val, wt)) + +# This code is contributed by Anant Agarwal. \ No newline at end of file