Q:解决Java中几乎递增的序列(Codefight)

kg7wmglp  于 2023-02-11  发布在  Java
关注(0)|答案(8)|浏览(123)

我没能通过最后的隐藏测试。你能告诉我我错过了什么吗?先谢了。
以下是声明:给定一个整数序列作为数组,确定是否可以通过从数组中删除不超过一个元素来获得严格递增的序列。

boolean almostIncreasingSequence(int[] sequence) 
{
    boolean increase = true;
    List<Integer> list = new ArrayList<>();
    for (int a :sequence ) 
    {
        list.add(a); 
    }
    System.out.println(list);
    if(list.size()==1)
    {
        return false;
    }
    for (int i = 0;i < list.size()-1 ;i++ ) 
    {
        if (list.get(1)<=list.get(0)) 
        {
            list.remove(0);
            break;    
        }
        if(list.get(i+1)<=list.get(i)) 
        {
            if (list.get(i+1)>list.get(i-1)) 
            {
                list.remove(i); 
            }
            else
            {
                list.remove(i+1);
            }
            break;
        } 
    }

    for (int i =0;i<list.size()-1 ;i++ ) 
    {
        if (list.get(i+1)<list.get(i) || list.get(i+1)==list.get(i) ) 
        {
            increase = false;
        }    
    }
    return increase;
}
kxkpmulp

kxkpmulp1#

这是我提出的线性解决方案,它涉及到静音数组,这样你就不必再循环数组了。

boolean almostIncreasingSequence(int[] sequence) {
    int removed = 0;

    for (int i = 0; i < sequence.length - 2 && removed <= 2; i ++) {
        int a = sequence[i];
        int b = sequence[i+1];
        int c = sequence[i+2];

        if (a >= b) {
            removed++;
            sequence[i] = b -1;
        } 

        if (b >= c){
            removed++;

            if (a == c) {
                sequence[i+2] = b +1;
            } else {
                sequence[i+1] = a;
            }
        }
    }

    return removed <= 1;
}
rbpvctlc

rbpvctlc2#

这是我复杂度为O(n)的解'

boolean almostIncreasingSequence(int[] sequence) {
    int flag = 0;
    int i = 0;
    while(i<sequence.length-1){
        if(sequence[i] < sequence[i+1]){
            i = i+1;
            continue;
        } else {
            flag = flag + 1;
            if(i>0 && i+2 < sequence.length && sequence[i+1] <= sequence[i-1] && sequence[i+2] <= sequence[i]){
                flag = flag + 1;
            } else {
                i = i+1;
            }
            if(flag > 1){
                return false;
            }
        }
    }
    return true;
}

'

enxuqcxy

enxuqcxy3#

这对我很有效

boolean almostIncreasingSequence(int[] sequence) {
    int failed = 0;
    boolean one_chance;    
        for(int i = 0; i < sequence.length - 1; i++){
            int curr=i,next=i+1;
            if(sequence[curr] >= sequence[next]) {                
                failed++;                                    
                    if( curr > 0 && next < sequence.length - 1 ){
                        // Problem is not on head neither tail
                        // So check if removing one of 2 problematic numbers solves the issue
                        one_chance = false;
                        if( sequence[curr - 1] < sequence[next] )
                            one_chance = true ;                         
                        if ( sequence[curr] < sequence[next+1] )
                            one_chance = true ;
                        if( one_chance == false ) return false;
                    }

            }
            if( failed > 1 ) return false;            
        }
    return true;        
}
l7wslrjt

l7wslrjt4#

剧透警告!

我也没能通过最后一次隐藏测试。所以我花了12,300个硬币中的10,000个(哎哟!)来解锁它们。
结果是,最后一个测试(#34)预期结果为true,并且传递了一个长度为100,000的int数组,顺序从1到100000!(如此之大,以至于我只能在代码中这样做:

System.out.printf("length: %d%nlastValue:%d%n",
        sequence.length, sequence[sequence.length - 1]);

我不知道为什么我的代码没有通过,但我至少解开了那个隐藏的测试,所以现在你可以知道它是什么,而不必花硬币来解开它自己。
然后,我变得懒惰起来,在方法的顶部添加了这一行,以使其通过:

if (sequence.length == 100000
        && sequence[sequence.length - 1] == 100000) {
    return true;
}
5m1hhzi4

5m1hhzi45#

下面是一个使用递归检查数组剩余部分的解决方案。
问题是当代码遇到一个不属于它的数字时,它不能确定两个数字中的哪一个是违规者,所以我只是从检测到问题的地方开始检查数组,跳过“坏”的数字。如果在跳过一个数字时再次失败,游戏就结束了。
这是在JavaScript中,但它可以很容易地被翻译。

function almostIncreasingSequence(sequence) {
    if(!sequence || sequence.length < 3) return true;

    return checkSorted(sequence);
}

function checkSorted(arr, start = 0, skip) {

    let last = arr[start === skip ? skip + 1 : start];

    for(let i = start + 1; i < arr.length; i++) {

        if(skip === i) continue;
        let current = arr[i];
        let lastIndex = skip === i - 1 ? i - 2 : i - 1;
        let last = arr[lastIndex];

        if(current <= last) {
            if(skip !== undefined) return false;
            return checkSorted(arr, i - 1, i) || checkSorted(arr, i - 1, i - 1);
        }
    }
    return true;
}
qeeaahzv

qeeaahzv6#

def almostIncreasingSequence(sequence):
initial_length = len(sequence)-1
length = len(sequence)-1
count = 0
i = 0
while i < length:
    if sequence[i] >= sequence[i+1]:
        if i == 0:
            sequence.pop(0)
            count +=1
            length = len(sequence)-1
            i =0
        elif sequence[i] == sequence[i+1]:
            sequence.pop(i+1)
            length = len(sequence)-1
            i -= 1
            count += 1
        elif sequence[i] > sequence[i+1]:
            if count ==0 and i + 1 == length:
                return True
            else:
                if max(sequence) == sequence[i] and count == 0:
                    sequence.pop(i)
                    length = len(sequence)-1
                    i -= 1
                    count += 1                        
                else:    
                    sequence.pop(i+1)
                    length = len(sequence)-1
                    i -= 1
                    count +=1
    else:
        i += 1
length = len(sequence)-1
if count == 1:
    if initial_length - length == 1:
        return True
    else:
        return False
elif count > 1:
    if initial_length - length > 1:
        return False
else:
    return True
u59ebvdq

u59ebvdq7#

boolean almostIncreasingSequence(int[] sequence) {
        int count = 0;
        int size = sequence.length;
        if(size==1)
        return true;
        for(int i=0;i<size-1 && count<=1;i++){
           if(sequence[i]>=sequence[i+1]) {
               count++;
               if(i>0 && (i+2)<size && sequence[i-1]>=sequence[i+1] && sequence[i]>=sequence[i+2]) {
                   count++;
               }
           }
        }
        return (count<=1);
    
}
ubof19bj

ubof19bj8#

boolean solution(int[] sequence) {
    boolean increasing = true;
    boolean isany = false;
    
    for(int i=0;i<sequence.length;i++){
        ArrayList<Integer> sequenceArrayList = new ArrayList();
        for(int a : sequence){
            sequenceArrayList.add(a);
        }

        sequenceArrayList.remove(i);
        for(int j=0;j<sequence.length-2;j++){
            if(sequenceArrayList.get(j)>=sequenceArrayList.get(j+1)){
                increasing = false;
                break;
            }else{
                increasing = true;
            }
        }
        if(increasing){
            isany = true;
        }
    }
    return isany;
}

相关问题