Leetcode 807 Max increase to Keep city Skyline unchanging skyline

Source: Internet
Author: User
Tags min

Title Description:

In a 2 dimensional array grid with each value grid[i][j] represents the height of a building located there. We is allowed to increase the height of any number of buildings, by any amount (the amounts can is different for differen T buildings). Height 0 is considered to being a building as well.

At the end, the "Skyline" is viewed from all four directions of the grid, i.e. top, bottom, left, and right, must is the Same as the skyline of the original grid. A city's skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. See the following example.

What's the maximum total sum and the height of the buildings can be increased?

Example:
input:grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
output:35
Explanation: The 
grid is  :
[[3, 0, 8, 4], 
  [2, 4, 5, 7],
  [9, 2, 6, 3],
  [0, 3, 1, 0]] The

skyline viewed from top or bottom is: [9, 4, 8, 7]
The skyline viewed from left or right are: [8, 7, 9, 3] The

grid after increasing the height of buildings without Affe Cting skylines is:

gridnew = [[8, 4, 8, 7],
            [7, 4, 7, 7],
            [9, 4, 8, 7],
            [3, 3, 3, 3]]

Notes:1 < Grid.length = Grid[0].length <= 50. All Heights Grid[i][j] is in the range [0, 100]. All buildings in Grid[i][j] occupy the entire grid cell:that was, they is a 1 x 1 x grid[i][j] Rectangular prism.


Translation:

1. Give a city matrix, each of which represents the height of the building in this place, a city with a left-to-right, top-to-bottom two skyline, which is the tallest building in this direction.

2. Increase the height of each building, but do not destroy the skyline, the maximum height can be lifted (sum)

3. Obviously grid[i][j] can be up to min (horizontal skyline [i], vertical skyline [j]), traversing a matrix once

Code:

Class Solution {public:vector<int> To_skyline_shuipin (vector<vector<int>>& grid,int n,int m)
        {vector<int> ans (m,int_min);
        for (int. j=0;j<m;j++) for (int i=0;i<n;i++) Ans[j]=max (Ans[j],grid[i][j]);
    return ans; } vector<int> To_skyline_chuizhi (vector<vector<int>>& grid,int n,int m) {vector<
        Int> ans (n,int_min);
        for (int. i=0;i<n;i++) for (int j=0;j<m;j++) Ans[i]=max (Ans[i],grid[i][j]);
    return ans; } int Maxincreasekeepingskyline (vector<vector<int>>& grid) {int max_up=0,n=grid.size (), M
        =grid[0].size (); vector<int> Shuipin = To_skyline_shuipin (grid,n,m);//From left to right see vector<int> Chuizhi = To_skyline_chuizhi (gr ID,N,M);//from the top down see for (int i=0;i<n;i++) for (int j=0;j<m;j++) max_up + = min (shuipin[ J],chuizhi[i])-GRID[I][J];
    return max_up; }
};

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.