Skip to content

Commit 13a8217

Browse files
authored
Add files via upload
1 parent 2a56e5d commit 13a8217

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

leetcode/956.最高的广告牌.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @lc app=leetcode.cn id=956 lang=cpp
3+
*
4+
* [956] 最高的广告牌
5+
*/
6+
7+
#include<bits/stdc++.h>
8+
using namespace std;
9+
// @lc code=start
10+
class Solution {
11+
public:
12+
int tallestBillboard(vector<int>& rods) {
13+
if(rods.empty())
14+
return 0;
15+
int sum = accumulate(rods.begin(), rods.end(), 0);
16+
int n = rods.size();
17+
vector<vector<int> > dp(n+1,vector<int>(2*sum +1, INT32_MIN /2));
18+
dp[0][sum] = 0;
19+
for(int i=0 ; i<n ;++i){
20+
for(int j = rods[i]; j<=2*sum-rods[i] ; ++j){
21+
dp[i+1][j] = max({dp[i][j], dp[i][j-rods[i]] + rods[i], dp[i][j+rods[i]]});
22+
}
23+
}
24+
25+
for(int i=0 ; i<n ; ++i)
26+
cout << dp[i][sum] << '\n';
27+
return dp[n][sum];
28+
}
29+
};
30+
// @lc code=end
31+
32+
int main(){
33+
vector<int> t({3,4,3,3,2});
34+
cout << Solution().tallestBillboard(t);
35+
return 0;
36+
37+
}
38+

0 commit comments

Comments
 (0)