Download presentation
Presentation is loading. Please wait.
Published byCrystal Casey Modified over 8 years ago
1
NAME THAT ALGORITHM #2 HERE ARE SOME PROBLEMS. SOLVE THEM. GL HF
2
DROP DAT EGG You have N identical eggs, and an H story building. You want to determine the highest floor from which you can drop an egg before it breaks. You may continue to re-use an egg until it breaks. What is the minimum number of total drops necessary to determine this?
3
SOLUTION Dynamic Programming, O(ND), where D is the total number of drops and is on the order of the Nth root of H.
4
GET DAT VOLUME You are given the faces of a three-dimensional solid, all of whose edges are parallel to the coordinate axes. Determine the volume of this solid.
5
SOLUTION Only take faces parallel to one of the three coordinate planes, then sort by the remaining coordinate and determine orientation.
6
RETURN DAT GRID You are given an NxN grid, filled with 0s and 1s. Return an NxN grid where the entry at (i,j) represents the shortest distance from (i,j) to a 1.
7
SOLUTION O(N^2), do shortest distance for each column separately and use a stack to figure out what row is optimal to use at each row in the column.
8
PLAY DAT GAME You have a circular game board with N squares. Each spot on the board is marked with an integral point value, and landing at that spot causes you to gain that many points. You start at square 0, and on each turn you can either quit the game or roll a 6-sided die, and go forward the number of squares that appear on the die. Given that the sum of the point values is negative, determine your maximum possible expected value.
9
SOLUTION O(NC) iterative process, based on Dynamic Programming. Assume that all squares other than the current one are correct, then update that square. The algorithm should eventually converge, so you could either prove a bound on the convergence or just run it for as many iterations as possible.
10
RANK DOSE GUESTS A number of guests want to stay at a hotel, and each guest has ranked the hotel rooms in order of preference. Minimize the maximally ranked hotel room that a guest has to stay in (a guest would prefer to stay in a hotel room that is ranked lower).
11
SOLUTION net flow with iterative edge additions, at the ith each stage add in the edges corresponding to the ith ranked hotel room, stop when you get a flow equal to the number of guests.
12
GIT DAT PROGRESSION Given a set of N integers, determine the first instance of an arithmetic progression of length 3 among them, if one exists.
13
SOLUTION O(N^2), loop through all possibilities of the first integer in the progression, then compute the second and third simultaneously by incrementing the third position as becomes necessary to keep up with the second position.
14
IMPLEMENT DOSE EQUATIONS Starting with the equation x = 0, we can perform two operations: (i) Change the left hand side (LHS) to |LHS - k| for some integer k, (ii) ask for the number of solutions to the equation. Write a program that can implement both operations arbitrarily, given that the range of solutions will always remain within [-1000,1000].
15
SOLUTION Just keep a list and array of all the solutions, should be O(N^2), where N = range of solutions = 2000 in this case.
16
COUNT DOSE PATHS Given a graph, determine the number of shortest paths.
17
SOLUTION Just do a normal dijkstra where you keep track of the number of paths at each point as well.
18
FIND DOSE DIGITS Find the last 6 digits of n choose k for many different n,k <= 1000.
19
SOLUTION DP on pascal's identity.
20
GIT DOSE STRINGS Find the number of binary strings of length N not containing a substring of length K.
21
SOLUTION O(NK) DP.
22
DUDE THIS IS HARD Given a set of integers, determine the longest arithmetic progression among them.
23
SOLUTION Solution: sort them and take the differences between consecutive terms, the problem becomes to determine how to partition them into contiguous sets with equal sums. The naieve solution would be to loop through every possible starting place, and then try all guys to take as the next term in the progression, and make the progression as long as possible. This is O(N^3). We can improve upon this by marking off when we have already used a progression as part of another progression that started earlier, so that the total runtime becomes O(number of progressions * average length of progression * average time to find next term). Since the average time to find the next term is O(logN) with a binary search, and each term can be in at most N progressions, this limits our runtime to O(N^2logN).
24
GIT DOSE INTS Given a list of distinct, unsorted integers, return the number of elements in the list that are binary searchable. An element is binary searchable if, regardless of how the pivot is chosen, the element will be found by the binary search.
25
SOLUTION O(N^2), We note that an element X is not binary searchable iff there is an element before X larger than it or if there is an element after X smaller than it. It then becomes trivial. (You can DP it but it's a lot more awkward).
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.