博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode: 554. Brick Wall
阅读量:6320 次
发布时间:2019-06-22

本文共 1954 字,大约阅读时间需要 6 分钟。

There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The bricks have the same height but different width. You want to draw a vertical line from the top to the bottom and cross the least bricks.

The brick wall is represented by a list of rows. Each row is a list of integers representing the width of each brick in this row from left to right.

If your line go through the edge of a brick, then the brick is not considered as crossed. You need to find out how to draw the line to cross the least bricks and return the number of crossed bricks.

 

You cannot draw a line just along one of the two vertical edges of the wall, in which case the line will obviously cross no bricks.

Example:

Note:

  1. The width sum of bricks in different rows are the same and won't exceed INT_MAX.
  2. The number of bricks in each row is in range [1,10,000]. The height of wall is in range [1,10,000]. Total number of bricks of the wall won't exceed 20,000.

 

我的解决方案:

每个空隙处是以本组元素前面所有元素的叠加+0.1作为key的一个空隙,

例如[1,2,3,4]2与3之间的空隙就是2.1,3与4之间的空隙就是6.1

遍历每组数据,找到重复最多空隙的次数,

以第一维数组的长度减去这个值就是需求的结果了

 

源代码:

1 package main 2  3 import ( 4     "fmt" 5 ) 6  7 var wall = [][]int{
{1, 2, 2, 1}, {3, 1, 2}, {1, 3, 2}, {2, 4}, {3, 1, 2}, {1, 3, 1, 1}} 8 9 func main() {10 fmt.Println(wall)11 leastBricks(wall)12 }13 14 func leastBricks(wall [][]int) int {15 m := make(map[int]int)16 len1 := len(wall)17 max := 018 var len2 int19 var tKey int20 for i := 0; i < len1; i++ {21 len2 = len(wall[i]) - 122 tKey = 023 for j := 0; j < len2; j++ {24 tKey += wall[i][j]25 26 _, ok := m[tKey]27 if ok {28 m[tKey]++29 } else {30 m[tKey] = 131 }32 if m[tKey] > max {33 max = m[tKey]34 }35 }36 }37 return len1 - max38 }

 

转载于:https://www.cnblogs.com/adoontheway/p/6727689.html

你可能感兴趣的文章
103. Binary Tree Zigzag Level Order Traversal
查看>>
JavaScript函数式编程,真香之组合(一)
查看>>
使用Envoy 作Sidecar Proxy的微服务模式-3.分布式追踪
查看>>
深入了解以太坊
查看>>
SpringBoot 实战 (二) | 第一个 SpringBoot 工程详解
查看>>
Go goroutine理解
查看>>
IDE 插件新版本发布,开发效率 “biu” 起来了
查看>>
如何让被遮挡层可以进行事件点击?(纯CSS方法)
查看>>
理解环境变量 JAVA_TOOL_OPTIONS
查看>>
Java Bridge Pattern(桥接模式)
查看>>
看大牛是如何使用和理解线程池
查看>>
sql server 索引阐述系列八 统计信息
查看>>
c# Request对象(13)
查看>>
USB,蓝牙,以太网,还是WIFI?
查看>>
阿里云服务器更改时区为utc
查看>>
APP测试流程和测试点
查看>>
ansible实战
查看>>
PowerShell 远程管理之启用和执行命令
查看>>
mysql安装错误
查看>>
马斯克:我并不讨厌苹果 Apple Watch还不成熟
查看>>