807. Max Increase to Keep City Skyline

来源:https://www.cnblogs.com/gsz-/archive/2018/07/27/9379704.html
-Advertisement-
Play Games

題目描述: 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 ...


題目描述

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] ]

Notes:

  • 1 < grid.length = grid[0].length <= 50.
  • All heights grid[i][j] are in the range [0, 100].
  • All buildings in grid[i][j] occupy the entire grid cell: that is, they are a 1 x 1 x grid[i][j] rectangular prism.

解題思路

先得到每一行所對應最高建築的高度row[i]和每一列對應的最高建築的高度col[j]。要增加建築面積,但是為了不改變橫向和縱向看去城市最高的建築高度,

對於每一個建築[i][j]:

  (1)是其所在行或列的最高建築時,不能改變其高度。

  (2)其不是所在行或列的最高建築時,可以增加高度至col[j]和row[i]中最小值。

代碼:

 1 int maxIncreaseKeepingSkyline(int** grid, int gridRowSize, int *gridColSizes) {
 2     int* row = (int*) malloc(sizeof(int) * gridRowSize);
 3     int* col = (int*) malloc(sizeof(int) * gridColSizes[0]);
 4     for (int i = 0; i < gridRowSize; ++i) {
 5         int max = 0;
 6         for (int j = 0; j < gridColSizes[0]; ++j) {
 7             if (grid[i][j] > max)
 8                 max = grid[i][j];
 9         }
10         row[i] = max;
11     }
12     for (int i = 0; i < gridColSizes[0]; ++i) {
13         int max = 0;
14         for (int j = 0; j < gridRowSize; ++j) {
15             if (grid[j][i] > max)
16                 max = grid[j][i];
17         }
18         col[i] = max;
19     }
20     int added = 0;
21     for (int i = 0; i < gridRowSize; ++i) {
22         for (int j = 0; j < gridColSizes[0]; ++j) {
23             int max = row[i] < col[j] ? row[i] : col[j];
24             if (grid[i][j] < max)
25                 added += max - grid[i][j];
26         }
27     }
28     return added;
29 }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 最近在面試過程中,遇到許多抽象類和介面的面試題,所以今天特意研究了下,然後寫出來分享給大家,希望對面試的朋友有幫助,如果覺得寫的可以點個贊吧! 1:抽象類可以實例化,抽象類可以通過子類間接的實例化父類,介面不能實例化。 2:抽象類可以擁有私有屬性、方法,介面不能擁有。 3:抽象類方法不能使用defa ...
  • C++11新特性: auto: auto讓編譯器通過初始值來推算變數的類型。 auto定義的變數必須有初始值。 auto聲明的所有變數的初始基本數據類型都必須一樣。 decltype: decltype的作用是選擇返回操作數的數據類型。 編譯器分析表達式並得到它的類型,卻不計算表達式的值。 如果de ...
  • Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the leng ...
  • 15.1 動態代理 在之後學習Spring框架時,Spring框架有一大核心思想,就是AOP,(Aspact-Oriented-Programming 面向切麵編程) 而AOP的原理就是Java的動態代理機制,在Java的動態代理機制中,有兩個重要的類或介面,一個是 InvocationHandle ...
  • 【前言】對於像我一樣的新手來說,我覺得此環節難點主要是相關依賴包的安裝和Flask-SQLAlchemy的使用,下麵將一一講解: 所謂數據模型,百度的解釋是:“數據模型(Data Model)是數據特征的抽象。數據(Data)是描述事物的符號記錄,模型(Model)是現實世界的抽象。數據模型從抽象層 ...
  • 3-1.c指針用作函數參數 目的:是為了通過swapdate()函數把實參x,y的值進行交換,上述例子是將形參dat_x,dat_y的值進行交換,但是形參的交換並沒有改變實參的交換,因為函數在調用時給形參分配了單獨的記憶體空間,實參的值傳遞給形參實際是把實參的值放在形參的記憶體空間,形參的值是實參的備份 ...
  • 閏年計算器 題目:輸入年份,判斷該年是否為閏年。 方法:1.能被400整除的年份 2.能被4整除,但不能被100整除 註:以上案例主要涉及到了條件判斷if...else...以及關係運算符的知識點。 每月天數計算器 題目:輸入一個月份,判斷該月有多少天 方法:先判斷年份是否為閏年或平年,再計算該年份 ...
  • JAVA方法調用中的解析與分派 本文算是《深入理解JVM》的讀書筆記,參考書中的相關代碼示例,從位元組碼指令角度看看解析與分派的區別。 方法調用,其實就是要回答一個問題:JVM在執行一個方法的時候,它是如何找到這個方法的? 找一個方法,就需要知道 所謂的 地址。這個地址,從不同的層次看,對它的稱呼也不 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...