You are given N disks and two rods, each with one initial disk.
On the left rod, disks can be placed in decreasing order of size (smaller disks on top of bigger ones). On the right rod, disks can be placed in increasing order of size (bigger disks on top of smaller ones). Note that it is not permissible to place two disks of equal size on top of each other. The initial disks cannot be moved.
Write a function:
int solution(vector<int> &A, int L, int R);
that, given an array A of integers representing the sizes of the N disks and two integers L and R representing the size of the initial disks on the left and right rods respectively, returns the maximum number of disks from A that can be placed on the rods while satisfying the rules presented above.
Examples:
1. Given A = [2, 3, 3, 4], L = 3 and R = 1, your function should return 3, since only three disks can be placed on the rods. Note that the disk of size 2 can be placed on either the left rod or the right rod.
2. Given A = [1, 4, 5, 5], L = 6 and R = 4, your function should return 4.
3. Given A = [5, 2, 5, 2], L = 8 and R = 1, your function should return 4.
4. Given A = [1, 5, 5], L = 2 and R = 4, your function should return 2.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..50,000];
- each element of array A is an integer within the range [1..1,000,000,000];
- L and R are integers within the range [1..1,000,000,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> &A, int L, int R) {
sort( begin(v), end(v));
for ( x : v ){
}
// write your code in C++14 (g++ 6.2.0)
}
// 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> &A, int L, int R) {
sort( begin(v), end(v));
for ( x : v ){
cout << x << ",";
}
// write your code in C++14 (g++ 6.2.0)
}
// 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> &A, int L, int R) {
sort( begin(v), end(v));
for ( x : v ){
cout << x << ",";
}
// write your code in C++14 (g++ 6.2.0)
}
func.cpp: In function 'int solution(std::vector<int>&, int, int)': func.cpp:8:17: error: 'v' was not declared in this scope sort( begin(v), end(v)); ^ func.cpp:8:27: error: 'sort' was not declared in this scope sort( begin(v), end(v)); ^ func.cpp:10:13: error: found ':' in nested-name-specifier, expected '::' for ( x : v ){ ^ func.cpp:10:11: error: 'x' has not been declared for ( x : v ){ ^ func.cpp:14:1: error: expected primary-expression before '}' token } ^ func.cpp:14:1: error: expected ';' before '}' token func.cpp:14:1: error: expected primary-expression before '}' token func.cpp:14:1: error: expected ')' before '}' token func.cpp:14:1: error: expected primary-expression before '}' token func.cpp:14:1: warning: no return statement in function returning non-void [-Wreturn-type]
// 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> &A, int L, int R) {
sort( begin(A), end(A));
for ( x : A ){
cout << x << ",";
}
// write your code in C++14 (g++ 6.2.0)
}
func.cpp: In function 'int solution(std::vector<int>&, int, int)': func.cpp:8:27: error: 'sort' was not declared in this scope sort( begin(A), end(A)); ^ func.cpp:10:13: error: found ':' in nested-name-specifier, expected '::' for ( x : A ){ ^ func.cpp:10:11: error: 'x' has not been declared for ( x : A ){ ^ func.cpp:14:1: error: expected primary-expression before '}' token } ^ func.cpp:14:1: error: expected ';' before '}' token func.cpp:14:1: error: expected primary-expression before '}' token func.cpp:14:1: error: expected ')' before '}' token func.cpp:14:1: error: expected primary-expression before '}' token func.cpp:14:1: warning: no return statement in function returning non-void [-Wreturn-type]
// 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> &A, int L, int R) {
sort( begin(A), end(A));
for ( auto x : A ){
cout << x << ",";
}
// write your code in C++14 (g++ 6.2.0)
}
func.cpp: In function 'int solution(std::vector<int>&, int, int)': func.cpp:8:27: error: 'sort' was not declared in this scope sort( begin(A), end(A)); ^ func.cpp:14:1: warning: no return statement in function returning non-void [-Wreturn-type] } ^
// 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> &A, int L, int R) {
sort( begin(A), end(A));
for ( auto x : A ){
cout << x << ",";
}
// write your code in C++14 (g++ 6.2.0)
}
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A));
for ( auto x : A ){
cout << x << ",";
}
// write your code in C++14 (g++ 6.2.0)
}
2,3,3,4,
1,4,5,5,
2,2,5,5,
1,5,5,
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), gr);
for ( auto x : A ){
cout << x << ",";
}
// write your code in C++14 (g++ 6.2.0)
}
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), greater<int>);
for ( auto x : A ){
cout << x << ",";
}
// write your code in C++14 (g++ 6.2.0)
}
func.cpp: In function 'int solution(std::vector<int>&, int, int)': func.cpp:8:41: error: expected primary-expression before ')' token sort( begin(A), end(A), greater<int>); ^ func.cpp:14:1: warning: no return statement in function returning non-void [-Wreturn-type] } ^
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), greater<int>());
for ( auto x : A ){
cout << x << ",";
}
// write your code in C++14 (g++ 6.2.0)
}
4,3,3,2,
5,5,4,1,
5,5,2,2,
5,5,1,
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), greater<int>());
for ( auto x : A ){
cout << x << ",";
}
// write your code in C++14 (g++ 6.2.0)
}
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), greater<int>());
int L;
for ( auto x : A ){
cout << x << ",";
}
// write your code in C++14 (g++ 6.2.0)
}
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), greater<int>());
int L;
vector<int> B;
for ( auto x : A ){
cout << x << ",";
}
// write your code in C++14 (g++ 6.2.0)
}
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), greater<int>());
int lastLeft = L;
int lastRight = R;
vector<int> B;
for ( auto x : A ){
cout << x << ",";
}
// write your code in C++14 (g++ 6.2.0)
}
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), greater<int>());
int lastLeft = L;
int lastRight = R;
int result =
vector<int> B;
for ( auto x : A ){
if ( x < lastLeft ){
lastLeft = x;
}
cout << x << ",";
}
// write your code in C++14 (g++ 6.2.0)
}
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), greater<int>());
int lastLeft = L;
int lastRight = R;
int result = 0;
vector<int> B;
for ( auto x : A ){
if ( x < lastLeft ){
lastLeft = x;
result++;
} else {
B.push_back(x);
}
cout << x << ",";
}
// write your code in C++14 (g++ 6.2.0)
}
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), greater<int>());
int lastLeft = L;
int lastRight = R;
int result = 0;
vector<int> B;
for ( auto x : A ){
if ( x < lastLeft ){
lastLeft = x;
result++;
} else {
B.push_back(x);
}
}
return result;
// write your code in C++14 (g++ 6.2.0)
}
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), greater<int>());
int lastLeft = L;
int lastRight = R;
int result = 0;
vector<int> B;
for ( auto x : A ){
if ( x < lastLeft ){
lastLeft = x;
result++;
} else {
B.push_back(x);
}
}
sort( begin(B), end(b) );
return result;
// write your code in C++14 (g++ 6.2.0)
}
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), greater<int>());
int lastLeft = L;
int lastRight = R;
int result = 0;
vector<int> B;
for ( auto x : A ){
if ( x < lastLeft ){
lastLeft = x;
result++;
} else {
B.push_back(x);
}
}
sort( begin(B), end(b) );
for ( auto x : B ){
if ( x > lastRight )
}
return result;
// write your code in C++14 (g++ 6.2.0)
}
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), greater<int>());
int lastLeft = L;
int lastRight = R;
int result = 0;
vector<int> B;
for ( auto x : A ){
if ( x < lastLeft ){
lastLeft = x;
result++;
} else {
B.push_back(x);
}
}
sort( begin(B), end(b) );
for ( auto x : B ){
if ( x > lastRight ){
lastRight = x;
result++;
}
}
return result;
// write your code in C++14 (g++ 6.2.0)
}
func.cpp: In function 'int solution(std::vector<int>&, int, int)': func.cpp:23:25: error: 'b' was not declared in this scope sort( begin(B), end(b) ); ^
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), greater<int>());
int lastLeft = L;
int lastRight = R;
int result = 0;
vector<int> B;
for ( auto x : A ){
if ( x < lastLeft ){
lastLeft = x;
result++;
} else {
B.push_back(x);
}
}
sort( begin(B), end(B) );
for ( auto x : B ){
if ( x > lastRight ){
lastRight = x;
result++;
}
}
return result;
// write your code in C++14 (g++ 6.2.0)
}
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), greater<int>());
int lastLeft = L;
int lastRight = R;
int result = 0;
vector<int> B;
for ( auto x : A ){
if ( x < lastLeft ){
lastLeft = x;
result++;
} else {
B.push_back(x);
}
}
sort( begin(B), end(B) );
for ( auto x : B ){
if ( x > lastRight ){
lastRight = x;
result++;
}
}
return result;
// write your code in C++14 (g++ 6.2.0)
}
// 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;
#include<algorithm>
int solution(vector<int> &A, int L, int R) {
sort( begin(A), end(A), greater<int>());
int lastLeft = L;
int lastRight = R;
int result = 0;
vector<int> B;
for ( auto x : A ){
if ( x < lastLeft ){
lastLeft = x;
result++;
} else {
B.push_back(x);
}
}
sort( begin(B), end(B) );
for ( auto x : B ){
if ( x > lastRight ){
lastRight = x;
result++;
}
}
return result;
// write your code in C++14 (g++ 6.2.0)
}
The solution obtained perfect score.
Small random tests, every value occurs at least twice. N = 100.