Skip to content

Commit 7e0a144

Browse files
authored
Merge pull request #1711 from akgmage/dev
add juice bottling in cpp
2 parents ae24f85 + 6812f5f commit 7e0a144

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
3+
4+
Explanation:
5+
6+
1. The function `JuiceBottling` takes an array `prices` as input, where `prices[i]` represents the price of a juice bottle of size `i`.
7+
8+
2. It initializes two arrays, `maxProfit` and `dividingPoints`, both of size `numSizes` (the number of bottle sizes).
9+
These arrays will be used to store information about maximum profit and dividing points.
10+
11+
3. The outer loop iterates through each possible bottle size, from 0 to `numSizes - 1`.
12+
13+
4. The inner loop iterates through possible dividing points for the current bottle size. For each combination of bottle size
14+
and dividing point, it calculates the possible profit by adding the maximum profit from the previous bottle sizes and the
15+
price of the bottle at the current dividing point.
16+
17+
5. If the calculated possible profit is greater than the current maximum profit for the bottle size, it updates both `maxProfit`
18+
and `dividingPoints` arrays.
19+
20+
6. After completing the loops, the function reconstructs the solution by backtracking from the last bottle size to the first.
21+
It appends the recorded dividing points to the `solution` array, which represents the optimal way to divide the bottles.
22+
23+
7. The function returns the `solution` array, which contains the indices of the dividing points that maximize profit.
24+
25+
In summary, the code uses dynamic programming to determine the optimal division of juice bottles to maximize profit.
26+
It calculates the maximum profit for each bottle size and keeps track of the dividing points that lead to the maximum profit.
27+
The solution is then reconstructed by backtracking from the end using the recorded dividing points.
28+
*/
29+
30+
#include <iostream>
31+
#include <vector>
32+
33+
std::vector<int> juiceBottling(std::vector<int>& prices) {
34+
int numSizes = prices.size();
35+
std::vector<int> maxProfit(numSizes); // Vector to store the maximum profit for each bottle size
36+
std::vector<int> dividingPoints(numSizes); // Vector to store the dividing points that maximize profit
37+
38+
// Loop through each bottle size
39+
for (int size = 0; size < numSizes; size++) {
40+
// Loop through possible dividing points for the current size
41+
for (int dividingPoint = 0; dividingPoint < size + 1; dividingPoint++) {
42+
// Calculate the possible profit by combining the previous maximum profit
43+
// with the price at the current dividing point
44+
int possibleProfit = maxProfit[size - dividingPoint] + prices[dividingPoint];
45+
46+
// Update maxProfit and dividingPoints if the new possible profit is greater
47+
if (possibleProfit > maxProfit[size]) {
48+
maxProfit[size] = possibleProfit;
49+
dividingPoints[size] = dividingPoint;
50+
}
51+
}
52+
}
53+
54+
std::vector<int> solution;
55+
int currentDividingPoint = numSizes - 1;
56+
// Reconstruct the solution by tracing back from the end
57+
// using the dividing points information
58+
while (currentDividingPoint > 0) {
59+
solution.push_back(dividingPoints[currentDividingPoint]);
60+
currentDividingPoint -= dividingPoints[currentDividingPoint];
61+
}
62+
return solution;
63+
}
64+
65+
int main() {
66+
std::vector<int> prices = {3, 5, 8, 9, 10, 17, 17, 20};
67+
std::vector<int> result = juiceBottling(prices);
68+
69+
std::cout << "Dividing Points for Maximum Profit:";
70+
for (int point : result) {
71+
std::cout << " " << point;
72+
}
73+
std::cout << std::endl;
74+
75+
return 0;
76+
}

0 commit comments

Comments
 (0)