From b0d1ab9396e7264a3396ad501b4691c6286830ba Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Tue, 28 May 2024 23:50:02 +0530 Subject: [PATCH] Create 28 May Minimum cost to fill given weight in a bag --- 28 May Minimum cost to fill given weight in a bag | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 28 May Minimum cost to fill given weight in a bag diff --git a/28 May Minimum cost to fill given weight in a bag b/28 May Minimum cost to fill given weight in a bag new file mode 100644 index 00000000..ec1ecfa3 --- /dev/null +++ b/28 May Minimum cost to fill given weight in a bag @@ -0,0 +1,13 @@ +class Solution { +public: + int minimumCost(int n, int w, vector &cost) { + vector dp(w + 1, INT_MAX); + dp[0] = 0; + + for (int i = 0; i < n; i++) + for (int j = i + 1; j <= w; j++) + dp[j] = min(dp[j], cost[i] + dp[j - i - 1]); + + return dp[w]; + } +};