%정리하고싶음
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.
주어진 정수 배열에서, 오름차순 배열이 되는지 확인해라 (최대 1개까지 삭제 가능)
//Process
//1. Input integer sequence
//2. Check if it's ascending vector or not
//3. Check if it's ascending vector after erasing front error number index
//4. Check if it's ascending vector after erasing rear error number index
//5. Return result
//처리과정
//1. 정수 배열을 입력받는다.
//2. 오름차순 배열인지 확인한다.
//3. 오름차순 배열이 아니면 아닌 부분의 앞쪽 인덱스 정수를 지우고 다시 확인한다.
//4. 그래도 오름차순 배열이 아니면 아닌 부분의 뒷쪽 인덱스 정수를 지우고 다시 확인한다.
//5. 결과를 리턴한다.
bool almostIncreasingSequence(std::vector<int> sequence) {
bool answer = false;
int i = 0;
int j;
int length = sequence.size();
int front = 0;
int next = front + 1;
int count = 0;
while (i < length - 1 && count == 0) {
if (sequence[front] >= sequence[next]) {
++count;
}
front = next;
next = front + 1;
++i;
}
if (count == 0) {
answer = true;
}
if (answer == false) {
std::vector<int> sequence1 = sequence;
sequence1.erase(sequence1.begin() + i - 1);
front = 0;
next = front + 1;
count = 0;
j = 0;
while (j < sequence1.size() - 1 && count == 0) {
if (sequence1[front] >= sequence1[next]) {
++count;
}
front = next;
next = front + 1;
++j;
}
if (count == 0) {
answer = true;
}
}
if (answer == false) {
std::vector<int> sequence2 = sequence;
sequence2.erase(sequence2.begin() + i);
front = 0;
next = front + 1;
count = 0;
j = 0;
while (j < sequence2.size() - 1 && count == 0) {
if (sequence2[front] >= sequence2[next]) {
++count;
}
front = next;
next = front + 1;
++j;
}
if (count == 0) {
answer = true;
}
}
return answer;
}
2018/09/23 - [Algorithm/Code Signal (Code Fights)] - Aracade Intro #7 AlmostIncreasingSequence
2018/09/22 - [Algorithm/Code Signal (Code Fights)] - Aracade Intro #6 MakeArrayConsecutive2
2018/09/21 - [Algorithm/Code Signal (Code Fights)] - Aracade Intro #5 ShapeArea
2018/09/19 - [Algorithm/Code Signal (Code Fights)] - Aracade Intro #4 AdjacentElementsProduct
2018/09/16 - [Algorithm/Code Signal (Code Fights)] - Aracade Intro #3 CheckPalindrome
2018/09/16 - [Algorithm/Code Signal (Code Fights)] - Aracade Intro #2 CenturyFromYear
2018/09/16 - [Algorithm/Code Signal (Code Fights)] - Aracade Intro #1 Add
'Algorithm > Code Fights (Code Signal)' 카테고리의 다른 글
Aracade Intro #9 AllLongestStrings (0) | 2018.09.26 |
---|---|
Aracade Intro #8 MatrixElementsSum (0) | 2018.09.24 |
Aracade Intro #6 MakeArrayConsecutive2 (0) | 2018.09.22 |
Aracade Intro #5 ShapeArea (0) | 2018.09.21 |
Aracade Intro #4 AdjacentElementsProduct (0) | 2018.09.19 |