几乎IncreasingSequence - Javascript

Posted

技术标签:

【中文标题】几乎IncreasingSequence - Javascript【英文标题】:almostIncreasingSequence - Javascript 【发布时间】:2019-05-06 03:21:33 【问题描述】:

大家好,我在学习算法和数据结构时同时使用 Codefights,但我似乎无法解决这个问题。

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

我的代码由于性能问题而失败,我大致了解为什么要考虑复制原始数组并循环遍历两者。但是我想不出更优化的方法。

function almostIncreasingSequence(sequence) 
    let result = false;
    for(let i = 0; i < sequence.length; i++) 
        let newSequence = [...sequence]
        newSequence.splice(i,1)
        result = isArraySequential(newSequence)
        if (result) 
            return result;
        
    
        return result;


function isArraySequential(array) 
    let isSequential = true;
    for(let i = 0; i < array.length; i++) 
        if(i == array.length - 1) return isSequential
         if (array[i + 1] < array[i] || array[i + 1] == array[i]) 
            return !isSequential;
        
    

【问题讨论】:

【参考方案1】:

你不需要经常检查整个数组,也不需要使用多个循环。

问题可以分解为更小的问题。对于列表中的每个元素...

当前元素是否大于上一个元素(增加)? 是的... 好!我们不需要做任何事情。 不... 这种情况已经发生了吗?如果是这样,几乎不会增加。 如果我们删除前一个项目,周围的项目是否固定? 没有?如果我们改为删除 current 项会怎样? 还是没有?那么这意味着我们不能一举解决这个问题。 几乎没有增加。

代码如下所示:

function almostIncreasingSequence(sequence) 
  let invalidItemsCount = 0;
  
  for (let i = 1; i < sequence.length; i++) 
    if (sequence[i] <= sequence[i-1]) 
      invalidItemsCount++;
      if (invalidItemsCount > 1) return false;
      if (sequence[i] <= sequence[i-2] && sequence[i+1] <= sequence[i-1]) return false;
    
  
  
  return true;


var test1 = [0,1,2,3,4,7,6,7,8,9,10];
var test2 = [0,1,2,4,3,4,5,7,6,7,8,9,10];

console.log(almostIncreasingSequence(test1));
console.log(almostIncreasingSequence(test2));

评论版本:

function almostIncreasingSequence(sequence) 
  //Keep track of how many replacements we've had to make
  let invalidItemsCount = 0;
  
  //Start our loop at 1 so that [i-1] doesn't refer to index "-1"
  for (let i = 1; i < sequence.length; i++) 
  
    //If this item is not increasing, we'll need to address it
    if (sequence[i] <= sequence[i-1]) 
    
      //Increment our invalidItemsCount
      invalidItemsCount++;               
      
      //If this is our second invalidItem, then it's not almost increasing.
      if (invalidItemsCount > 1) return false;  
      
      //If removing the previous element doesn't help, and removing the current item doesn't help,
      //then we can't solve this in one move. It's not almost increasing.
      if (sequence[i] <= sequence[i-2] && sequence[i+1] <= sequence[i-1]) return false;
      
    
  
  
  //We've made it through the entire loop without fail. This is almost increasing.
  return true;


var test1 = [0,1,2,3,4,7,6,7,8,9,10];
var test2 = [0,1,2,4,3,4,5,7,6,7,8,9,10];

console.log(almostIncreasingSequence(test1));
console.log(almostIncreasingSequence(test2));

【讨论】:

感谢您的帮助。我想过做类似的事情,但不确定如何设置最后一个 if 语句。我感谢 cmets,并将尝试完全理解此解决方案。【参考方案2】:

这是我解决问题的方法。我希望有人会觉得它有帮助。

function almostIncreasingSequence(sequence) 
    let flag = 0;
    for (let i = 0; i < sequence.length; i++)  
      if (sequence[i] >= sequence[i+1])
          flag++; 
          if(i !== 0 && sequence[i] >= sequence[i+2])
              if(sequence[i-1] >= sequence[i+1])
                  return false;
          
      
   
  return flag < 2;

【讨论】:

【参考方案3】:

查看我找到的这个解决方案,时间复杂度为 O(n)。希望有人觉得它有帮助

function almostIncreasingSequence()
  let bad=0,i; //initialize the number of bad counts
  for(i=1; i<sequence.length; i++)
        if(sequence[i]<=sequence[i-1])
        bad++
        
 // The descriptions say "removing no more than one element from the array."
 // if the number of bad counts is more than one, we can't obtain the desired result so return false
        if(bad>1) return false
// if the element on current index is  less than or equal the adjacent element -2 steps back
// && next element is less than or equal to the element on previous index return false 
        if(sequence[i]<=sequence[i-2] && sequence[i+1]<=sequence[i-1])return false

    
   return true

Here is the link

【讨论】:

【参考方案4】:

我在 TypeScript 中提出了这个解决方案,我放在这里是为了获得一些反馈。 它之所以有效,是因为布尔值最初是未定义的。

function almostIncreasingSequence(sequence: number[]): boolean 

let joker : boolean;
console.log(joker);
for (let index = 1; index < sequence.length; index++) 
    const element = sequence[index];
    if (sequence[index] <= sequence[index-1]) 
        if (!joker) 
            joker = true;     
         else 
            return false;
        
     

return true;

【讨论】:

【参考方案5】:
package main

import "fmt"

func main() 
    fmt.Println(almostIncreasingSequence([]int10, 1, 2, 3, 4, 5))
    fmt.Println(almostIncreasingSequence([]int1, 2, 3, 2, 3, 6))
    fmt.Println(almostIncreasingSequence([]int1, 2, 3, 4, 99, 5, 6))
    fmt.Println(almostIncreasingSequence([]int1, 2, 3, 4, 5, 6, 5))
    fmt.Println(almostIncreasingSequence([]int1, 5, 3, 4, 5, 6, 5))


func almostIncreasingSequence(sequence []int) bool 
    count := 0
    for i := 1; i <= len(sequence)-1; i++ 
        if sequence[i] <= sequence[i-1] 
            count++
            if count > 1 
                return false
            

            if i <= 1 || i == len(sequence)-1 
                continue
            

            if sequence[i] <= sequence[i-2] && sequence[i+1] <= sequence[i-1] 
                return false
            
            continue
        
    
    return true


【讨论】:

这个问题清楚地提到(和标签)javascript。你的回复是go代码,没有任何解释。

以上是关于几乎IncreasingSequence - Javascript的主要内容,如果未能解决你的问题,请参考以下文章

第十四章:垃圾回收概述

mybatis简介及实例

Java_MyBatis

jsonarray和jsonobject

OOZIE:JA009:RPC 响应超出最大数据长度

第四章:jianja2模板用法