Download presentation
Presentation is loading. Please wait.
1
Sit-In Lab 4 Queues & Stacks © Nicholas Lum
2
Cake Queues
3
Cake Your friend baked a long cake and cut it into pieces
Some pieces have raisins, which you hate You can only take home a contiguous slice of pieces You want to maximise the chocolate on your slice But you are limited to a certain number of raisin pieces Input: N – number of pieces of cake C – number of raisins pieces allowed Followed by N lines of: T – state of the piece (raisin or not) X – amount of chocolate on that piece Output the maximum amount of chocolate you can gain
4
Ask yourself… Store the original cake in what data structure?
Take one piece of cake at a time and check it If there is no raisin, then no problem If there is a raisin, are we allowed to simply take it? How to compare between different slices of cake? How to keep track of previous amounts of chocolate?
5
Visualisation 1 2 10 2 3 4 5 Current Chocs 0 Max Chocs 0
Legend Green == No Raisin Orange == Has Raisin Number inside == num of Choc Original Cake 1 2 10 2 3 4 5 Current Chocs 0 Max Chocs 0 Raisin Pieces Taken 0 Max Raisin Pieces 2
6
Visualisation 2 10 2 3 4 5 1 Only chocolate, so just take it
Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake 2 10 2 3 4 5 Only chocolate, so just take it Current Chocs 1 Max Chocs 0 Raisin Pieces Taken 0 Max Raisin Pieces 2 1 Potential Take Home Slice
7
Visualisation 10 2 3 4 5 1 2 Only chocolate, so just take it
Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake 10 2 3 4 5 Only chocolate, so just take it Current Chocs 3 Max Chocs 0 Raisin Pieces Taken 0 Max Raisin Pieces 2 1 2 Potential Take Home Slice
8
Visualisation Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake 2 3 4 5 Raisin piece. We still have leeway to take it Current Chocs 13 Max Chocs 0 Raisin Pieces Taken 1 Max Raisin Pieces 2 1 2 10 Potential Take Home Slice
9
Visualisation 3 4 5 1 2 10 2 Only chocolate, so just take it
Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake 3 4 5 Only chocolate, so just take it Current Chocs 15 Max Chocs 0 Raisin Pieces Taken 1 Max Raisin Pieces 2 1 2 10 2 Potential Take Home Slice
10
Visualisation Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake 4 5 Raisin piece. We still have leeway to take it. But it’s the last one we can have in this slice Current Chocs 18 Max Chocs 0 Raisin Pieces Taken 2 Max Raisin Pieces 2 1 2 10 2 3 Potential Take Home Slice
11
Visualisation 5 1 2 10 2 3 4 Only chocolate, so just take it
Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake 5 Only chocolate, so just take it Current Chocs 22 Max Chocs 0 Raisin Pieces Taken 2 Max Raisin Pieces 2 1 2 10 2 3 4 Potential Take Home Slice
12
Visualisation Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake 5 Raisin piece. We don’t have leeway to take it. What we have here is a potential take-home slice with a certain amount of chocolate Current Chocs 22 Max Chocs 0 Raisin Pieces Taken 2 Max Raisin Pieces 2 1 2 10 2 3 4 Potential Take Home Slice
13
Visualisation Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake 5 Compare this slice against our max chocolates so far and record the higher value Current Chocs 22 Max Chocs 22 Raisin Pieces Taken 2 Max Raisin Pieces 2 1 2 10 2 3 4 Potential Take Home Slice
14
Visualisation Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake 5 Now we need to remove the first raisin piece so that we can consider the new piece. Current Chocs 22 Max Chocs 22 Raisin Pieces Taken 2 Max Raisin Pieces 2 1 2 10 2 3 4 Potential Take Home Slice
15
Visualisation Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake 5 To exclude the first raisin piece, we need to also exclude all pieces that come before it. Current Chocs 22 Max Chocs 22 Raisin Pieces Taken 2 Max Raisin Pieces 2 1 2 10 2 3 4 Potential Take Home Slice
16
Visualisation Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake 5 To exclude the first raisin piece, we need to also exclude all pieces that come before it. Current Chocs 21 Max Chocs 22 Raisin Pieces Taken 2 Max Raisin Pieces 2 2 10 2 3 4 Potential Take Home Slice
17
Visualisation Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake 5 To exclude the first raisin piece, we need to also exclude all pieces that come before it. Current Chocs 19 Max Chocs 22 Raisin Pieces Taken 2 Max Raisin Pieces 2 10 2 3 4 Potential Take Home Slice
18
Visualisation Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake 5 Now that we have excluded the first raisin piece, we have space for the new raisin piece. Current Chocs 9 Max Chocs 22 Raisin Pieces Taken 1 Max Raisin Pieces 2 2 3 4 Potential Take Home Slice
19
Visualisation Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake Take the raisin piece and add its chocolate amount Current Chocs 14 Max Chocs 22 Raisin Pieces Taken 2 Max Raisin Pieces 2 2 3 4 5 Potential Take Home Slice
20
Visualisation Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake We have checked all the slices in the original cake. Now we have another potential take-home slice. Current Chocs 14 Max Chocs 22 Raisin Pieces Taken 2 Max Raisin Pieces 2 2 3 4 5 Potential Take Home Slice
21
Visualisation Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake Compare the amount of chocolate in this slice with the max chocolate so far. There is no change in the max. Current Chocs 14 Max Chocs 22 Raisin Pieces Taken 2 Max Raisin Pieces 2 2 3 4 5 Potential Take Home Slice
22
Visualisation Max Chocs 22 2 3 4 5
Legend Green == No Raisin Orange == Has Raisin White == Taken Number inside == num of Choc Original Cake Since the cake has been fully checked, we just return the max chocolate amount, 22. Current Chocs 14 Max Chocs 22 Raisin Pieces Taken 2 Max Raisin Pieces 2 2 3 4 5 Potential Take Home Slice
23
Cake class Main method: Why queue?
Read in the inputs N and C For each of the N subsequent lines: Read in T and X Construct a Piece object that encapsulates these information Store the Piece objects in a queue Solve the question and print the output Why queue? FIFO We want to consider the Pieces in the same order that they were read in Can we use Stack instead? Why or why not?
24
Piece class The encapsulation of the information on each piece
Attributes: raisin – boolean amountChocolate – int Getters: hasRaisin() getChocolate()
25
Algorithm Sketch (1) Refer to the diagrams when tracing this algorithm
Keep track of the following variables: The allowed number of raisins Given in the question. This does not change Another Queue to hold the take-home slice Why queue? Because we need the FIFO property! Max chocolate amount so far Updated before we consider a new take-home slice Current chocolate amount in the take-home slice Kept updated with every piece taken or removed from the slice Raisin count in the take-home slice
26
Algorithm Sketch (2) Repeat until the cake queue is empty:
Dequeue the next cake piece If it has raisins: Increment the raisin count If we exceeded the allowed number of raisins: Compare the current chocolate against the max chocolate and take the higher value as the new max: use Math.max(a, b) Remove all pieces up to and including the first raisin piece from the take home slice. Decrement the current chocolate while removing. Reduce the raisin count by 1 after removing. Regardless of whether it has raisins: Increment the current chocolate amount with the piece’s amount Enqueue the piece into the take-home slice Finally return the higher value between the max chocolate and the current chocolate
27
Questions? Cake
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.