★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-bkdhjtkv-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
You are installing a billboard and want it to have the largest height. The billboard will have two steel supports, one on each side. Each steel support must be an equal height.git
You have a collection of rods
which can be welded together. For example, if you have rods of lengths 1, 2, and 3, you can weld them together to make a support of length 6.github
Return the largest possible height of your billboard installation. If you cannot support the billboard, return 0.微信
Example 1:ide
Input: [1,2,3,6]
Output: 6 Explanation: We have two disjoint subsets {1,2,3} and {6}, which have the same sum = 6.
Example 2:spa
Input: [1,2,3,4,5,6]
Output: 10 Explanation: We have two disjoint subsets {2,3,5} and {4,6}, which have the same sum = 10.
Example 3:code
Input: [1,2]
Output: 0 Explanation: The billboard cannot be supported, so we return 0.
Note:htm
0 <= rods.length <= 20
1 <= rods[i] <= 1000
The sum of rods is at most 5000.
你正在安裝一個廣告牌,並但願它高度最大。這塊廣告牌將有兩個鋼製支架,兩邊各一個。每一個鋼支架的高度必須相等。blog
你有一堆能夠焊接在一塊兒的鋼筋 rods
。舉個例子,若是鋼筋的長度爲 一、2 和 3,則能夠將它們焊接在一塊兒造成長度爲 6 的支架。get
返回廣告牌的最大可能安裝高度。若是無法安裝廣告牌,請返回 0。
示例 1:
輸入:[1,2,3,6] 輸出:6 解釋:咱們有兩個不相交的子集 {1,2,3} 和 {6},它們具備相同的和 sum = 6。
示例 2:
輸入:[1,2,3,4,5,6] 輸出:10 解釋:咱們有兩個不相交的子集 {2,3,5} 和 {4,6},它們具備相同的和 sum = 10。
示例 3:
輸入:[1,2] 輸出:0 解釋:無法安裝廣告牌,因此返回 0。
提示:
0 <= rods.length <= 20
1 <= rods[i] <= 1000
鋼筋的總數最多爲 5000 根
1392ms
1 class Solution { 2 func tallestBillboard(_ rods: [Int]) -> Int { 3 var n:Int = rods.count 4 var h:Int = n/2 5 var o:Int = 10002 6 var ls:[Int] = [Int](repeating:-99999999,count:20005) 7 for i in 0..<Int(pow(3, Double(h))) 8 { 9 var s:Int = 0 10 var ass:Int = 0 11 var v:Int = i 12 for j in 0..<h 13 { 14 var w:Int = v % 3 15 if w == 1 16 { 17 18 } 19 else if w == 0 20 { 21 s += rods[j] 22 ass += rods[j] 23 } 24 else 25 { 26 s -= rods[j] 27 ass += rods[j] 28 } 29 v /= 3 30 } 31 ls[s+o] = max(ls[s+o], ass) 32 } 33 var ret:Int = 0 34 for i in 0..<Int(pow(3, Double(n - h))) 35 { 36 var s:Int = 0 37 var ass:Int = 0 38 var v:Int = i 39 for j in 0..<(n - h) 40 { 41 var w:Int = v % 3 42 if w == 1 43 { 44 45 } 46 else if w == 0 47 { 48 s += rods[j + h] 49 ass += rods[j + h] 50 } 51 else 52 { 53 s -= rods[j + h] 54 ass += rods[j + h] 55 } 56 v /= 3 57 } 58 ret = max(ret, (ls[o-s] + ass) / 2) 59 } 60 return ret 61 } 62 }
1480ms
1 class Solution { 2 struct State: Hashable { 3 let delta: Int 4 let score: Int 5 } 6 7 func make(from half: ArraySlice<Int>) -> [Int : Int] { 8 var states = Set<State>([.init(delta: 0, score: 0)]) 9 for i in half { 10 let x = Set(states.map { State(delta: $0.delta + i, score: $0.score) }) 11 let y = Set(states.map { State(delta: $0.delta, score: $0.score + i) }) 12 states = states.union(x.union(y)) 13 } 14 15 var delta = [Int : Int]() 16 for state in states { 17 delta[state.delta - state.score] = max(delta[state.delta - state.score] ?? 0, state.delta) 18 } 19 20 return delta 21 } 22 23 func tallestBillboard(_ rods: [Int]) -> Int { 24 let count = rods.count 25 let leftDelta = make(from: rods[..<(count / 2)]) 26 let rightDelta = make(from: rods[(count / 2)...]) 27 28 var answer = 0 29 30 for delta in leftDelta.keys { 31 if rightDelta.keys.contains(-delta) { 32 answer = max(answer, leftDelta[delta]! + rightDelta[-delta]!) 33 } 34 } 35 36 return answer 37 } 38 }