Java程序在删除最多一个元素后测试严格递增的数组

Java程序在删除最多一个元素后测试严格递增的数组,java,arrays,Java,Arrays,给定一个整数序列作为数组,确定是否可以通过从数组中删除不超过一个元素来获得严格递增序列 对于sequence=[1,3,2,1],输出应该是 几乎递增序列(序列)=假; 这个数组中没有一个元素可以被移除,以获得严格递增的序列 对于sequence=[1,3,2],输出应该是 几乎递增序列(序列)=真。 您可以从数组中删除3以获得严格递增的序列[1,2]。或者,您可以删除2以获得严格递增的序列[1,3] 我为上面编写的代码如下所示,但它不能满足序列[1,2,3,4,3,6] 实际输出=假 预期输出

给定一个整数序列作为数组,确定是否可以通过从数组中删除不超过一个元素来获得严格递增序列

对于sequence=[1,3,2,1],输出应该是 几乎递增序列(序列)=假; 这个数组中没有一个元素可以被移除,以获得严格递增的序列

对于sequence=[1,3,2],输出应该是 几乎递增序列(序列)=真。 您可以从数组中删除3以获得严格递增的序列[1,2]。或者,您可以删除2以获得严格递增的序列[1,3]

我为上面编写的代码如下所示,但它不能满足序列[1,2,3,4,3,6] 实际输出=假 预期输出=真

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; 
}
布尔几乎递增序列(int[]序列){
int max=Integer.MIN_值,计数=0;
布尔标志=真;
对于(int j=0;j=序列[j]| |序列[j]>=序列[j+1]){
计数++;
}
其他的
max=序列[j];
如果(计数>1){
flag=false;
打破
}
}        
返回标志;
}
[时限]:3000ms(java) [输入]:数组.整数序列

保证约束条件: 2.≤ 序列长度≤ 105, -105 ≤ 序列[i]≤ 105

[输出]:布尔值


如果可以从数组中删除一个元素以获得严格递增的序列,则返回true,否则返回false。

此程序将通过数组元素比较当前值是否大于或等于下一个值。如果是这样,它会比较当前元素周围的元素,因为它可能会被删除。它会比较next周围的元素,因为它可能会被删除。如果删除当前或下一个并没有使递增数组返回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;
}
publicstaticvoidmain(字符串[]args){
布尔值almostIncreasingSequence=almostIncreasingSequence(新int[]{1,2,3,4,99,4,3});
System.out.println(几乎递增顺序);
}
静态布尔值几乎递增序列(int[]序列){
int removeCount=0;
对于(int i=0;i=下一个){
removeCount++;
//尝试删除当前元素。跳过删除的元素(当前)。检查
//如果“上一个”小于“当前”周围的下一个元素
//如果i为0,则不存在上一个元素
//如果removeCurrent为true,则可以删除它并
//阵列持续增加
布尔removeCurrent=i==0 | |序列[i-1]1){
返回false;
}
}
返回true;
}

它满足所有给定的测试用例。

根据您的方法,我建议您在for循环中进行以下修改:

对于(……):

完成for循环后: 如果计数>1,则返回false
否则返回true

使用简单的蛮力方法,创建一个返回缺少其中一个元素的数组的方法,然后检查该数组是否在增加。围绕这一切的一个循环应该是可行的

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;
}
publicstaticvoidmain(字符串[]args){
if(递增序列(新int[]{1,2,3,4,3,6}))
System.out.println(“通过删除一个元素,数组将增加”);
其他的
System.out.println(“通过删除一个元素,数组不会增加”);
}
私有静态布尔递增序列(int[]序列){
int[]nextArray;
for(int i=0;i=序列[i+1]){
返回false;
}
}
返回true;
}
私有静态int[]MinusOneElement(int[]序列,int elementToRemove){
int[]newArray=newint[sequence.length-1];
int指数=0;
for(int i=0;i
这是可以通过从原始程序中删除内容来修复的错误之一:

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; 
}
布尔几乎递增序列(int[]序列){
int max=Integer.MIN_值,计数=0;
布尔标志=真;
对于(int j=0;j=序列[j]){
计数++;
}
max=序列[j];
如果(计数>1){
flag=false;
打破
}
}        
返回标志;
}
原始版本的主要问题是,相同的故障位置被发现了两次。只需删除一个检查,同时添加一个循环迭代即可解决此问题。

对于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;  

 }
fun几乎递增序列(序列:可变列表):布尔值
{
var removeCount=0;
var i=0;
而(i=下一个){
removeCount++;
var removeCurrent:Boolean=i==0 | |序列[i-1]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;  

 }