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;
#include <set>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
for (auto &i : A)
}
// 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 <set>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<typename _Key>
for (auto &i : A)
}
// 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 <set>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
}
// 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 <set>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
sorted.
}
// 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 <set>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
sorted.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;
#include <set>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
sorted.insert
}
// 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 <set>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
sorted.inse
}
// 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 <set>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
}
}
// 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 <set>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
}
}
// 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 <set>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
}
}
// 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 <set>
#include <
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
}
}
// 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 <set>
#include <utility>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
}
}
// 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 <set>
#include <utility>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
if (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;
#include <set>
#include <utility>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
if (i < L)
}
}
// 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 <set>
#include <utility>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short count
if (i < L)
}
}
// 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 <set>
#include <utility>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (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;
#include <set>
#include <utility>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
}
}
// 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 <set>
#include <utility>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
std::pair<
}
}
// 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 <set>
#include <utility>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
std::pair<
}
}
// 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 <set>
#include <utility>
typedef std::set<int>
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
std::pair<
}
}
// 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 <set>
#include <utility>
typedef std::multiset<int>::iterator
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
std::pair<
}
}
// 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 <set>
#include <utility>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
std::pair<
}
}
// 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 <set>
#include <utility>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
std::pair<It, It>
}
}
// 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 <set>
#include <utility>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
std::pair<It, It> test = sorted.equal_range()
}
}
// 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 <set>
#include <utility>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
std::pair<It, It> test = sorted.equal_range(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;
#include <set>
#include <utility>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
std::pair<It, It> test = sorted.equal_range(i);
if
}
}
// 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 <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
std::pair<It, It> test = sorted.equal_range(i);
if (std::)
}
}
// 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 <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
std::pair<It, It> test = sorted.equal_range(i);
if (std::count )
}
}
// 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 <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
std::pair<It, It> test = sorted.equal_range(i);
if (std::count \)
}
}
// 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 <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first(), test.second()))
}
}
// 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 <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first(), test.second()) < limit)
}
}
// 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 <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
}
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first(), test.second()) < limit)
}
}
// 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 <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first(), test.second()) < limit)
}
}
}
// 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 <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first(), test.second()) < limit)
}
}
}
// 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 <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first(), test.second()) < limit)
sorted.insert
}
}
}
// 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 <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first(), test.second()) < limit)
sorted.insert()
}
}
}
// 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 <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first(), test.second()) < limit)
sorted.inser
}
}
}
// 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 <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first(), test.second()) < limit)
sorted.emplace_hint(test.second(), _Args &&__args...)
}
}
}
// 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 <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first(), test.second()) < limit)
sorted.emplace_hint(test.second(), 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;
#include <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first(), test.second()) < limit)
sorted.emplace_hint(test.second(), 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;
#include <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first(), test.second()) < limit)
sorted.emplace_hint(test.second(), i);
}
}
return sorted.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;
#include <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first(), test.second()) < limit)
sorted.emplace_hint(test.second(), i);
}
}
return sorted.size();
}
func.cpp: In function 'int solution(std::vector<int>&, int, int)': func.cpp:26:39: error: no match for call to '(std::_Rb_tree_const_iterator<int>) ()' if (std::count(test.first(), test.second()) < limit) ^ func.cpp:26:54: error: no match for call to '(std::_Rb_tree_const_iterator<int>) ()' if (std::count(test.first(), test.second()) < limit) ^ func.cpp:27:49: error: no match for call to '(std::_Rb_tree_const_iterator<int>) ()' sorted.emplace_hint(test.second(), 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;
#include <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first, test.second()) < limit)
sorted.emplace_hint(test.second(), i);
}
}
return sorted.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;
#include <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first, test.second) < limit)
sorted.emplace_hint(test.second, i);
}
}
return sorted.size();
}
func.cpp: In function 'int solution(std::vector<int>&, int, int)': func.cpp:26:51: error: no matching function for call to 'count(std::_Rb_tree_const_iterator<int>&, std::_Rb_tree_const_iterator<int>&)' if (std::count(test.first, test.second) < limit) ^ In file included from /opt/lang/gcc/include/c++/6.2.0/algorithm:62:0, from func.cpp:9: /opt/lang/gcc/include/c++/6.2.0/bits/stl_algo.h:3961:5: note: candidate: template<class _IIter, class _Tp> typename std::iterator_traits<_Iterator>::difference_type std::count(_IIter, _IIter, const _Tp&) count(_InputIterator __first, _InputIterator __last, const _Tp& __value) ^~~~~ /opt/lang/gcc/include/c++/6.2.0/bits/stl_algo.h:3961:5: note: template argument deduction/substitution failed: func.cpp:26:51: note: candidate expects 3 arguments, 2 provided if (std::count(test.first, test.second) < limit) ^
// 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 <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first, test.second,) < limit)
sorted.emplace_hint(test.second, i);
}
}
return sorted.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;
#include <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first, test.second, i) < limit)
sorted.emplace_hint(test.second, i);
}
}
return sorted.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;
#include <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first, test.second, i) < limit)
sorted.emplace_hint(test.second, i);
}
}
return sorted.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;
#include <set>
#include <utility>
#include <algorithm>
typedef std::multiset<int>::iterator It;
int solution(vector<int> &A, int L, int R) {
// write your code in C++14 (g++ 6.2.0)
std::multiset<int> sorted;
for (auto &i : A)
{
short limit = 0;
if (i < L)
limit++;
if (i > R)
limit++;
if (limit > 0)
{
std::pair<It, It> test = sorted.equal_range(i);
if (std::count(test.first, test.second, i) < limit)
sorted.emplace_hint(test.second, i);
}
}
return sorted.size();
}
The solution obtained perfect score.
Small random tests, every value occurs at least twice. N = 100.