Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. // This is the text editor interface.
  2. // Anything you type or change here will be seen by the other person in real time.
  3. import java.util.*;
  4. class Solution{
  5. public static void main(String[] str){
  6. List<int[]> list = new ArrayList<int[]>();
  7. list.add(new int[]{1,3,2});
  8. list.add(new int[]{2,4,2});
  9. //list.add(new int[]{5,6,7});
  10. //list.add(new int[]{5,8,2});
  11. System.out.println(String.valueOf(get_area(list)));
  12. }
  13. public static int get_area(List<int[]> list){
  14. if(list==null||list.size()==0) return 0;
  15. List<int[]> lines = new ArrayList<int[]>();
  16. for(int[] i : list){
  17. lines.add(new int[]{i[0],i[2]});
  18. lines.add(new int[]{i[1],-i[2]});
  19. }
  20. Collections.sort(lines,new Comparator<int[]>(){
  21. public int compare(int[] a, int[] b){
  22. if(a[0]!=b[0]) return a[0]-b[0];
  23. else return b[1]-a[1];
  24. }
  25. });
  26. PriorityQueue<Integer> height = new PriorityQueue<Integer>(100,Collections.reverseOrder());
  27. height.add(0);
  28. int pre = 0;
  29. List<int[]> point = new ArrayList<int[]>();
  30. for(int[] line:lines){
  31. if(line[1]>0){
  32. height.add(line[1]);
  33. }
  34. else{
  35. height.remove(-line[1]);
  36. }
  37. if(height.peek()!=pre){
  38. point.add(new int[]{line[0],height.peek()});
  39. pre = height.peek();
  40. }
  41. }
  42. int res = 0;
  43. for(int i =1;i<point.size();i++){
  44. int[] pre1 = point.get(i-1);
  45. int[] cur = point.get(i);
  46. res+=(cur[0]-pre1[0])*pre1[1];
  47. }
  48. return res;
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement