Presentation is loading. Please wait.

Presentation is loading. Please wait.

Public void main What do you call something that’s not static?

Similar presentations


Presentation on theme: "Public void main What do you call something that’s not static?"— Presentation transcript:

1 public void main What do you call something that’s not static?

2 PotW Prizes The prizes: 5 th place - $5 Fry’s gift card 4 th place - $5 Fry’s gift card (and bragging rights over the 5 th place winner) 3 rd place - $10 Fry’s gift card (and bragging rights over the 4 th and 5 th place winners) 2 nd place - $15 Fry’s gift card (and bragging rights over the 3 rd, 4 th, and 5 th place winners) 1 st place - $15 Fry’s gift card, and A SURPRISE :O …and bragging rights

3 PotW Winners =D 5 th place – Julia Huang 4 th place – Steven Hao 3 rd place – Qingqi Zeng 2 nd place – Tony Jiang 1 st place – A winner is you! James Hong/* Johnny Ho */Johnny Ho

4 Dynamic Programming Basically, it's magic. o Whenever you have a recursive method that takes a certain set of parameters Return value must be uniquely determined by this set of parameters o “Memoize” the return value of the method for that set of parameters using an array or hash table o Programs can often be sped up by exponential factors using this method

5 Fibonacci (naïve) int fib(int i) { // assumes i >= 0 if (i == 0) return 0; if (i == 1) return 1; return (fib(i - 1) + fib(i - 2)) % MOD; } Very easy to understand Incredibly slow o Exponential time on i

6 Fibonacci (recursive) int[] mem = new int[MAX_NUM]; Arrays.fill(mem, -1); int fib(int i) { // assumes i >= 0 if (i == 0) return 0; if (i == 1) return 1; if (mem[i] != -1) return mem[i]; return mem[i] = (fib(i - 1) + fib(i - 2)) % MOD; } Elegant, understandable Risk overflowing stack memory

7 Fibonacci (iterative) int[] mem = new int[MAX_NUM]; mem[0] = 0; mem[1] = 1; for (int i = 2; i < MAX_NUM; ++i) { mem[i] = (mem[i - 1] + mem[i - 2]) % MOD; } Faster, no risk of overflowing stack memory Becomes less understandable for more complex problems Order of iteration matters!

8 Binomial Coefficient (recursion in 2D) int[][] mem = new int[MAX_NUM][MAX_NUM]; for (int[] arr: mem) { Arrays.fill(arr, -1); } int choose(int n, int k) { // assumes n, k >= 0 if (n == 0 || k == 0) return 1; if (mem[n][k] != -1) return mem[n][k]; return mem[n][k] = (choose(n - 1, k - 1) + choose(n - 1, k)) % MOD; }

9 Knapsack Given a set of objects, each with some volume and some value, maximize the value of the objects you can fit in a container of a certain volume o This is known as the 0-1 knapsack problem Each object is either not chosen or chosen o 2D DP over index of current object and currently used volume Reconstructing the solution o Which objects should I pick? o References to previous states should be saved

10 More Examples Maximal sum paths o Given a rectangle of numbers, find the largest sum of numbers you can achieve walking only right/down from the top-left corner to the bottom-right corner Longest increasing subsequence o Given a sequence of numbers, find the longest strictly increasing (not necessarily consecutive) subsequence Longest common subsequence o Given two sequences of numbers, find the longest common (not necessarily consecutive) subsequence Edit (Levenshtein) distance o If you can erase/insert/swap characters of a string A, how many operations do you need to reach string B

11 February USACO Is this coming weekend! (Feb. 3-6) Is worth 5 points of PotW credit! (as usual) Is something you should take! (as always) USACO is already halfway over!

12 PotW – Magic Feed Machine Bessie has figured out how to manipulate the amount of feed she gets. On the feeder, there are N lights in a row, and Bessie only has time for at most K operations. On each operation, she can take a continuous subsequence of the lights and flip their sign (lights that were on turn off, lamps that were off turn on). She will receive feed proportional to the number of lights that are on in the end. Tell her the optimal possible result. Worth 35 points.

13 Magic Feed Machine (cont.) Constraints: N < 1000 K < 500 Sample Input: 1(K) 100010(starting configuration: 1 = on, 0 = off) Sample Output: 5(111110 and 111101 both have 5 lights on)

14 PotW Hints Dynamic programming! (surprise surprise) Note that each operation (substring flip) is equivalent to: o Two suffix flips o Count suffix flips instead of substring flips o Thus, you can perform up to 2*K suffix flips Recurse over: o the index of the current light o how many suffix flips have been done so far Store this in a N by 2*K array Create a recursive function Overall algorithm is O(NK)


Download ppt "Public void main What do you call something that’s not static?"

Similar presentations


Ads by Google