×

Skyline Problem in C++

We have given n rectangular buildings in a 2-dimensional city. Here, to compute the Skyline of the given n rectangle structures in a two-dimensional metropolis while removing hidden lines, the primary job is to delete all portions of systems that are not visible after viewing them from a distance.

A triplet represents each building, and all structures have a standard bottom (left, ht, right)

  • Left: Is the left side of x coordinated(or wall)?
  • Right: The right side's x coordinate.
  • Ht: The building's height is indicated by ht.

An urban skyline is made up of many rectangular strips. A pair (left, ht) is used to describe a rectangular strip, where left denotes the x coordinate of the strip's left side and its height.

Example:

Buildings[][] = 1, 11, 5, 2, 6, 7, 9, 12, 14, 3, 25, 19, 18, 22, 23, 13, 29, 24, and 4 (in that order)

Output: 1, 11, 3, 13, 9, 0; 12, 7; 16, 3; 19, 18; 22, 3; 23, 13; 29, 0;

Approach:

  • Retrieve the left wall location, height, and correct wall location values for each structure from the triplets provided.
  • Keep the pair of the right wall's total height, and the left wall's negative height value in a vector called walls. This is done to distinguish between the left and right borders of the same building.
  • Order the walls from highest to lowest.
  • Cross the vector walls, and if a left wall is discovered, put its height in the multiset M. If a suitable barrier is met in any other case, take the multiset's appropriate height out.
  • Verify whether the top value has changed. Update the maximum value and save the current wall's abscissa if it has changed (x-coordinate).
  • The value pairs kept in the skyline vector should be printed.

Example:

// C++ programme for themethod described above
#include <bits/stdc++.h>
usingnamespace std;
// work to construct a skyline
vector<pair<int, int>>
create skylines(vector<vector<int>>& buildings)
{
 
    // learn how manybuildings there are.
    int N = buildings.size();
 
    // To keep track of the buildings' left and right wall positions.
    vector<pair<int, int>> wall;
 
    // triplet of architectural elements and parameters
    int left, height, right;
    for (inti = 0; i< N; i++) {
 
        // veer to the left of the building
        left = buildings[i][0];
 
        // veer to the left of the building
        height = buildings[i][1];
 
        // the appropriate building location
        right = buildings[i][2];
 
        // Keep left point and height in mind.
        // from the left wall.
        // Negative value indicates the left wall.
        // first be added into the multiset
        // identical abscissa(x) as the right wall
        wall.push_back({ left, -height });
 
        // Store the right wall's correct point and height.
        wall.push_back(
            make_pair(right, height));
    }
 
    // order the walls from highest to lowest
    sort(wall.begin(), wall.end());
 
    // output storing Skyline
    vector<pair<int, int>> skyline;
 
    // Begin a multiset with
    // Organize the heights of the left wall.
    multiset<int>leftWallHeight = { 0 };
 
    // Maximum height currently among leftWallHeights
    int top = 0;
 
    // Through the sorted walls, turn.
    For (autow wall) {
 
        // if one finds the left wall
        if (w.second< 0) {
 
            // Add the height.
            left all eight.insert(-w.second);
        }
 
        // if one finds the right wall
        else {
 
            // Take the height down
            left all eight.erase(
                left all eight.find(w.second));
        }
        //.rbegin(): Mark a skyline point if the top changes. backward iteration
 
        if (*leftWallHeight.rbegin() != top) {
 
            top = *leftWallHeight.rbegin();
            skyline.push_back(
                make_pair(w.first, top));
        }
    }
    //Skyline to print again Skyline
    return Skyline;
}
   // Print the output skyline using this function.
