There are N rectangular buildings standing along the road next to each other. The K-th building is of size H[K] × 1.
Because a renovation of all of the buildings is planned, we want to cover them with rectangular banners until the renovations are finished. Of course, to cover a building, the banner has to be at least as high as the building. We can cover more than one building with a banner if it is wider than 1.
For example, to cover buildings of heights 3, 1, 4 we could use a banner of size 4×3 (i.e. of height 4 and width 3), marked here in blue:
We can order at most two banners and we want to cover all of the buildings. Also, we want to minimize the amount of material needed to produce the banners.
What is the minimum total area of at most two banners which cover all of the buildings?
Write a function:
int solution(vector<int> &H);
that, given an array H consisting of N integers, returns the minimum total area of at most two banners that we will have to order.
Examples:
1. Given H = [3, 1, 4], the function should return 10. The result can be achieved by covering the first two buildings with a banner of size 3×2 and the third building with a banner of size 4×1:
2. Given H = [5, 3, 2, 4], the function should return 17. The result can be achieved by covering the first building with a banner of size 5×1 and the other buildings with a banner of size 4×3:
3. Given H = [5, 3, 5, 2, 1], your function should return 19. The result can be achieved by covering the first three buildings with a banner of size 5×3 and the other two with a banner of size 2×2:
4. Given H = [7, 7, 3, 7, 7], your function should return 35. The result can be achieved by using one banner of size 7×5:
5. Given H = [1, 1, 7, 6, 6, 6], your function should return 30. The result can be achieved by using banners of size 1×2 and 7×4:
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array H is an integer within the range [1..10,000].
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// fi
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.append();
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.append(*max_elemnet(H.begin(),));
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.append(*max_elemnet(H.begin(), H.end()) );
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.append(*max_elemnet(H.begin(), H.end()) * H.size());
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.append(*max_element(H.begin(), H.end()) * H.size());
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.append(*max_element(H.begin(), H.end()) * H.size());
return
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.append(*max_element(H.begin(), H.end()) * H.size());
return *min_element(sum)
}
// you can use includes, for example:
// #include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.append(*max_element(H.begin(), H.end()) * H.size());
return *min_element(sum);
}
func.cpp: In function 'int solution(std::vector<int>&)': func.cpp:15:9: error: 'class std::vector<int>' has no member named 'append'; did you mean 'end'? sum.append(*max_element(H.begin(), H.end()) * H.size()); ^~~~~~ func.cpp:15:47: error: 'max_element' was not declared in this scope sum.append(*max_element(H.begin(), H.end()) * H.size()); ^ func.cpp:17:28: error: 'min_element' was not declared in this scope return *min_element(sum); ^
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.append(*max_element(H.begin(), H.end()) * H.size());
return *min_element(sum);
}
func.cpp: In function 'int solution(std::vector<int>&)': func.cpp:15:9: error: 'class std::vector<int>' has no member named 'append'; did you mean 'end'? sum.append(*max_element(H.begin(), H.end()) * H.size()); ^~~~~~ func.cpp:17:28: error: no matching function for call to 'min_element(std::vector<int>&)' return *min_element(sum); ^ In file included from /opt/lang/gcc/include/c++/6.2.0/algorithm:62:0, from func.cpp:2: /opt/lang/gcc/include/c++/6.2.0/bits/stl_algo.h:5475:12: note: candidate: template<class _FIter> constexpr _FIter std::min_element(_FIter, _FIter) inline min_element(_ForwardIterator __first, _ForwardIterator __last) ^~~~~~~~~~~ /opt/lang/gcc/include/c++/6.2.0/bits/stl_algo.h:5475:12: note: template argument deduction/substitution failed: func.cpp:17:28: note: candidate expects 2 arguments, 1 provided return *min_element(sum); ^ In file included from /opt/lang/gcc/include/c++/6.2.0/algorithm:62:0, from func.cpp:2: /opt/lang/gcc/include/c++/6.2.0/bits/stl_algo.h:5500:5: note: candidate: template<class _FIter, class _Compare> constexpr _FIter std::min_element(_FIter, _FIter, _Compare) min_element(_ForwardIterator __first, _ForwardIterator __last, ^~~~~~~~~~~ /opt/lang/gcc/include/c++/6.2.0/bits/stl_algo.h:5500:5: note: template argument deduction/substitution failed: func.cpp:17:28: note: candidate expects 3 arguments, 1 provided return *min_element(sum); ^
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.pu(*max_element(H.begin(), H.end()) * H.size());
return *min_element(sum);
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i)
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 0; i<H.size();i++)
{
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
);
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
*max_element(H.begin(),H.end()-1));
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
*max_element(H.begin(),H.end()-1)*);
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
*max_element(H.begin(),H.end()-i)*);
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
*max_element(H.begin(),H.end()-i)* H.end());
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i)* H.end()-i));
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i)* H.end()-i)
+ ());
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i)* H.end()-i)
+ (*max_element(H.end()-i,H.end())));
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i)* H.end()-i)
+ (*max_element(H.end()-i,H.end())*));
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i)* H.end()-i)
+ (*max_element(H.end()-i,H.end())*i));
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i)* H.end()-i)
+ (*max_element(H.end()-i,H.end())*i));
}
return *min_element(sum.begin(),sum.end());
}
func.cpp: In function 'int solution(std::vector<int>&)': func.cpp:16:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(int i = 1; i<H.size();i++) ~^~~~~~~~~ func.cpp:19:47: error: no match for 'operator*' (operand types are 'int' and 'std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}') (*max_element(H.begin(),H.end()-i)* H.end()-i) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i)* (H.end()-i)
+ (*max_element(H.end()-i,H.end())*i));
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i)* (H.end()-i))
+ (*max_element(H.end()-i,H.end())*i));
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i) * (H.end()-i))
+ (*max_element(H.end()-i,H.end())*i));
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i) * (H.end()-i))
+ (*max_element(H.end()-i,H.end()) * i));
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i) * (H.end()-i))
+ (*max_element(H.end()-i,H.end()) * i));
}
return *min_element(sum.begin(),sum.end());
}
func.cpp: In function 'int solution(std::vector<int>&)': func.cpp:16:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(int i = 1; i<H.size();i++) ~^~~~~~~~~ func.cpp:19:48: error: no match for 'operator*' (operand types are 'int' and '__gnu_cxx::__normal_iterator<int*, std::vector<int> >') (*max_element(H.begin(),H.end()-i) * (H.end()-i)) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i) * (H.size()-i))
+ (*max_element(H.end()-i,H.end()) * i));
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(int i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i) * (H.size()-i))
+ (*max_element(H.end()-i,H.end()) * i));
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(si i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i) * (H.size()-i))
+ (*max_element(H.end()-i,H.end()) * i));
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(size_t i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i) * (H.size()-i))
+ (*max_element(H.end()-i,H.end()) * i));
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(size_t i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i) * (H.size()-i))
+ (*max_element(H.end()-i,H.end()) * i));
}
return *min_element(sum.begin(),sum.end());
}
// you can use includes, for example:
#include <algorithm>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)
// find the max which becomes height
// calculate the area for 1 banner.
// calculate the area for 2 banner.
// find the smallest sum for 2 banners.
// find the least of 1 banner and 2 banners.
vector<int> sum;
sum.push_back(*max_element(H.begin(), H.end()) * H.size());
for(size_t i = 1; i<H.size();i++)
{
sum.push_back(
(*max_element(H.begin(),H.end()-i) * (H.size()-i))
+ (*max_element(H.end()-i,H.end()) * i));
}
return *min_element(sum.begin(),sum.end());
}
The following issues have been detected: timeout errors.
Every value in the array is different. N=7,500.
running time: 0.164 sec., time limit: 0.100 sec.
Random maximum tests. N=100,000.
Killed. Hard limit reached: 6.000 sec.
Various hard hand-created tests. N=100,000.
Killed. Hard limit reached: 6.000 sec.
Values in the tests are arranged into ascending or descending pyramids. N=100,000.
Killed. Hard limit reached: 6.000 sec.