[Swift]LeetCode554. 磚牆 | Brick Wall

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-bfzrtzpa-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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 leastbricks.git

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.github

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. ide

Example:this

Input: [[1,2,2,1],
        [3,1,2],
        [1,3,2],
        [2,4],
        [3,1,2],
        [1,3,1,1]]

Output: 2

Explanation: 

 

Note:spa

  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.

你的面前有一堵方形的、由多行磚塊組成的磚牆。 這些磚塊高度相同可是寬度不一樣。你如今要畫一條自頂向下的、穿過最少磚塊的垂線。code

磚牆由行的列表表示。 每一行都是一個表明從左至右每塊磚的寬度的整數列表。htm

若是你畫的線只是從磚塊的邊緣通過,就不算穿過這塊磚。你須要找出怎樣畫才能使這條線穿過的磚塊數量最少,而且返回穿過的磚塊數量。blog

你不能沿着牆的兩個垂直邊緣之一畫線,這樣顯然是沒有穿過一塊磚的。 

示例:

輸入: [[1,2,2,1],
      [3,1,2],
      [1,3,2],
      [2,4],
      [3,1,2],
      [1,3,1,1]]

輸出: 2

解釋: 

 

提示:

  1. 每一行磚塊的寬度之和應該相等,而且不能超過 INT_MAX。
  2. 每一行磚塊的數量在 [1,10,000] 範圍內, 牆的高度在 [1,10,000] 範圍內, 總的磚塊數量不超過 20,000。

264ms

 1 class Solution {
 2     func leastBricks(_ wall: [[Int]]) -> Int {
 3         guard !wall.isEmpty else {
 4             return 0
 5         }
 6         
 7         var sumToCount: [Int:Int] = [:]
 8         var maxCount = 0
 9         
10         for row in 0..<wall.count {
11             
12             var sum = 0
13             
14             for column in 0..<wall[row].count-1 {
15                 sum += wall[row][column]
16                 
17                 let count = sumToCount[sum, default: 0] + 1
18                 sumToCount[sum] = count
19                 
20                 maxCount = max(maxCount, count)
21             }
22         }
23         
24         return wall.count - maxCount
25     }
26 }

Runtime: 268 ms
Memory Usage: 19.1 MB
 1 class Solution {
 2     func leastBricks(_ wall: [[Int]]) -> Int {
 3         var mx:Int = 0
 4         var m:[Int:Int] = [Int:Int]()
 5         for a in wall
 6         {
 7             var sum:Int = 0
 8             for i in 0..<a.count - 1
 9             {
10                 sum += a[i]
11                 if m[sum] == nil
12                 {
13                     m[sum] = 1
14                 }
15                 else
16                 {
17                     m[sum]! += 1
18                 }                
19                 mx = max(mx, m[sum]!)
20             }
21         }
22         return wall.count - mx
23     }
24 }

268ms

 1 class Solution {
 2     func leastBricks(_ wall: [[Int]]) -> Int {
 3         if wall.isEmpty || wall[0].isEmpty { return 0 }
 4         
 5         var results = [Int: Int]()
 6         for i in 0..<wall.count {
 7             var idx = 0
 8             for j in 0..<wall[i].count - 1 {
 9                 idx += wall[i][j]
10                 results[idx, default:0] += 1
11             }
12         }
13         
14         return wall.count - (results.values.max() ?? 0)
15     }
16 }

352ms

 1 class Solution {
 2     func leastBricks(_ wall: [[Int]]) -> Int {
 3         var map = [Int:Int]()
 4         var minCount = wall.count
 5         wall.forEach { (ele) in
 6             var sum = 0
 7            for i in 0..<ele.count-1 {
 8                 sum += ele[i]
 9                 map[sum] = (map[sum] ?? 0) + 1
10             }
11         }
12         map.forEach { (_, value) in
13             minCount = min(minCount, wall.count - value)
14         }
15         return minCount
16     }
17 }

388ms

 1 class Solution {
 2     func leastBricks(_ wall: [[Int]]) -> Int {
 3         var map: [Int: Int] = [:]
 4         for row in wall {
 5             var sum = 0
 6             for i in 0 ..< (row.count - 1) {
 7                 sum += row[i]
 8                 map[sum] = (map[sum] ?? 0) + 1
 9             }
10         }
11         var res = wall.count
12         for key in map.keys {
13             res = min(res, wall.count - map[key]!)
14         }
15         return res
16     }
17 }

512ms

 1 class Solution {
 2     func leastBricks(_ wall: [[Int]]) -> Int {
 3         var map: [Int:Int] = [:]
 4         for row in wall {
 5             var tmp = 0
 6             for i in 0..<(row.count - 1) {
 7                 tmp += row[i]
 8                 map[tmp] = (map[tmp] ?? 0) + 1
 9             }
10         }
11         var count = wall.count
12         for (key,val) in map {
13             count = min(wall.count - val, count)
14         }
15         return count 
16     }
17 }

640ms

 1 class Solution {
 2     func leastBricks(_ wall: [[Int]]) -> Int {
 3 guard wall.count > 0 else { return 0 }
 4     var offsetsWithHoles = Array(repeating: 0, count: 65536)
 5     for row in wall {
 6         var offset = 0
 7         for brick in row.dropLast() {
 8             offset += brick
 9             offsetsWithHoles[offset] += 1
10         }
11     }
12     return wall.count - offsetsWithHoles.max()!
13     }
14 }
相關文章
相關標籤/搜索