voidprint skyline(
    vector<vector<int>>& buildings)
{
 
      // Skyline creation function call
    vector<pair<int, int>> skyline
        = create skylines(buildings);
 
    cout<< "Skyline for the given."
         << " buildings:\n{";
 
    for (autoit: skyline) {
 
        cout<< "{" <<it.first<< ", "
             <<it.second<< "} ";
    }
    cout<< "}";
}
//Driver Number
 int main()
{
    vector<vector<int>> buildings;
    //Given the wall's height and the left and right locations,
    buildings = { { 1, 11, 5 }, { 2, 6, 7 },
                  { 3, 13, 9 }, { 12, 7, 16 },
                  { 14, 3, 25 }, { 19, 18, 22 },
                  { 23, 13, 29 }, { 24, 4, 28 } };
   //Feature Call
    print skyline(buildings);
    return 0;
}

Output:

Skyline Problem in C++

Time Complexity: O(n*log(N))

Auxiliary Space: O(N)


Related Topics

The Stock Span Problem

It is necessary to determine the span of a stock's price throughout all n days in order to solve the stock span problem, which involves a set of n daily...

5 minutes read.

Singleton Design Pattern in C++

Singleton is similar to the global variable. Singleton helps to have only one object of its kind and provides only single access. One of the key features of the singleton...

2 minutes read.

How to create a directory or folder in C/C++?

A directory is to lists all files and subdirectories in a directory, set of the files will be kept in the directory, which is a location. A subdirectory is a...

5 minutes read.

How the value is passed in C++

Introduction: The call-by-value method of giving arguments to a function duplicates the real value of an argument into the formal parameter of the function. In this instance, modifications to the parameter...

5 minutes read.

Virtual base class in C++

Consider in a C++ program, there are 4 classes named class A, class B, class C, and class D. If class B and class c inherit properties from class A....

3 minutes read.

C++ Aggregation

C++ Aggregation Definition: In C++, aggregation is a process in which one class (as an entity reference) defines another class. It provides another way to reuse the class. It represents...

4 minutes read.

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++ 1/2 decimal equal is 0.555555555555555555555 .... An indefinite number of lengths will require the storage...

4 minutes read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

Pointer to Object in C++

What is a pointer? A pointer in C++ is used to point the variable by storing the address of the variable. In C++, to print the address of the variable, we...

4 minutes read.

Dynamic Constructor in C++

Dynamic constructors create dynamic memory by using a dynamic memory allocator new within the constructor. This allows us to initialize objects dynamically. The term dynamic constructor is used when memory...

2 minutes read.

External merge sort in C++

External merge sort in C++ External sorting is a concept for a group of sorting algorithms capable of handling large data volumes. External sorting is needed if the information getting sorted...

9 minutes read.

C++ User Defined Exceptions

Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined...

4 minutes read.

Desired Capabilities in Selenium Web Driver in C++

A class called Desired Capabilities is used to specify a set of fundamental requirements, such as browser, operating system, and version combinations.to carry out automated testing of a web application...

7 minutes read.

C++ Prime number program

In this lesson, you'll learn how to verify whether a given number is a prime number or not in C++, and you'll obtain code to do it. What is the definition...

3 minutes read.

C++ Variable

In this article, we will discuss variables in C++ with their types and examples. What are Variables? Variables are specific memory storage spaces that hold a value. During the execution of a...

4 minutes read.

C++ Pointer

Pointer is a derived data type that stores the address of a variable. A pointer is used for memory management and dynamic memory allocation. Pointer works on the address of data rather than...

2 minutes read.

Bits stdc++.h in C++

<bits/stdc++.h> in C++ In essence, it is a header file that contains all the standard libraries. It makes sense to use this file in programming competitions to speed up work, especially...

2 minutes read.

C++ Math Functions

C++ Math Functions: Like other programming languages, C++ offers plenty of mathematical functions needed for various purposes. These functions are defined mainly in the math library in C++. Let us...

4 minutes read.

Sizeof() Operators in C++

sizeof() Operators in C++ The sizeof() operator in C++ defines the size of variables, constants, or data types. It is a unique operator that manipulates other operators and returns the size...

4 minutes read.

C++ array to function

Arrays in C++ : Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. An array can be declared by specifying the variable...

4 minutes read.