Download presentation

Presentation is loading. Please wait.

Published byShane Corbitt Modified over 2 years ago

1
This title is orange because of Halloween. It is totally not because it’s always orange.

2
Last three weeks’ PotW! BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringBuffer s = new StringBuffer(in.readLine()); int count = 0; for ( ; s.length() > 1; count++) { if (s.charAt(s.length() - 1) == '0') // if even s.deleteCharAt(s.length() - 1); // remove last char else // if odd { int last0 = s.lastIndexOf("0"); if (last0 == -1) // no '0' remaining { System.out.println(count + s.length() + 1); // shortcut return; } // continued on next slide

3
PotW solution (cont.) else // there are still '0's remaining { s.setCharAt(last0, '1'); // replace last '0' with 1 last0++; // replace trailing '1's with '0' for ( ; last0 < s.length(); last0++) s.setCharAt(last0, '0'); } } // end else (if odd) } // end for loop if (s.charAt(0) == '0') count++; System.out.println(count);

4
PotW Solution (hax ver.) import java.util.Scanner; public class YoungBetsy { public static void main(String[] args) { Scanner s = new Scanner(System.in); String n = s.next(); int loc = n.lastIndexOf('1'); if (loc != 0) System.out.println(n.split("0", -1).length + loc + 1); else System.out.println(n.length() - 1); }

5
Greedy Algorithms All hail Johnny. Allow him to enlighten you on this fascinating topic.

6
At each step of the algorithm, make the locally optimal choice This only works for a fairly limited set of problems Only use greedy algorithms if you can prove their validity Examples: o Change making o Segment covering o Dijkstra's algorithm for shortest paths o Kruskal's and Prim's algorithms for minimum spanning tree Greedy Algorithms Source: Wikipedia

7
Change Making (working case) Change making: o Try to make change for a certain amount using the minimum number of coins/bills At each step of the algorithm, use the highest denomination that fits under current amount o Guaranteed to work if each denomination is divisible by the next highest o Bills: $1, $2, $4 o Amount: $11 to $7 to $3 to $1 to $0 o {$1,$2,$4,$4} o Works!

8
Change Making (fail case) Doesn't always work! o Bills: $1, $3, $4 o Amount: $6 o $6 to $2 to $1 to $0 o {$1,$1,$4} o Not as good as {$3,$3}! You would have to use a knapsack algorithm in this case

9
Segment Covering Find maximum number of non-intersecting segments o e.g. try to attend largest number of activites Sort all segments by endpoint Go through segments one by one and check if they can be added to the current segment set without intersections o If so, add it! o Note that you only have to maintain the latest endpoint in the current set - you don't need arrays or anything Source: Topcoder

10
Partyin’, Partyin’ (yeah!) Partyin’, Partyin’ (yeah!) fun, fun, fun, fun looking forward to the weekend It's Monday, and Halloween, so naturally, the cows are out partying. Bessie has a list of N<100,000 parties and their start/end times (in 24 hr format), and she wants to maximize the number of parties she attends, k. She must attend the entirety of each party. Note that the parties are sorted by end time (VERY important). Sample Input: 4 0:00 0:05 0:00 0:10 0:05 0:15 0:15 0:20 Sample Output: (print out k on a single line) 3 This is worth 20 pts, but for 15 extra pts, also print out the maximum amount of time she can spend attending k parties (fairly difficult).

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google