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

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

Embed Size (px)

Citation preview

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

public void main

What do you call something that’s not static?

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

PotW PrizesThe prizes:• 5th place - $5 Fry’s gift card• 4th place - $5 Fry’s gift card (and bragging rights

over the 5th place winner)

• 3rd place - $10 Fry’s gift card (and bragging rights over the 4th and 5th place winners)

• 2nd place - $15 Fry’s gift card (and bragging rights over the 3rd, 4th, and 5th place winners)

• 1st place - $15 Fry’s gift card, and A SURPRISE :O

…and bragging rights

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

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

James Hong/* Johnny Ho */Johnny Ho

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

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 parameterso “Memoize” the return value of the method for that set of

parameters using an array or hash tableo Programs can often be sped up by exponential factors

using this method

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

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

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

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

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

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!

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

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;}

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

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 volumeo This is known as the 0-1 knapsack problem

• Each object is either not chosen or choseno 2D DP over index of current object and currently used

volume

• Reconstructing the solutiono Which objects should I pick?o References to previous states should be saved

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

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 subsequenceo Given a sequence of numbers, find the longest strictly

increasing (not necessarily consecutive) subsequence

• Longest common subsequenceo Given two sequences of numbers, find the longest

common (not necessarily consecutive) subsequence

• Edit (Levenshtein) distanceo If you can erase/insert/swap characters of a string A, how

many operations do you need to reach string B

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

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!

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

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.

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

Magic Feed Machine (cont.)

• Constraints:N < 1000K < 500

• Sample Input:1 (K)100010 (starting configuration: 1 = on, 0 = off)

• Sample Output:5 (111110 and 111101 both have 5 lights on)

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

PotW Hints• Dynamic programming! (surprise surprise)• Note that each operation (substring flip) is

equivalent to:o Two suffix flipso Count suffix flips instead of substring flipso Thus, you can perform up to 2*K suffix flips

• Recurse over:o the index of the current lighto 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)