为了账号安全,请及时绑定邮箱和手机立即绑定

【算法题】给定int数组,移除不超过一个元素后,判断是否存在自增序列

【算法题】给定int数组,移除不超过一个元素后,判断是否存在自增序列

慕少森 2019-03-01 11:14:52
没什么思路啊,题目如下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 bealmostIncreasingSequence(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 bealmostIncreasingSequence(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 回答

?
函数式编程

TA贡献1807条经验 获得超9个赞

提供一个思路

作出逐差数组: 如 a=[1,3,2,1],逐差后得 [2,-1,-1]

所谓删除一个元素,即在在逐差数组中去头或去尾,或把相邻两个相加合并成一个元素。

因此,若逐差数组中有多于一个负数,则不行; 若无负数,则可以; 否则对惟一的负数作以上操作,若其能被删掉或被合并成正数,则可以

这样一来,时间复杂度可以降到 O(n)

查看完整回答
反对 回复 2019-03-01
?
白板的微信

TA贡献1883条经验 获得超3个赞

可以在 O(n) 时间做到:

  1. 对每个相邻的 [a, b],判断是否 a >= b。这样的数对破坏严格递增性。如果这样的数对超过一个,返回false。如果一个也没有,返回true。

  2. 如果1中只有一对 [a0, b0],判断 "移除a0b0后是否还是递增" 并返回

查看完整回答
反对 回复 2019-03-01
?
GCT1015

TA贡献1827条经验 获得超4个赞

结果是对的,但是超过规定的时间了,有更好的方法吗?

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;
}
查看完整回答
反对 回复 2019-03-01
?
慕姐8265434

TA贡献1813条经验 获得超2个赞

这个是不是统计逆序数的个数?

查看完整回答
反对 回复 2019-03-01
  • 4 回答
  • 0 关注
  • 642 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号