90 likes | 214 Views
CS100-Stacks & Queues Part 2. As you arrive, continue looking at the non-recursive isAncestor problem from last time. You’ll be seeing more problems like it in class today. What You Will Do Today. You will solve more problems that involve “deferred work”
E N D
CS100-Stacks & Queues Part 2 As you arrive, continue looking at the non-recursive isAncestor problem from last time. You’ll be seeing more problems like it in class today.
What You Will Do Today • You will solve more problems that involve “deferred work” • You will be able to explain how stacks and queues differ in solving deferred work problems • You will be exposed to the specialized names of functions in stacks (push, pop, peek) – you need to memorize these
Similar Problem: Number Game The number gamestarts with a particular number. You win the game if you can reduce the number to 0 using only the operations allowed in the game. The operations allowed are: Subtract 7 Subtract 9 Subtract 20 Write a function isWinnable to determine if the game is winnable for a given starting value.
Number Game 2 The number gamestarts with a particular number. You win the game if you can reduce the number to 0 using only the operations allowed in the game. The operations allowed are: Subtract 3 Divide by 2 (only allowed if even) Subtract 20 Write a function isWinnable to determine if the game is winnable for a given starting value. For this problem (and the other problems in today) don’t use recursion
Number Game 3: Good Move The number gamestarts with a particular number. You win the game if you can reduce the number to 0 using only the operations allowed in the game. The operations allowed are: Subtract 3 Subtract -1 (a.k.a. “add 1”) Subtract 7 Write a function to determine a good move. A good move is one that can win the game in the minimum number of moves. e.g. (20 can be -1+7+7+7 or 3+3+3+3+3+3+3+-1…or others) But goodMove3(20) could return -1 or 7 but not 3, because -1+7+7+7 is the shortest solution
Queue • It’s when you add to the end of a list but remove from the beginning • Java does have a Queue interface, but in this class we’ll just use a LinkedList • Sometimes adding to a queue and removing from a queue are called “enqueue” and “dequeue” • Particularly useful when you want to do something in the minimum number of steps, because smaller step counts always happen earlier
Number Game 2: States List Subtract 3 Divide by 2 (only allowed if even) Subtract 20 Write a function statesList2 that returns a list of game states that win the game in a minimum number of moves. e.g. 32’s state list looks like [32, 12, 6, 3, 0]. Hint: you’ll want to keep the list of states in your “to do”, rather than nums. Challenge: write MovesList that returns the winning moves instead (e.g. [20,2,2,3] for 32) After completing this, please submit your code via ambient.
Stack • A stack is a list that you add to the beginning and remove from the beginning. We call the beginning the “top” of the stack • Java has a stack class, which we will use (although we could really just use a list) • Stacks have special names: • push(element) puts a new element on the top of the stack • pop() removes an element from the top of the stack • peek() gets the element at the top of the stack without removing it • Often useful in “tree like” problems where things have subthings, which may have still more sub things
Matching Parens • You’ve got a string consisting only of ‘(‘ and ‘)’ and ‘[‘ and ‘]’ • You want to know of the parents “match”. That is, every opening paren has a closing paren of the same type, and they are in the right order. For example: • “()” “([])” “[()]” “([]())” “()[]” match • “)” “(]” “)(“ “()()(“ “((())” do not match