zoukankan      html  css  js  c++  java
  • 2019年2月22日 807. Max Increase to Keep City Skyline

    不是很难的题,O(n^2)解决。

    主要的思路是确定你需要的信息,和状态转移方程。我们需要确定各个行列最大的高度:row[i], column[j],然后取 min(row[i], column[j]) - grid[i][j] 之和就可以了。

    class Solution {
    public:
        int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
            
            int total = 0;
            int maxX = 0, maxY = 0;
            vector<int> row, column;
            
            for (int i=0; i<grid.size(); ++i) {
                maxX = 0;
                maxY = 0;
                for (int j=0; j<grid[i].size(); ++j) {
                    
                    if (maxX < grid[i][j]) maxX = grid[i][j];
                    if (maxY < grid[j][i]) maxY = grid[j][i];
                }
                row.push_back(maxX);
                column.push_back(maxY);
            }
            
            for (int i=0; i<grid.size(); ++i) {
                for (int j=0; j<grid[i].size(); ++j) {
                    total += min(row[i], column[j]) - grid[i][j];
                }
            }
            return total;
        }
    };
  • 相关阅读:
    图论
    数学
    P2222 外婆婆~
    P2083 找人
    P1215 [USACO1.4]母亲的牛奶 Mother's Milk
    New Rap
    P2298 Mzc和男家丁的游戏
    P2040 打开所有的灯
    P1135 奇怪的电梯
    UVA10474 Where is the Marble?
  • 原文地址:https://www.cnblogs.com/seenthewind/p/10416528.html
Copyright © 2011-2022 走看看