Skip to content

Commit 6183e0d

Browse files
committed
add juice bottling in java
1 parent c92dcec commit 6183e0d

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
import java.util.ArrayList;
31+
import java.util.List;
32+
33+
public class JuiceBottling {
34+
35+
public static List<Integer> juiceBottling(int[] prices) {
36+
int numSizes = prices.length;
37+
int[] maxProfit = new int[numSizes]; // Array to store the maximum profit for each bottle size
38+
int[] dividingPoints = new int[numSizes]; // Array to store the dividing points that maximize profit
39+
40+
// Loop through each bottle size
41+
for (int size = 0; size < numSizes; size++) {
42+
// Loop through possible dividing points for the current size
43+
for (int dividingPoint = 0; dividingPoint < size + 1; dividingPoint++) {
44+
// Calculate the possible profit by combining the previous maximum profit
45+
// with the price at the current dividing point
46+
int possibleProfit = maxProfit[size - dividingPoint] + prices[dividingPoint];
47+
48+
// Update maxProfit and dividingPoints if the new possible profit is greater
49+
if (possibleProfit > maxProfit[size]) {
50+
maxProfit[size] = possibleProfit;
51+
dividingPoints[size] = dividingPoint;
52+
}
53+
}
54+
}
55+
56+
List<Integer> solution = new ArrayList<>();
57+
int currentDividingPoint = numSizes - 1;
58+
// Reconstruct the solution by tracing back from the end
59+
// using the dividing points information
60+
while (currentDividingPoint > 0) {
61+
solution.add(dividingPoints[currentDividingPoint]);
62+
currentDividingPoint -= dividingPoints[currentDividingPoint];
63+
}
64+
return solution;
65+
}
66+
67+
public static void main(String[] args) {
68+
int[] prices = {3, 5, 8, 9, 10, 17, 17, 20};
69+
List<Integer> result = juiceBottling(prices);
70+
71+
System.out.println("Dividing Points for Maximum Profit: " + result);
72+
}
73+
}

0 commit comments

Comments
 (0)