[英]almost increasing sequence java
I am trying to write code that determines if is it possible to attain an array of strictly increasing integers by removing only one element from that array.我正在尝试编写代码来确定是否可以通过仅从该数组中删除一个元素来获得严格递增的整数数组。
I have my code working for 16 out of 17 cases but cannot think of a way to neatly rewrite my code so that it accounts for the case of a number being bigger than the one before it as well as smaller than the one after it the way I've written this for loop.我的代码适用于 17 种情况中的 16 种,但无法想出一种方法来巧妙地重写我的代码,以便解决一个数字大于它之前的数字以及小于它之后的数字的情况我已经写了这个 for 循环。 Here is my code.
这是我的代码。 The case that this doesn't work for is the array: [1, 2, 3, 4, 3, 6], since it doesn't consider the last 3 in the array as an offender the way my for loop is currently constructed.
这不起作用的情况是数组:[1, 2, 3, 4, 3, 6],因为它不认为数组中的最后 3 个作为我的 for 循环当前构造方式的违规者.
boolean almostIncreasingSequence(int[] sequence) {
int offenderPosition = 0;
int[] arrCopy = Arrays.copyOf(sequence, sequence.length);
boolean ordered = true;
//trying to neatly rewrite this for loop
for(int i= 0; i < sequence.length; i++){
if(i<sequence.length-1){
for(int j = i+1; j < sequence.length; j++) {
if(!(sequence[i] < sequence[j])){
ordered = false;
offenderPosition = i;
}
}
}
if(i == sequence.length-1){
if(!(sequence[i] > sequence[i-1])){
ordered = false;
offenderPosition = i;
}
}
}
if(ordered == false) {
//remove offender
int currentSize = arrCopy.length;
for(int i = offenderPosition+1;i< currentSize; i++) {
arrCopy[i-1] = arrCopy[i];
}
currentSize--;
//reassign array
arrCopy = Arrays.copyOf(arrCopy, currentSize);
boolean lastChance = true;
for(int i = 0; i < currentSize-1; i++){
for(int j = i+1; j < currentSize; j++) {
if(!(arrCopy[i] < arrCopy[j])){
lastChance = false;
}
}
}
return lastChance;
}
else{
return true;
}
} }
I think that this might work:我认为这可能有效:
boolean almostIncreasingSequence(int[] a) {
int count1 = 0 , count2 = 0;
for(int i = 0 ; i < a.length-1 ; i++){
if(a[i] >= a[i+1]) count1++;
}
for(int i = 0 ; i < a.length-2 ; i++){
if(a[i] >= a[i+2]) count2++;
}
return (count1 <=1) && (count2 <= 1);
}
The first loop only checks the number that are near each other.第一个循环只检查彼此靠近的数字。 If the first index is greater than the second one we will add 1 to count1.
如果第一个索引大于第二个索引,我们将向 count1 加 1。 When adding 1 to the count1 it means that the first index was greater than the second one which the method should return false;
当将 1 添加到 count1 时,这意味着第一个索引大于第二个索引,该方法应该返回 false; The second for-loop will also check ex.
第二个 for 循环也将检查 ex。 If the first index is greater than the third index.
如果第一个索引大于第三个索引。 1, 2 ,1,2 for example it will add 1 to the count2 Efter each loop get executed the method will return the boolean that the if statement returns.
例如 1, 2 ,1,2 它将在 count2 上加 1 在每个循环执行后,该方法将返回 if 语句返回的布尔值。
You could break the code into a few methods:您可以将代码分解为几种方法:
// The first method just checks if the input array is sorted
public static boolean isAscending(int[] arr) {
boolean sorted = true;
for (int i = 0; i < arr.length - 1; ++i) {
if (arr[i] >= arr[i + 1]) {
sorted = false;
break;
}
}
return sorted;
}
// The second method is the important one.
public static boolean isAlmostAscending(int[] array) {
int[] tmpArray = new int[array.length - 1];
// loop through all possible combinations
for(int i = 0; i < array.length; ++i) {
copyArray(array, tmpArray, i);
if(isAscending(tmpArray)) {
// if the array is sorted after skipping element i, we are done
return true;
}
}
return false;
}
// helper method to copy array and skip element at skip
private static void copyArray(int[] srcArray, int[] destArray, int skip) {
for(int i = 0, j = 0; i < destArray.length; ++i, ++j) {
if(i == skip) {
++j;
}
destArray[i] = srcArray[j];
}
}
You could lump all three methods into one as follows:您可以将所有三种方法合二为一,如下所示:
public static boolean isAlmostAscending(int[] array) {
int[] tmpArray = new int[array.length - 1];
// loop through all possible combinations
for(int index = 0; index < array.length; ++index) {
// copyArray
for(int i = 0, j = 0; i < tmpArray.length; ++i, ++j) {
if(i == index) {
++j;
}
tmpArray[i] = array[j];
}
// check if the current array is sorted
boolean sorted = true;
for (int i = 0; i < tmpArray.length - 1; ++i) {
if (tmpArray[i] >= tmpArray[i + 1]) {
sorted = false;
break;
}
}
if(sorted) {
// if the array is sorted after skipping element i, we are done
return true;
}
}
return false;
}
Here is the solution:这是解决方案:
boolean almostIncreasingSequence(int[] sequence) {
int count = checkSeq(sequence);
if(count == -1) {
return true;
}
int index = count;
count = checkSeq(removeIndex(sequence,count-1));
if(count == -1) {
return true;
}
count = checkSeq(removeIndex(sequence,index));
if(count == -1) {
return true;
}
return false;
}
int checkSeq(int[] source){
int index = -1;
for(int i=1; i<source.length;i++){
if(source[i] <= source[i-1]){
index = i;
break;
}
}
return index;
}
int[] removeIndex(int[] source , int index){
int[] dist = new int[source.length-1];
int x = 0;
for(int i=0; i<source.length;i++){
if(i==index){
continue;
}
dist[x++] = source[i];
}
return dist;
}
boolean almostIncreasingSequence(int[] sequence) {
int count=0;
for(int i=0;i<sequence.length-1;i++){
//if a problem is found increase the counter
if(sequence[i]>=sequence[i+1]){
count++;
/*Lots of conditions, just making sure we don't go out of bounds so
that we can check if there are any potential duplicates/order issues if
a number would be removed
*/
if(i-1>=0&&i+2<sequence.length&&sequence[i-
1]>=sequence[i+1]&&sequence[i]>=sequence[i+2]){
return false;
}
//if we found a problem twice
if(count>=2){
return false;
}
}
} return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.