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.
With Python3, I started with something like this...
def almostIncreasingSequence(sequence):
for i, x in enumerate(sequence):
ret = False
s = sequence[:i]+sequence[i+1:]
for j, y in enumerate(s[1:]):
if s[j+1] <= s[j]:
ret = True
break
if ret:
break
if not ret:
return True
return False
But kept timing out on Check #29.
I kicked myself when I realized that this works, too, but still times out on #29. I have no idea how to speed it up.
def almostIncreasingSequence(sequence):
for i, x in enumerate(sequence):
s = sequence[:i]
s.extend(sequence[i+1:])
if s == sorted(set(s)):
return True
return False
boolean almostIncreasingSequence(int[] sequence) {
int length = sequence.length;
if(length ==1) return true;
if(length ==2 && sequence[1] > sequence[0]) return true;
int count = 0;
int index = 0;
boolean iter = true;
while(iter){
index = checkSequence(sequence,index);
if(index != -1){
count++;
index++;
if(index >= length-1){
iter=false;
}else if(index-1 !=0){
if(sequence[index-1] <= sequence[index]){
iter=false;
count++;
}else if(((sequence[index] <= sequence[index-2])) && ((sequence[index+1] <= sequence[index-1]))){
iter=false;
count++;
}
}
}else{
iter = false;
}
}
if(count > 1) return false;
return true;
}
int checkSequence(int[] sequence, int index){
for(; index < sequence.length-1; index++){
if(sequence[index+1] <= sequence[index]){
return index;
}
}
return -1;
}
def almostIncreasingSequence(sequence):
if len(sequence) == 1:
return False
if len(sequence) == 2:
return True
c = 0
c1 = 0
for i in range(1,len(sequence)):
if sequence[i-1] >= sequence[i]:
c += 1
if i != 0 and i+1 < len(sequence):
if sequence[i-1] >= sequence[i+1]:
c1 += 1
if c > 1 or c1 > 1:
return False
return c1 == 1 or c == 1
Below is the Python3 code that I used and it worked fine:
def almostIncreasingSequence(sequence):
flag = False
if(len(sequence) < 3):
return True
if(sequence == sorted(sequence)):
if(len(sequence)==len(set(sequence))):
return True
bigFlag = True
for i in range(len(sequence)):
if(bigFlag and i < len(sequence)-1 and sequence[i] < sequence[i+1]):
bigFlag = True
continue
tempSeq = sequence[:i] + sequence[i+1:]
if(tempSeq == sorted(tempSeq)):
if(len(tempSeq)==len(set(tempSeq))):
flag = True
break
bigFlag = False
return flag
This one works well.
bool almostIncreasingSequence(std::vector<int> sequence) {
/*
if(is_sorted(sequence.begin(), sequence.end())){
return true;
}
*/
int max = INT_MIN;
int secondMax = INT_MIN;
int count = 0;
int i = 0;
while(i < sequence.size()){
if(sequence[i] > max){
secondMax = max;
max = sequence[i];
}else if(sequence[i] > secondMax){
max = sequence[i];
count++;
cout<<"count after increase = "<<count<<endl;
}else {count++; cout<<"ELSE count++ = "<<count<<endl;}
i++;
}
return count <= 1;
}
This is mine. Hope you find this helpful:
def almostIncreasingSequence(sequence):
#Take out the edge cases
if len(sequence) <= 2:
return True
#Set up a new function to see if it's increasing sequence
def IncreasingSequence(test_sequence):
if len(test_sequence) == 2:
if test_sequence[0] < test_sequence[1]:
return True
else:
for i in range(0, len(test_sequence)-1):
if test_sequence[i] >= test_sequence[i+1]:
return False
else:
pass
return True
for i in range (0, len(sequence) - 1):
if sequence[i] >= sequence [i+1]:
#Either remove the current one or the next one
test_seq1 = sequence[:i] + sequence[i+1:]
test_seq2 = sequence[:i+1] + sequence[i+2:]
if IncreasingSequence(test_seq1) == True:
return True
elif IncreasingSequence(test_seq2) == True:
return True
else:
return False