codesignal Brush Problem--almostincreasingsequence

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.