File tree 1 file changed +34
-0
lines changed
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Time: O(n * 3^(n/2))
2
+ // Space: O(3^(n/2))
3
+
4
+ class Solution {
5
+ public:
6
+ int tallestBillboard (vector<int >& rods) {
7
+ auto left = dp (rods.cbegin (), rods.cbegin () + rods.size () / 2 );
8
+ auto right = dp (rods.cbegin () + rods.size () / 2 , rods.cend ());
9
+ int result = 0 ;
10
+ for (const auto & kvp : left) {
11
+ if (right.count (kvp.first )) {
12
+ result = max (result,
13
+ left[kvp.first ] + right[kvp.first ] + kvp.first );
14
+ }
15
+ }
16
+ return result;
17
+ }
18
+
19
+ private:
20
+ unordered_map<int , int > dp (vector<int >::const_iterator it,
21
+ vector<int >::const_iterator end) {
22
+ unordered_map<int , int > lookup;
23
+ lookup[0 ] = 0 ;
24
+ for (; it != end; ++it) {
25
+ unordered_map<int , int > tmp (lookup);
26
+ for (const auto & kvp : tmp) {
27
+ lookup[kvp.first + *it] = max (lookup[kvp.first + *it], kvp.second );
28
+ lookup[abs (kvp.first - *it)] = max (lookup[abs (kvp.first - *it)],
29
+ kvp.second + min (kvp.first , *it));
30
+ }
31
+ }
32
+ return lookup;
33
+ }
34
+ };
You can’t perform that action at this time.
0 commit comments