Download presentation
1
CS1020E Lab 4 (Stack and Queue)
Problem 1 : Swinging Monkey Problem 2 : Alice
2
Problem 1 Swinging Monkey
3
Swinging Monkey Problem Description: Input: Output:
Monkey can swing from one tree to another directly as long as there is no tree in between that is taller than or have the same height as either one of the two trees Given the sequence of tree heights, determine the number of pair of trees that the Monkey can swing Input: First line is [INTEGER] N: number of trees Second line is N x [INTEGER]: sequence of height Output: Single line of [INTEGER]: #trees Monkey can swing
4
Swinging Monkey Output: 5 Illustration:
5 trees: 19m – 17m – 20m – 20m – 20m Output: 5
5
Swinging Monkey Discussions:
What is the simplest algorithm to solve this? Given two trees: check if the Monkey can jump i.e. Check if there are taller trees in between Loop for all tree pairs Algorithm: fun canSwing (i, j) { for(k=i+1 to j-1) do{ if(height[k] ≥ min(height[i], height[j]){ return false } } return true }
6
Swinging Monkey Observe:
Observation: 5 trees: 19m – 17m – 20m – 20m – 17m Observe: from the tree before the Blue tree, the Monkey cannot jump to the tree after the Blue tree
7
Swinging Monkey Idea: After we process tree i, we can forget all the trees before i that are shorter than i. What will be the property of the sequence of trees that are not forgotten? Decreasing Order from the tree with smallest index Why? Suppose it is not in decreasing sequence After we process Blue, we can forget Yellow Yellow would have been removed from the sequence
8
Swinging Monkey Property of the Sequence: Discussions:
Decreasing Order of Height At the start of the processing of the next tree, the Monkey can jump from all tree in the sequence to the next tree If the next tree is higher than some elements, these elements in the sequence cannot jump beyond the next tree Discussions: What Data Structure do we need to implement? Stack: why? Because we need to remember the previous elements in order
9
Problem 2 AlicE
10
Alice the Baker Problem Description: Limitation:
Given a list of pancakes which can be sweet on either side or both, given the operations: Flip the top X pancakes and add syrup to top Add new pancake without adding syrup to top Count the number of sweet pancakes Pancake is NOT sweet if none of the sides are sweet Otherwise, the pancake is sweet Limitation: Only use Stack/Queue to solve the problem STACK: Last In First Out QUEUE: First In First Out
11
Alice the Baker Input: Output:
First line is [INTEGER] N and [INTEGER] Q N = initial #pancakes; Q = #queries/operations Next Q-lines are [STRING] operations If operations is FLIP: contains another [INTEGER] index Else only the operations is present Output: Only output when COUNT operations is encountered Output: [INTEGER] <number of sweet pancakes> followed by newline character
12
Alice the Baker Simulation: Input: Output: 5 6 FLIP 3 ADD COUNT FLIP 4
2 5
13
Alice the Baker E D C B A Simulation: Input: Output: 5 6 FLIP 3 ADD
COUNT FLIP 4 Output: 2 5 C D E B A
14
Alice the Baker E D C B A Simulation: Input: Output: 5 6 FLIP 3 ADD
COUNT FLIP 4 Output: 2 5 C D E B A
15
Alice the Baker F C D E B A Simulation: Input: Output: 5 6 FLIP 3 ADD
COUNT FLIP 4 Output: 2 5 F C D E B A
16
Alice the Baker F C D E B A Simulation: Input: Output: 5 6 FLIP 3 ADD
COUNT FLIP 4 Output: 2 5 F C D E B A
17
Alice the Baker F C D E B A Simulation: Input: Output: 5 6 FLIP 3 ADD
COUNT FLIP 4 Output: 2 5 C D F E B A
18
Alice the Baker D C F E B A Simulation: Input: Output: 5 6 FLIP 3 ADD
COUNT FLIP 4 Output: 2 5 E C D F B A
19
Alice the Baker E F C D B A Simulation: Input: Output: 5 6 FLIP 3 ADD
COUNT FLIP 4 Output: 2 5 E F C D B A
20
Alice the Baker Discussions:
What are the data members of Pancake Class? Sweet Top [Value: Yes/No] Sweet Bottom [Value: Yes/No] Which Side on Top [Value: Top/Bottom] What are the operations required? Is top side sweet? Is bottom side sweet? Is any of the sides sweet? I want to flip this Pancake, and I don’t care how, whenever I ask for top-side, it is the real top-side (since top and bottom are reversed when flipping) NOTE: The last operation might affects the implementation of the previous two Can there be other operations combination?
21
Alice the Baker Discussions: How do you Store a Pile of Pancake?
Stack Why? How do you Flip a Pile of Pancakes? Use of auxiliary Queue to reverse the order of a Stack How do you Count the Pile of Pancakes in Stack? You cannot look into arbitrary position (violate Stack structure) You can only pop from the top, push to the top You cannot use any other data structure besides Stack/Queue Use of auxiliary Stack to preserve the order of a Stack
22
Stack/Queue Primer Stack: Queue: Last In First Out
Can be simulated using Linked List A restricted Linked List Insert only to the beginning (i.e. push = addFirst) Remove only from the beginning (i.e. pop = removeFirst) Queue: First in First Out Insert only to the end (i.e. enqueue = addLast) Remove only from the beginning (i.e. dequeue = removeFirst)
23
Stack/Queue Primer 1 2 3 4 5 Stack A Stack B Stack A
Remove all elements from Stack A Insert into B Remove all elements from Stack B Insert into A int size = A.size(); for (int i=0; i<size; i++) B.addFirst(A.removeFirst()); for (int i=0; i<size; i++) A.addFirst(B.removeFirst()); 1 2 3 4 5
24
Stack/Queue Primer 1 2 3 4 5 Stack A Queue B Stack A
Remove all elements from Stack A Insert into B Remove all elements from Queue B Insert into A int size = A.size(); for (int i=0; i<size; i++) B.addLast(A.removeFirst()); for (int i=0; i<size; i++) A.addFirst(B.removeFirst()); 1 2 3 4 5
25
Alice the Baker Algorithms: Flip the Pancake Pseudo-code:
function Flip (index) { flip using auxiliary Queue; FOR each element moved back to Stack DO { IF needs sweeten THEN sweeten; } add sweetener to top stack; }
26
Alice the Baker Algorithms: Add to the top of the Pancake Pseudo-code:
function Add() { push into Stack; } Count the number of Sweet Pancake Pseudo-code: function Count() { push into auxiliary Stack; FOR each element moved back to Stack DO { IF sweet THEN increment counter; } }
27
The End Any Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.