SKYLINE SOLUTION


  • 0
    A

    '''#include<stdio.h>

    void insert(int);
    void delete(int);
    void create();
    void check(int);
    void display_pqueue();

    int pri_que[100];
    int front=-1, rear=-1;

    struct build{
    int x;
    int height;
    int start;
    }arr[100];

    void create()
    {
    front = rear = -1;
    }

    void insert(int data)
    {
    if ((front == -1) && (rear == -1))
    {
    front++;
    rear++;
    pri_que[rear] = data;
    return;
    }
    else
    check(data);
    rear++;
    }

    void check(int data)
    {
    int i,j;

    for (i = 0; i <= rear; i++)
    {
        if (data >= pri_que[i])
        {
            for (j = rear + 1; j > i; j--)
            {
                pri_que[j] = pri_que[j - 1];
            }
            pri_que[i] = data;
            return;
        }
    }
    pri_que[i] = data;
    

    }
    void delete(int data)
    {
    int i;
    for (i = 0; i <= rear; i++)
    {
    if (data == pri_que[i])
    {
    for (; i < rear; i++)
    {
    pri_que[i] = pri_que[i + 1];
    }

        pri_que[i] = -99;
        rear--;
    
        if (rear == -1)
            front = -1;
        return;
        }
    }
    

    }

    main(){
    int i,n,left,right,height,j;
    printf("enter no of building : ");
    scanf("%d",&n);

    int k=0;
    for(i=0;i<n;i++){
    scanf("%d %d %d",&left,&right,&height);
    arr[k].x=left;
    arr[k].height=height;
    arr[k].start=1;

    k++;
    
    arr[k].x=right;
    arr[k].height=height;
    arr[k].start=0;
    
    k++;
    

    }

    int temp;

    for(i=0;i<k;i++)
    {
    for(j=i+1;j<k;j++){
    //sort based on x
    //dissimilar x
    if(arr[i].x!=arr[j].x){
    if(arr[i].x>arr[j].x){

                temp=arr[i].x;
                arr[i].x=arr[j].x;
                arr[j].x=temp;
    
                temp=arr[i].height;
                arr[i].height=arr[j].height;
                arr[j].height=temp;
    
                temp=arr[i].start;
                arr[i].start=arr[j].start;
                arr[j].start=temp;
    
            }
        }
        //similar x
        else if(arr[i].x!=arr[j].x){
                //if two are start
            if(arr[i].start==1&&arr[j].start==1){
               //height is high
               if(arr[i].height<arr[j].height)
               {
                temp=arr[i].x;
                arr[i].x=arr[j].x;
                arr[j].x=temp;
    
                temp=arr[i].height;
                arr[i].height=arr[j].height;
                arr[j].height=temp;
    
                temp=arr[i].start;
                arr[i].start=arr[j].start;
                arr[j].start=temp;
               }
            }
               //if two are end
            if(arr[i].start==0&&arr[j].start==0){
                //height is low
                if(arr[i].height>arr[j].height)
               {
                temp=arr[i].x;
                arr[i].x=arr[j].x;
                arr[j].x=temp;
    
                temp=arr[i].height;
                arr[i].height=arr[j].height;
                arr[j].height=temp;
    
                temp=arr[i].start;
                arr[i].start=arr[j].start;
                arr[j].start=temp;
               }
    
            }
            //if first is start and another is end
            if(arr[i].start==0&&arr[j].start==0){
            //height is hight
            if(arr[i].height<arr[j].height)
               {
                temp=arr[i].x;
                arr[i].x=arr[j].x;
                arr[j].x=temp;
    
                temp=arr[i].height;
                arr[i].height=arr[j].height;
                arr[j].height=temp;
    
                temp=arr[i].start;
                arr[i].start=arr[j].start;
                arr[j].start=temp;
               }
    
            }
        }
    }
    

    }

    int tempmax=0,max=0;

    printf("\nThe co-ordinates of skyline are : \n");
    for(i=0;i<k;i++)
    {
    if(arr[i].start==1){
    insert(arr[i].height);
    max=pri_que[front];
    if(tempmax!=max){
    printf("[%d , %d]\n",arr[i].x,max);
    tempmax=max;
    }
    }
    if(arr[i].start==0){
    delete(arr[i].height);
    max=pri_que[front];
    if(tempmax!=max){
    printf("[%d , %d]\n",arr[i].x,max);
    tempmax=max;
    }
    }

    }

    }'''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.