给定一个整数序列作为数组,通过从数组中删除不超过一个元素来确定是否可以获得严格增加的序列。
对于sequence = [1,3,2,1],输出应为 almostIncreasingSequence(sequence)= false; 此数组中没有一个元素可以删除,以便获得严格增加的序列。
对于sequence = [1,3,2],输出应为 almostIncreasingSequence(sequence)= true。 您可以从数组中删除3以获得严格增加的序列[1,2]。或者,您可以删除2以获得严格增加的序列[1,3]。
我为上面所写的代码如下所示,但它对序列不满意[1,2,3,4,3,6] 实际输出= false 预期输出=真
boolean almostIncreasingSequence(int[] sequence) {
int max = Integer.MIN_VALUE, count = 0;
boolean flag = true;
for (int j = 0; j < sequence.length-1 ; j++){
if ( max >= sequence [j] || sequence[j]>=sequence[j+1]){
count++;
}
else
max = sequence[j];
if ( count > 1){
flag = false;
break;
}
}
return flag;
}
[时间限制]:3000ms(java) [input]:array.integer sequence
保证约束: 2≤sequencelength长度≤105, -105≤sequence[i]≤105。
[output]:boolean
如果可以从数组中删除一个元素以获得严格增加的序列,则返回true,否则返回false。
答案 0 :(得分:1)
这是可以通过从原始程序中删除内容来解决的错误之一:
boolean almostIncreasingSequence(int[] sequence) {
int max = Integer.MIN_VALUE, count = 0;
boolean flag = true;
for (int j = 0; j < sequence.length; j++){
if ( max >= sequence[j]){
count++;
}
max = sequence[j];
if ( count > 1){
flag = false;
break;
}
}
return flag;
}
原始版本的主要问题是,发现两次相同的无序位置。只需删除其中一个检查,同时添加一个循环迭代即可解决此问题。
答案 1 :(得分:0)
如果当前大于或等于下一个,则该程序将通过数组元素进行比较。如果是这样,它会比较当前的元素,因为它可能被删除。它会比较下一个元素,因为它可能会被删除。如果删除当前或下一个不会使增加数组返回false。
public static void main(String[] args) {
boolean almostIncreasingSequence = almostIncreasingSequence(new int[] { 1, 2, 3, 4, 99, 4, 3 });
System.out.println(almostIncreasingSequence);
}
static boolean almostIncreasingSequence(int[] sequence) {
int removeCount = 0;
for (int i = 0; i < sequence.length - 1; i++) {
int current = sequence[i];
int next = sequence[i + 1];
if (current >= next) {
removeCount++;
// Try to remove current. Skip removed element(current). Check
// if previous is less than next element around current
// if i is 0 there is no previous element
// if removeCurrent is true it is possible to remove it and
// arrays stays increasing
boolean removeCurrent = i == 0 || sequence[i - 1] < next;
// if removeNext is true it is possible to remove it and arrays
// stays increasing
boolean removeNext = i + 1 == sequence.length - 1 || current < sequence[i + 2];
if (!removeCurrent && !removeNext)
// if current is removed and array isn't increasing and if
// next is removed and array is not increasing,
// increment removeCount to return false
removeCount++;
}
if (removeCount > 1) {
return false;
}
}
return true;
}
它符合所有给定的测试用例。
答案 2 :(得分:0)
根据您的方法,我建议您在for循环中进行以下修改:
for(......):
if s[i] >= s[i+1] count++
else if max >= s[i] count++
else max = s[i]
完成循环后: 如果计数&gt; 1返回false 否则返回真实
答案 3 :(得分:0)
使用简单的暴力方法,创建一个方法,返回一个缺少其中一个元素的数组,然后检查该数组是否正在增加。围绕所有这一切的循环应该有效。
public static void main(String[] args) {
if (IncreasingSequence(new int[] {1,2,3,4,3,6}))
System.out.println("By removing one element the array is increasing");
else
System.out.println("By removing one element the array is Not increasing");
}
private static boolean IncreasingSequence(int[] sequence) {
int[] nextArray;
for (int i = 0; i < sequence.length; i++) {
nextArray = MinusOneElement(sequence, i);
if (CheckSequenceIncreasing(nextArray))
return true;
}
return false;
}
private static boolean CheckSequenceIncreasing(int[] sequence) {
for (int i = 0; i < sequence.length - 1; i++) {
if (sequence[i] >= sequence[i + 1]) {
return false;
}
}
return true;
}
private static int[] MinusOneElement(int[] sequence, int elementToRemove) {
int[] newArray = new int[sequence.length - 1];
int index = 0;
for (int i = 0; i < sequence.length; i++) {
if (i != elementToRemove) {
newArray[index] = sequence[i];
index++;
}
}
return newArray;
}
答案 4 :(得分:0)
对于kotlin程序员
fun almostIncreasingSequence(sequence: MutableList<Int>): Boolean
{
var removeCount = 0;
var i = 0;
while ( i < sequence.size - 1) {
var current = sequence[i];
var next = sequence[i + 1];
if (current >= next) {
removeCount++;
var removeCurrent : Boolean= i == 0 || sequence[i - 1] < next;
var removeNext : Boolean = i + 1 == sequence.size - 1 || current < sequence[i + 2];
if (!removeCurrent && !removeNext)
removeCount++;
}
if (removeCount > 1) {
return false;
}
i++
}
return true;
}