https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/

执行用时 : 3 ms, 在Max Increase to Keep City Skyline的Java提交中击败了95.31% 的用户 内存消耗 : 37.7 MB, 在Max Increase to Keep City Skyline的Java提交中击败了92.13% 的用户

最直接的思路:

  1. 获取矩阵的每一行最大值和每一列最大值
  2. 根据上述最大值来“提高”建筑
  1. class Solution {
  2. public int maxIncreaseKeepingSkyline(int[][] grid) {
  3. int result = 0;
  4. int rowSize = grid.length;
  5. // rowMax 每行最大数值
  6. int[] rowMax = new int[rowSize];
  7.  
  8. int colSize = grid[0].length;
  9. // colMax 每列最大数值
  10. int[] colMax = new int[colSize];
  11.  
  12. int i = 0;
  13. int j = 0;
  14.  
  15. for (i = 0; i < rowSize; i++) {
  16. rowMax[i] = 0;
  17. for (j = 0; j < colSize; j++) {
  18. if (grid[i][j] > rowMax[i]) {
  19. rowMax[i] = grid[i][j];
  20. }
  21. }
  22. }
  23.  
  24. for (j = 0; j < colSize; j++) {
  25. colMax[j] = 0;
  26. for (i = 0; i < rowSize; i++) {
  27. if (grid[i][j] > colMax[j]){
  28. colMax[j] = grid[i][j];
  29. }
  30. }
  31. }
  32.  
  33. for (i = 0; i < rowSize; i++) {
  34. for (j = 0; j < colSize; j++) {
  35. if (rowMax[i] > colMax[j]) {
  36. result += colMax[j] - grid[i][j];
  37. } else {
  38. result += rowMax[i] - grid[i][j];
  39. }
  40. }
  41. }
  42.  
  43. return result;
  44. }
  45. }

LeetCode #807. Max Increase to Keep City Skyline 保持城市天际线的更多相关文章

  1. Leetcode 807 Max Increase to Keep City Skyline 不变天际线

    Max Increase to Keep City Skyline In a 2 dimensional array grid, each value grid[i][j] represents th ...

  2. Leetcode 807. Max Increase to Keep City Skyline

    class Solution(object): def maxIncreaseKeepingSkyline(self, grid): """ :type grid: Li ...

  3. [LeetCode] Max Increase to Keep City Skyline 保持城市天际线的最大增高

    In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...

  4. LC 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 the ...

  5. 【LeetCode】807. Max Increase to Keep City Skyline 解题报告(Python &C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. [LeetCode&Python] Problem 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 the ...

  7. 【Leetcode】807. Max Increase to Keep City Skyline

    Description In a 2 dimensional array grid, each value grid[i][j] represents the height of a building ...

  8. [Swift]LeetCode807. 保持城市天际线 | Max Increase to Keep City Skyline

    In a 2 dimensional array grid, each value grid[i][j]represents the height of a building located ther ...

  9. BZOJ1628: [Usaco2007 Demo]City skyline

    1628: [Usaco2007 Demo]City skyline Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 256  Solved: 210[Su ...

随机推荐

  1. openstack stein部署手册 5. placement

    # 建立数据库用户及权限 create database placement; grant all privileges on placement.* to placement@'localhost' ...

  2. 获取用户真实IP:(模拟:客户端--F5--nginx--tomcat 后端获取用户真实IP)

    模拟:客户端--F5--nginx--tomcat 后端获取用户真实IP 192.168.109.137 :nginx01(充当第一层代理==F5)192.168.109.138 :nginx02(二 ...

  3. 树——binary-tree-postorder-traversal(树的前序遍历)

    问题: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given bina ...

  4. xml与json互转

    依赖包: <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib< ...

  5. 装饰器模式-Decerator

    一.定义 装饰器模式又叫做包装模式(Wrapper).装饰器模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 在以下情况下应该使用装饰器模式: 1.需要扩展一个类的功能,或给一个类增 ...

  6. 六、MyBatis-缓存机制

    MyBatis 包含一个非常强大的查询缓存特性,它可以非常方便地配置和定制.缓存可以极大的提升查询效率.MyBatis系统中默认定义了两级缓存, 一级 缓存和 二级缓存.– 1.默认情况下,只有一级缓 ...

  7. flask之视图函数从前端接收数据的方法

    一:从前端接收查询字符串  query-string 注意:get和post都可以在url后面添加查询字符串?a=1&b=2 测试工具:postman 1:get方式接收 视图函数 from ...

  8. python 按多维列表中的某一个元素位进行排序

    import os,re top = os.popen("tasklist") process_list = [] split_r = r"\s+" memor ...

  9. DELPHI FMX 获取系统版本 ANDROID IOS通用

    引用System.sysutils function getOSInfo:String; begin result:= fomrat('%s:%d.%d', TOSVersion.Name,TOSVe ...

  10. 编辑器直接word直接上传word里的图片

    tinymce是很优秀的一款富文本编辑器,可以去官网下载.https://www.tiny.cloud 这里分享的是它官网的一个收费插件powerpaste的旧版本源码,但也不影响功能使用. http ...