The Skyline Problem leetcode Java__java

Source: Internet
Author: User
public class Solution {class KeyPoint {public int key;
        public int height;

        Public KeyPoint next = null;
            public KeyPoint (int key, int height) {this.key = key;
        This.height = height;
        } public static int[] Getkeypoint (int key, int height) {int[] kp = new int[2];
        Kp[0] = key;
        KP[1] = height;
    return KP;
        List<int[]> getskyline (int[][] buildings) {KeyPoint head = new KeyPoint ( -1,0);
        KeyPoint PREVKP = head;
            For (int[] building:buildings) {int L = building[0], r = building[1], h= building[2];
            Insert left point while (Prevkp.next!= null && prevKP.next.key <= l) PREVKP = Prevkp.next;
            int preheight = Prevkp.height;
            if (Prevkp.key = = L) prevkp.height = Math.max (Prevkp.height, h); else if (Prevkp.height < h) {KeyPoint next = Prevkp.nexT
                Prevkp.next = new KeyPoint (l, h);
                PREVKP = Prevkp.next;
            Prevkp.next = Next;
            }//Insert right point and update points in between KeyPoint prev = PREVKP, cur = prevkp.next;
                while (cur!= null && Cur.key < r) {preheight = Cur.height;
                Cur.height = Math.max (Cur.height, h);
                if (cur.height = = prev.height) Prev.next = Cur.next;
                else prev = cur;
            cur = cur.next;
                } if (Prev.height!= preheight && prev.key!= r && (cur = null | | Cur.key!= r)) {
                KeyPoint next = Prev.next;
                Prev.next = new KeyPoint (r, Preheight);
            Prev.next.next = Next;
        }//Convert to list<int[]> list<int[]> List = new arraylist<int[]> (); KeyPoint prev =Head, cur = head.next; while (cur!= null) {if (cur.height!= prev.height) list.add (Getkeypoint (Cur.key, Cur.height)
            );
            prev = cur;
        cur = cur.next;
    } return list; }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.