[LeetCode] 807. Max Increase to Keep City Skyline

807.Max Increase to Keep City Skyline

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

At the end, the "skyline" when viewed from all four directions of the grid, i.e. top, bottom, left, and right, must be 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 is the maximum total sum that 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 is: [8, 7, 9, 3]

The grid after increasing the height of buildings without affecting skylines is:

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

解:

在不破坏天际线的前提下,建筑物的高度有两个限制

1.from top or bottom

2.from left or right

要同时满足这两个限制,取最小值即可

先算出一个矩阵的横向最大值和纵向最大值,即天际线

将矩阵中每个元素替换为对应的横向和纵向最大值中的最小值就得到了gridNew, 作个减法,再把sum加起来就搞定了

horizontal为图中的[9 4 8 7]

vertical为图中的[8 7 9 3]

即将 grid[i][j] 替换为 min(horizontal[j], vertical[i])

图中的红色数字即为gridNew

附上代码

static int x=[](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();

class Solution {
public:
    int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
        vector<int> horizontal;
        vector<int> vertical;

        for (int i = 0; i < grid.size(); i++)
        {
            int vertical_max = 0;
            int horizontal_max = 0;
            for (int j = 0; j < grid.at(i).size(); j++)
            {
                if (grid.at(i).at(j) > vertical_max)
                {
                    vertical_max = grid.at(i).at(j);
                }

                if (grid.at(j).at(i) > horizontal_max)
                {
                    horizontal_max = grid.at(j).at(i);
                }
            }
            vertical.push_back(vertical_max);
            horizontal.push_back(horizontal_max);
        }

        int sum = 0;

        for (int i = 0; i < grid.size(); i++)
        {
            for (int j = 0; j < grid.at(i).size(); j++)
            {
                int max_i_j = (vertical.at(i) < horizontal.at(j)) ? vertical.at(i) : horizontal.at(j);
                sum += (max_i_j - grid.at(i).at(j));
                grid.at(i).at(j) = max_i_j;
            }
        }

        return sum;
    }
};

下面这段代码是leetcode里运行时间为0ms的代码里的,可以减少leetcode里代码的运行时间

static int x=[](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();

用 sync_with_stdio 接口,关闭 std::cin 和 std::cout 与 scanf 和 printf 的同步,减少了相当的 IO 开销

用 cin.tie 接口,完成了 cin 和 cout 的解耦,减少了大量 flush 调用

Reference

  1. https://www.jianshu.com/p/fa8ad995d300

原文地址:https://www.cnblogs.com/arcsinw/p/9337551.html

时间: 2024-10-07 15:11:35

[LeetCode] 807. Max Increase to Keep City Skyline的相关文章

807. Max Increase to Keep City Skyline

题目描述: In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts can be different for different buildings). Heig

(Medium) Max Increase to Keep City SkyLine LeetCode

Description: In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts can be different for different buildings

【leetcode】Max Points on a Line (python)

给定一个点,除该点之外的其他所有点中,与该点的关系要么是共线,要么就是共点,也就是两点重合. 共线有三种情况:水平共线,垂直共线,倾斜的共线.合并下这三种情况就是斜率存在的共线和斜率不存在的共线. 那么我们的任务就是针对每个点,找出与其共线的这些情况中,共线最多的点的个数. 注意:最终的结果别忘了加上共点的个数. class Solution: def maxPoints(self, points ): if len( points ) <= 1: return len( points ) ma

BZOJ1628: [Usaco2007 Demo]City skyline

1628: [Usaco2007 Demo]City skyline Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 256  Solved: 210[Submit][Status] Description The best part of the day for Farmer John's cows is when the sun sets. They can see the skyline of the distant city. Bessie w

[LeetCode OJ] Max Points on a Line—Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

//定义二维平面上的点struct Point { int x; int y; Point(int a=0, int b=0):x(a),y(b){} }; bool operator==(const Point& left, const Point& right) { return (left.x==right.x && left.y==right.y); } //求两个点连接成的直线所对应的斜率 double line_equation(const Point&

LeetCode OJ - Max Points on a Line

题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 解题思路: 第一反应:枚举两个点组成的直线,然后看其他的点在不在这条直线上,在此过程中统计最大值.此思路的复杂度 O(n^3) 参考了网上的思路:枚举第一个点,用unordered_map来记录其余的点和这个点的斜率,若斜率相同则代表这些点在同一直线上.避免double问题,把斜率转化成化简

[USACO2005][POJ3044]City Skyline(贪心+单调栈)

题目:http://poj.org/problem?id=3044 题意:以坐标的形式给出一张图,表示一些楼房的正视图,求出楼房的最少个数. 分析:和小学常做的立方体问题很像,很容易想到一个贪心方法,那就是尽量把矮的楼房放在高的楼房的前面,即连续的等高的一些"X"我们把它视为一座楼房. 具体的做法可以维护一个Y坐标单增的栈,从左到右读入每个坐标,将Y坐标与栈顶坐标比较: 若Y==栈顶坐标,那么接着读下面一个坐标 若Y>栈顶坐标,那么把Y坐标加入栈成为新的栈顶 若Y<栈顶坐标

[Usaco2007 Demo][BZOJ1628] City skyline

1628: [Usaco2007 Demo]City skyline Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 320  Solved: 260[Submit][Status][Discuss] Description Input 第一行给出N,W 第二行到第N+1行:每行给出二个整数x,y,输入的x严格递增,并且第一个x总是1 Output 输出一个整数,表示城市中最少包含的建筑物数量 Sample Input 10 26 1 1 2 2 5

[滑动窗口] leetcode 1004 Max Consecutive Ones III

problem:https://leetcode.com/problems/max-consecutive-ones-iii/ 维护最多包含k个0的滑动窗口,一旦超过了k个0,把队首的0 pop出来.不断更新当前滑动窗口中的数据个数,并取最大值返回即可. class Solution { public: int longestOnes(vector<int>& A, int K) { int count = 0; int index = -1; deque<int> zer