problem dicusssion
play

Problem Dicusssion Part 2: Some more problems Lucca Siaudzionis and - PowerPoint PPT Presentation

Problem Dicusssion Part 2: Some more problems Lucca Siaudzionis and Jack Spalding-Jamieson 2020/03/19 University of British Columbia Announcements A4 is now due Wednesday March 25th. 1 Maximum Area Rectangle Find the maximum area of a


  1. Problem Dicusssion Part 2: Some more problems Lucca Siaudzionis and Jack Spalding-Jamieson 2020/03/19 University of British Columbia

  2. Announcements • A4 is now due Wednesday March 25th. 1

  3. Maximum Area Rectangle Find the maximum area of a rectangle that can fit in a histogram. 7 6 5 4 3 3 3 2 1 1 The histogram has 1 ≤ n ≤ 5 · 10 6 different entries of non-negative integers. Try it yourself: https://leetcode.com/problems/largest-rectangle-in-histogram/ 2

  4. Maximum Area Rectangle - General Ideas This problem can be solved with a segment tree in Θ( n log n ) time (a good exercise), but we can do better. We will find all the maximal rectangles, i.e. the rectangles that cannot be expanded any more within the bounds of the histogram. 7 7 6 6 5 5 4 4 3 3 3 3 3 3 2 2 1 1 1 1 Figure 1: Two different rectangles. One is maximal (left), the other is not (right). The right rectangle could be expanded to the left. 3

  5. Maximum Area Rectangle - Line Sweep To find all these maximal rectangles, we’re going to do a line sweep. As we move our line, we’re going to keep track of all the maximal rectangles that touch the sweep line, sorted by their height. Note that since the sweep line constrains the rectangles, these rectangles may not be maximal in the entire problem. Let’s look at an example. 4

  6. Maximum Area Rectangle - Line Sweep Demo (1) 7 6 5 4 3 3 3 2 1 1 5

  7. Maximum Area Rectangle - Line Sweep Demo (2) 7 6 5 4 3 3 3 2 1 1 6

  8. Maximum Area Rectangle - Line Sweep Demo (3) 7 6 5 4 3 3 3 2 1 1 7

  9. Maximum Area Rectangle - Line Sweep Demo (4) 7 6 5 4 3 3 3 2 1 1 8

  10. Maximum Area Rectangle - Line Sweep Demo (5) 7 6 5 4 3 3 3 2 1 1 9

  11. Maximum Area Rectangle - Line Sweep Demo (6) 7 6 5 4 3 3 3 2 1 1 10

  12. Maximum Area Rectangle - Line Sweep Demo (7) 7 6 5 4 3 3 3 2 1 1 11

  13. Maximum Area Rectangle - Line Sweep Demo (8) 7 6 5 4 3 3 3 2 1 1 12

  14. Maximum Area Rectangle - Line Sweep Demo (9) 7 6 5 4 3 3 3 2 1 1 13

  15. Maximum Area Rectangle - Line Sweep Demo (10) 7 6 5 4 3 3 3 2 1 1 14

  16. Maximum Area Rectangle - Line Sweep Demo (11) 7 6 5 4 3 3 3 2 1 1 15

  17. Maximum Area Rectangle - Line Sweep Demo (12) 7 6 5 4 3 3 3 2 1 1 16

  18. Maximum Area Rectangle - Line Sweep Demo (13) 7 6 5 4 3 3 3 2 1 1 17

  19. Maximum Area Rectangle - Line Sweep Demo (14) 7 6 5 4 3 3 3 2 1 1 18

  20. Maximum Area Rectangle - Line Sweep Demo (15) 7 6 5 4 3 3 3 2 1 1 19

  21. Maximum Area Rectangle - Line Sweep Demo (16) 7 6 5 4 3 3 3 2 1 1 20

  22. Maximum Area Rectangle - Line Sweep Demo (17) 7 6 5 4 3 3 3 2 1 1 21

  23. Maximum Area Rectangle - Line Sweep Demo (18) 7 6 5 4 3 3 3 2 1 1 22

  24. Maximum Area Rectangle - Line Sweep Demo (19) 7 6 5 4 3 3 3 2 1 1 23

  25. Maximum Area Rectangle - Line Sweep Demo (20) 7 6 5 4 3 3 3 2 1 1 24

  26. Maximum Area Rectangle - Line Sweep Demo (21) 7 6 5 4 3 3 3 2 1 1 25

  27. Maximum Area Rectangle - Line Sweep Demo (22) 7 6 5 4 3 3 3 2 1 1 26

  28. Maximum Area Rectangle - Line Sweep Demo (23) 7 6 5 4 3 3 3 2 1 1 27

  29. Maximum Area Rectangle - Line Sweep Demo (24) 7 6 5 4 3 3 3 2 1 1 28

  30. Maximum Area Rectangle - Line Sweep When we advance the line, we need to destroy some of the rectangles that can no longer touch the line. Observe that the set of rectangles we destroy corresponds to exactly those with a height greater than the next entry in the histogram. For implementation, we can store our current set of rectangles with a stack!. Some quick observations about this method: The current set of rectangles stored in the stack at any given time is decreasing in both leftmost x coordinate, and height (as we delete things with larger height, they also get wider). 29

  31. Maximum Area Rectangle - Implementation (C++) struct bar { int x, y; bar(int x, int y) : x(x), y(y) {} }; 1 int largestRectangleArea(vector<int>& heights) { 2 stack<bar> mc; mc.emplace(-1,-1); int best = 0; 3 // sweep line x coordinate 4 int x; for (x = 0; x < heights.size(); ++x) { 5 int& h = heights[x]; // height of next item 6 // look at and delete all the rectangles too tall to touch the new sweep 7 while (mc.size() > 1 && mc.top().y > h) { 8 bar cur = mc.top(); mc.pop(); 9 best = max(best, (x-mc.top().x-1)*cur.y); } 10 mc.emplace(x,h); } 11 while (mc.size() > 1) { // look at the rectangles that touch the very end 12 bar cur = mc.top(); mc.pop(); 13 best = max(best, (x-mc.top().x-1)*cur.y); } 14 return best; 15 30 } 16

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend