java - 【算法题】给定int数组,移除不超过一个元素后,判断是否存在自增序列
怪我咯
怪我咯 2017-04-18 10:52:52
0
4
679

没什么思路啊,题目如下
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.

Example

For sequence = [1, 3, 2, 1], the output should be
almostIncreasingSequence(sequence) = false;

There is no one element in this array that can be removed in order to get a strictly increasing sequence.

For sequence = [1, 3, 2], the output should be
almostIncreasingSequence(sequence) = true.

You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].

Input/Output

[time limit] 4000ms (js)
[input] array.integer sequence

Guaranteed constraints:
2 ≤ sequence.length ≤ 105,
-105 ≤ sequence[i] ≤ 105.

[output] boolean

Return true if it is possible to remove one element from the array in order to get a strictly increasing sequence, otherwise return false.

有个思路:2层循环,第一循环移除元素,第二层循环判断移除这个元素后是否有自增序列。

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回覆(4)
PHPzhong

提供一個思路

作出逐差數組: 如 a=[1,3,2,1],逐差後得 [2,-1,-1]

所謂刪除一個元素,即在逐差數組中去頭或去尾,或把相鄰兩個相加合併成一個元素。

因此,若逐差數組中有多於一個負數,則不行; 若無負數,則可以; 否則對惟一的負數作以上操作,若其能被刪除或被合併成正數,則可以

這樣一來,時間複雜度可以降到 O(n)

迷茫

可以在 O(n) 時間做到:

  1. 對每個相鄰的 [a, b],判断是否 a >= b。這樣的數對破壞嚴格遞增性。如果這樣的數對超過一個,則回傳false。如果一個也沒有,回傳true。

  2. 如果1中只有一對 [a0, b0],判断 "移除a0b0後是否還是遞增" 並返回

Ty80

結果是對的,但是超過規定的時間了,有更好的方法嗎?

function almostIncreasingSequence(sequence) {

    var iscan = false;
    var is = true;
    var temp
    for(var i=0;i<sequence.length;i++){
        is = true;
        temp = sequence.slice(0,i).concat(sequence.slice(i+1));
        for(var j=0;j+1<temp.length;j++){
           
            if(temp[j] <= temp[j+1]){
                is = false;
                break;
            }
        }
        if(is){
            iscan=true;
            break;
        }

    }
    return iscan;
}

時間複雜度為O(n)的方法

boolean almostIncreasingSequence(int[] sequence) {
    if(sequence.length<=2){
        return true;
    }
    //找出逆序的数的index
    int count = 0;
    int biggerIndex = 0;
    int smallerIndex = 0;
    boolean isHave = true;
    for(int i=0;i+1<sequence.length;i++){
        //如果找到2组逆序,直接返回false
        if(count>1){
            isHave = false;
        }
        if(sequence[i]>=sequence[i+1]){
            count ++;
            biggerIndex = i;
            smallerIndex = i+1;
        }
    }
    
    //分别判断当移除2个数后剩下的数组是不是自增的
    for(int i=0;i+2<sequence.length;i++){
        int cur = i;
        int next = i+1;
        if(i==biggerIndex){
            continue;
        }
        if(i+1==biggerIndex){
            next = i+2;
        }
        if(sequence[cur]>=sequence[next]){
            isHave = false;
        }
    }
    if(isHave){
        return isHave;
    }else{
        isHave = true;
    }

    for(int i=0;i+2<sequence.length;i++){
        int cur = i;
        int next = i+1;
        if(i==smallerIndex){
            continue;
        }
        if(i+1==smallerIndex){
            next = i+2;
        }
        if(sequence[cur]>=sequence[next]){
            isHave = false;
           
        }
    }
    return isHave;
}
左手右手慢动作

這個是不是統計逆序數的個數?

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!