Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is the text editor interface.
- // Anything you type or change here will be seen by the other person in real time.
- import java.util.*;
- class Solution{
- public static void main(String[] str){
- List<int[]> list = new ArrayList<int[]>();
- list.add(new int[]{1,3,2});
- list.add(new int[]{2,4,2});
- //list.add(new int[]{5,6,7});
- //list.add(new int[]{5,8,2});
- System.out.println(String.valueOf(get_area(list)));
- }
- public static int get_area(List<int[]> list){
- if(list==null||list.size()==0) return 0;
- List<int[]> lines = new ArrayList<int[]>();
- for(int[] i : list){
- lines.add(new int[]{i[0],i[2]});
- lines.add(new int[]{i[1],-i[2]});
- }
- Collections.sort(lines,new Comparator<int[]>(){
- public int compare(int[] a, int[] b){
- if(a[0]!=b[0]) return a[0]-b[0];
- else return b[1]-a[1];
- }
- });
- PriorityQueue<Integer> height = new PriorityQueue<Integer>(100,Collections.reverseOrder());
- height.add(0);
- int pre = 0;
- List<int[]> point = new ArrayList<int[]>();
- for(int[] line:lines){
- if(line[1]>0){
- height.add(line[1]);
- }
- else{
- height.remove(-line[1]);
- }
- if(height.peek()!=pre){
- point.add(new int[]{line[0],height.peek()});
- pre = height.peek();
- }
- }
- int res = 0;
- for(int i =1;i<point.size();i++){
- int[] pre1 = point.get(i-1);
- int[] cur = point.get(i);
- res+=(cur[0]-pre1[0])*pre1[1];
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement