leetcode 554. 磚墻

你的面前有一堵矩形的、由 n 行磚塊組成的磚墻。這些磚塊高度相同(也就是一個單位高)但是寬度不同。每一行磚塊的寬度之和應該相等。

你現在要畫一條 自頂向下 的、穿過 最少 磚塊的垂線。如果你畫的線只是從磚塊的邊緣經過,就不算穿過這塊磚。你不能沿著墻的兩個垂直邊緣之一畫線,這樣顯然是沒有穿過一塊磚的。

給你一個二維數組 wall ,該數組包含這堵墻的相關信息。其中,wall[i] 是一個代表從左至右每塊磚的寬度的數組。你需要找出怎樣畫才能使這條線 穿過的磚塊數量最少 ,并且返回 穿過的磚塊數量 。
在這里插入圖片描述

示例 1:

輸入:wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]
輸出:2
示例 2:

輸入:wall = [[1],[1],[1]]
輸出:3

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/brick-wall
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。

解題思路

因為輸入是一個二維數組,因此我們可以將一維數組看成一行磚,例如示例1

輸入:wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]
輸出:2

第一行是[1,2,2,1],我們從這里分析,因為題目要求 穿過的磚塊數量最少,所以對于每一行,我們需要關注的是磚塊之間的空隙去進行插入,這樣就能避免穿過磚塊,因此我們需要記錄每一行磚塊空隙的位置

例如:
[1,2,2,1] 空隙坐標為 1,3,5(左端和右端不算空隙)

對于每一層我們使用map記錄不同空隙坐標以及對應出現的次數,出現次數最多的空隙坐標代表了有多層磚塊都在這個位置出現空隙,從這里穿過的空隙最小。

代碼

func leastBricks(wall [][]int) int {m2 := make(map[int]int)n,res:=len(wall),0for i := 0; i < n; i++ {pre:=0for j := 0; j < len(wall[i])-1; j++ {pre+=wall[i][j]i2,ok := m2[pre]if ok{m2[pre]=i2+1}else {m2[pre]=1}if m2[pre]>res{res=m2[pre]}}}return n-res
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391201.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391201.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391201.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

django-rest-framework解析請求參數過程詳解

https://www.jb51.net/article/165699.htm 轉載于:https://www.cnblogs.com/gcgc/p/11544187.html

遞歸 和 迭代 斐波那契數列

#include "stdio.h"int Fbi(int i) /* 斐波那契的遞歸函數 */ { if( i < 2 ) return i 0 ? 0 : 1; return Fbi(i - 1) Fbi(i - 2); /* 這里Fbi就是函數自己&#xff0c;等于在調用自己 */ }int main() { int i; int a[40]; printf("迭代顯示斐波那契數列…

單元測試 python_Python單元測試簡介

單元測試 pythonYou just finished writing a piece of code and you are wondering what to do. Will you submit a pull request and have your teammates review the code? Or will you manually test the code? 您剛剛編寫了一段代碼&#xff0c;并且想知道該怎么做。 您…

飛行模式的開啟和關閉

2019獨角獸企業重金招聘Python工程師標準>>> if(Settings.System.getString(getActivity().getContentResolver(),Settings.Global.AIRPLANE_MODE_ON).equals("0")) { Settings.System.putInt(getActivity().getContentResolver(),Settings.Global.AIRPLA…

消解原理推理_什么是推理統計中的Z檢驗及其工作原理?

消解原理推理I Feel:我覺得&#xff1a; The more you analyze the data the more enlightened, data engineer you will become.您對數據的分析越多&#xff0c;您將變得越發開明。 In data engineering, you will always find an instance where you need to establish whet…

pytest+allure測試框架搭建

https://blog.csdn.net/wust_lh/article/details/86685912 https://www.jianshu.com/p/9673b2aeb0d3 定制化展示數據 https://blog.csdn.net/qw943571775/article/details/99634577 環境說明&#xff1a; jdk 1.8 python 3.5.3 allure-commandline 2.13.0 文檔及下載地址&…

lintcode433 島嶼的個數

島嶼的個數 給一個01矩陣&#xff0c;求不同的島嶼的個數。 0代表海&#xff0c;1代表島&#xff0c;如果兩個1相鄰&#xff0c;那么這兩個1屬于同一個島。我們只考慮上下左右為相鄰。 您在真實的面試中是否遇到過這個題&#xff1f; Yes樣例 在矩陣&#xff1a; [[1, 1, 0, …

大數據分析要學習什么_為什么要學習數據分析

大數據分析要學習什么The opportunity to leverage insights from data has never been greater.利用來自數據的洞察力的機會從未如此大。 Humans tend to generate a lot of data each day - from heart rates to favorite songs, fitness goals and movie preferences. You …

POJ - 3257 Cow Roller Coaster (背包)

題目大意&#xff1a;要用N種材料建一條長為L的路&#xff0c;如今給出每種材料的長度w。起始地點x。發費c和耐久度f 問&#xff1a;在預算為B的情況下&#xff0c;建好這條路的最大耐久度是多少 解題思路&#xff1a;背包問題 dp[i][j]表示起始地點為i。發費為j的最大耐久度…

leetcode 1473. 粉刷房子 III(dp)

在一個小城市里&#xff0c;有 m 個房子排成一排&#xff0c;你需要給每個房子涂上 n 種顏色之一&#xff08;顏色編號為 1 到 n &#xff09;。有的房子去年夏天已經涂過顏色了&#xff0c;所以這些房子不需要被重新涂色。 我們將連續相同顏色盡可能多的房子稱為一個街區。&a…

大學生信息安全_給大學生的信息

大學生信息安全You’re an undergraduate. Either you’re graduating soon (like me) or you’re in the process of getting your first college degree. The process is not easy and I can only assume how difficult the pressures on Masters and Ph.D. students are. Ho…

打破冷漠僵局文章_保持冷靜并打破僵局-最佳

打破冷漠僵局文章Hack The Box (HTB) is an online platform allowing you to test your penetration testing skills. It contains several challenges that are constantly updated. Some of them simulating real world scenarios and some of them leaning more towards a …

使用DOM Breakpoints找到修改屬性的Javascript代碼

使用Chrome開發者工具的DOM斷點功能可以讓您快速找到修改了某一個DOM元素的Javascript代碼。 在Chrome開發者工具里&#xff0c;選中想要監控的DOM元素&#xff0c;點擊右鍵&#xff0c;選擇Break on->Attributes modifications: 之后在DOM Breakpoints的tab里能看到對應的斷…

特斯拉最安全的車_特斯拉現在是最受歡迎的租車選擇

特斯拉最安全的車Have you been curious to know which cars are most popular in US and what are their typical rental fares in various cities? As the head of Product and Data Science at an emerging technology start-up, Ving Rides, these were some of the quest…

leetcode 740. 刪除并獲得點數(dp)

給你一個整數數組 nums &#xff0c;你可以對它進行一些操作。 每次操作中&#xff0c;選擇任意一個 nums[i] &#xff0c;刪除它并獲得 nums[i] 的點數。之后&#xff0c;你必須刪除每個等于 nums[i] - 1 或 nums[i] 1 的元素。 開始你擁有 0 個點數。返回你能通過這些操作…

WebSocket入門

WebSocket前言  WebSocket是HTML5的重要特性&#xff0c;它實現了基于瀏覽器的遠程socket&#xff0c;它使瀏覽器和服務器可以進行全雙工通信&#xff0c;許多瀏覽器&#xff08;Firefox、Google Chrome和Safari&#xff09;都已對此做了支持。 在WebSocket出現之前&#xff…

安卓游戲開發推箱子_保持冷靜并砍箱子-開發

安卓游戲開發推箱子Hack The Box (HTB) is an online platform allowing you to test your penetration testing skills. It contains several challenges that are constantly updated. Some of them simulating real world scenarios and some of them leaning more towards …

自定義TabLayout

本文為kotlin仿開眼視頻Android客戶端的后續補充內容&#xff0c;本篇為大家介紹如何對TabLayout進行定制使用&#xff0c;基于項目需求&#xff0c;本篇主要對部分功能進行了定制&#xff0c;如&#xff1a;指示器距離文字的距離、文字選中加粗、文字選中變大等 本文部分代碼參…

ml dl el學習_DeepChem —在生命科學和化學信息學中使用ML和DL的框架

ml dl el學習Application of Machine Learning and Deep Learning for Drug Discovery, Genomics, Microsocopy and Quantum Chemistry can create radical impact and holds the potential to significantly accelerate the process of medical research and vaccine developm…

響應式網站設計_通過這個免費的四小時課程,掌握響應式網站設計

響應式網站設計This video tutorial from Kevin Powell teaches you to build responsive websites from scratch. 凱文鮑威爾(Kevin Powell)的這段視頻教程教您從頭開始構建響應式網站。 The course starts with explaining the core concepts needed to start thinking resp…