Public void main
This presentation is the property of its rightful owner.
Sponsored Links
1 / 14

public void main PowerPoint PPT Presentation


  • 52 Views
  • Uploaded on
  • Presentation posted in: General

public void main. What do you call something that’s not static?. 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)

Download Presentation

public void main

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Public void main

public void main

What do you call something that’s not static?


Potw prizes

PotW Prizes

The 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


Potw winners d

PotW Winners =D

  • 5th place – Julia Huang

  • 4th place – Steven Hao

  • 3rd place – QingqiZeng

  • 2nd place – Tony Jiang

  • 1st place –

  • A winner is you!

/* Johnny Ho */

Johnny Ho

James Hong


Dynamic programming

Dynamic Programming

  • Basically, it's magic.

    • Whenever you have a recursive method that takes a certain set of parameters

      • Return value must be uniquely determined by this set of parameters

    • “Memoize” the return value of the method for that set of parameters using an array or hash table

    • Programs can often be sped up by exponential factors using this method


Fibonacci na ve

Fibonacci (naïve)

int fib(inti) { // assumes i >= 0if (i == 0) return 0;if (i == 1) return 1;return (fib(i - 1) + fib(i - 2)) % MOD;

}

  • Very easy to understand

  • Incredibly slow

    • Exponential time on i


Fibonacci recursive

Fibonacci (recursive)

int[] mem = new int[MAX_NUM];Arrays.fill(mem, -1);int fib(inti) { // assumes i >= 0if (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


Fibonacci iterative

Fibonacci (iterative)

int[] mem = new int[MAX_NUM];mem[0] = 0;mem[1] = 1;for (inti = 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!


Binomial coefficient recursion in 2d

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 >= 0if (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;

}


Knapsack

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

    • This is known as the 0-1 knapsack problem

      • Each object is either not chosen or chosen

    • 2D DP over index of current object and currently used volume

  • Reconstructing the solution

    • Which objects should I pick?

    • References to previous states should be saved


More examples

More Examples

  • Maximal sum paths

    • 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

    • Given a sequence of numbers, find the longest strictly increasing (not necessarily consecutive) subsequence

  • Longest common subsequence

    • Given two sequences of numbers, find the longest common (not necessarily consecutive) subsequence

  • Edit (Levenshtein) distance

    • If you can erase/insert/swap characters of a string A, how many operations do you need to reach string B


February usaco

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!


Potw magic feed machine

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.


Magic feed machine cont

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)


Potw hints

PotW Hints

  • Dynamic programming! (surprise surprise)

  • Note that each operation (substring flip) is equivalent to:

    • Two suffix flips

    • Count suffix flips instead of substring flips

    • Thus, you can perform up to 2*K suffix flips

  • Recurse over:

    • the index of the current light

    • 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)


  • Login