Skip to content

Commit df5a6ec

Browse files
authored
Create 04 - Space Optimized | DP | Approach.cpp
1 parent ce0b4fd commit df5a6ec

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class Solution {
2+
public:
3+
// Helper function to solve the problem of picking non-adjacent slices
4+
int solve(vector<int>& slices){
5+
int k = slices.size(); // Total number of slices (3 * n)
6+
7+
// Initialize 3 arrays for dynamic programming:
8+
// prev, curr, and next are used to store the results of previous, current, and next steps, respectively.
9+
vector<int> prev(k+2, 0); // To store the results of the previous step for dp
10+
vector<int> curr(k+2, 0); // To store the results of the current step for dp
11+
vector<int> next(k+2, 0); // To store the results of the next step for dp
12+
13+
// DP for case 1 (excluding the last slice)
14+
for (int index = k - 2; index >= 0; index--) { // Iterate backwards through the slices (0 to k-2)
15+
for (int n = 1; n <= k / 3; n++) { // Iterate through the number of slices left to pick
16+
// Option 1: Take the current slice and move 2 steps forward (to avoid picking adjacent slices)
17+
int takeSlice = slices[index] + next[n - 1];
18+
19+
// Option 2: Skip the current slice and move 1 step forward
20+
int noTakeSlice = 0 + curr[n];
21+
22+
// Store the maximum of the two options (take or skip)
23+
prev[n] = max(takeSlice, noTakeSlice);
24+
}
25+
// Update the dp arrays by shifting
26+
next = curr; // Move current to next
27+
curr = prev; // Move previous to current
28+
}
29+
30+
// Store the result for case 1, excluding the last slice
31+
int case1 = curr[k / 3]; // We start from index 0 and need to pick k/3 slices
32+
33+
// DP for case 2 (excluding the first slice)
34+
vector<int> prev1(k+2, 0); // To store the results for the second case
35+
vector<int> curr1(k+2, 0); // To store the current results for the second case
36+
vector<int> next1(k+2, 0); // To store the next step results for the second case
37+
38+
// Iterate through slices for case 2, starting from index 1 and excluding the first slice
39+
for (int index = k - 1; index >= 1; index--) { // Iterate backwards through the slices (1 to k-1)
40+
for (int n = 1; n <= k / 3; n++) { // Iterate through the number of slices left to pick
41+
// Option 1: Take the current slice and move 2 steps forward
42+
int takeSlice = slices[index] + next1[n - 1];
43+
44+
// Option 2: Skip the current slice and move 1 step forward
45+
int noTakeSlice = 0 + curr1[n];
46+
47+
// Store the maximum of the two options (take or skip)
48+
prev1[n] = max(takeSlice, noTakeSlice);
49+
}
50+
// Update the dp arrays by shifting
51+
next1 = curr1; // Move current to next
52+
curr1 = prev1; // Move previous to current
53+
}
54+
55+
// Store the result for case 2, excluding the first slice
56+
int case2 = curr1[k / 3]; // We start from index 1 and need to pick k/3 slices
57+
58+
// Return the maximum of the two cases
59+
return max(case1, case2);
60+
}
61+
62+
// Main function to calculate the maximum sum of slices
63+
int maxSizeSlices(vector<int>& slices) {
64+
return solve(slices); // Call the solve function to compute the result
65+
}
66+
};

0 commit comments

Comments
 (0)