Given a sequence of integers as an array, determine whether it's possible to obtain a strictly increasing sequence by REM Oving no more than one element from the array.
Example
sequence = [1, 3, 2, 1]for, the output should is
almostIncreasingSequence(sequence) = false.
There is the no one element in the this array, the can removed in order to get a strictly increasing sequence.
sequence = [1, 3, 2]for, the output should is
almostIncreasingSequence(sequence) = true.
You can remove from the 3 array to get the strictly increasing sequence [1, 2] . Alternately, you can remove to 2 get the strictly increasing sequence [1, 3] .
Input/output
[Execution time limit] 0.5 seconds (CPP)
[Input] Array.integer sequence
Guaranteed constraints:
2 ≤ sequence.length ≤ 105,
-105 ≤ sequence[i] ≤ 105.
[Output] Boolean
- Return
true If it is possible to remove one element from the array in order to get a strictly increasing sequence, otherw Ise return false .
C + + Solution:
1 BOOLIssequence (vector<int>sequence)2 {3 Set<int>Iset;4 for(auto e:sequence)5 Iset.insert (e);6 if(Iset.size ()! = Sequence.size ())//returns False if there are duplicate elements in the sequence7 return false;8 9vector<int>v = sequence;//If the sequence is equal to the sorted V, the sequence is incremented and orderedTen sort (V.begin (), V.end ()); One if(Sequence = =v) A return true; - Else - return false; the - } - - BOOLAlmostincreasingsequence (std::vector<int>sequence) + { - if(issequence (sequence)) + return true; A inti =0; at while(I <sequence.size ()) - { -vector<int> v =sequence; -V.erase (V.begin () +i); - if(Issequence (v)) - return true; in++i; - } to return false; +}
codesignal Brush Problem--almostincreasingsequence