当数组停止增加并开始减少时返回数组的索引

Returning index of an Array when it stops increasing and begins decreasing

当 运行 使用此代码进行两次测试时出现问题,测试 1 returns 整个数组,测试 2 returns 适当的索引。 (数组中停止减少或增加并开始反向的点)如果只有一个序列它应该 return -1

测试用例是 输入:[-4, -2, 9, 10] 输出:-1 输入:[5, 4, 3, 2, 10, 11] 输出:3

for (i = 0; i < arr.length; i++) {
    while (arr[i] < arr[i + 1]) {
      i++;
      if (arr[i] < arr[i - 1]) {
        return -1
      }
      else if (arr[i] > arr[i + 1]) {
        return i
      }
    } while (arr[i] > arr[i + 1]) {
      i++;
      if (arr[i] > arr[i - 1]) {
        return -1
      } else if (arr[i] < arr[i + 1]) {
        return i
      }
    }
  }
  return arr;

}

在下面的解决方案中,如果数组的内容是 ever-increasing 或 ever-decreasing,isHomogeneous() 方法 returns 为真。 isIncreasing() 方法 returns 如果数组内容不断减少则为真。如果数组内容为 ever-increasing,isDecreasing() 方法 returns 为真。 getAscendingIndex() 方法 returns 数组内容开始减少的索引。 getDescendingIndex() 方法 returns 数组内容开始减少的索引。 application() 方法包含执行其他方法的应用程序代码。如果数组的内容不均匀(不断增加或减少),则使用getDescendingIndex()方法获取以值开始减少的数组中值开始增加的第一个索引值。

/* Returns true if the array is descending to the elements (homogeneous). */
function isIncreasing(array) {
  for(let i = 0 ; i < array.length ; ++i)
    if(array[i] < array[i + 1])
      return false;
  return true;
}

/* Returns true if the array is incremental (homogeneous) to the elements. */
function isDecreasing(array) {
  for(let i = 0 ; i < array.length ; ++i)
    if(array[i] > array[i + 1])
      return false;
  return true;
}

/* Returns true if the array content is ever-increasing or ever-decreasing. */
function isHomogeneous(array) {
  return isIncreasing(array) || isDecreasing(array);
}

/* return the descending index in the array starting with increasing. */
function getAscendingIndex(array) {
  for(let i = 0 ; i < array.length ; ++i)
    if(array[i] > array[i + 1])
      return i;
  return -1;
}

/* return increasing index in array starting with decreasing. */
function getDescendingIndex(array) {
  for(let i = 0 ; i < array.length ; ++i)
    if(array[i] < array[i + 1])
      return i;
  return -1;
}

/* demo */
function application() {
  let firstArray = [-4, -2, 9, 10];
  let secondArray = [5, 4, 3, 2, 10, 11];
  
  console.log(`Increasing: ${(isIncreasing(firstArray) == true) ? "true" : "false"}`);
  console.log(`Decreasing: ${(isDecreasing(firstArray) == true) ? "true" : "false"}`);
  console.log(`First Array Index: ${getAscendingIndex(firstArray)}`);
  
 if(!isHomogeneous(secondArray) && getAscendingIndex(secondArray) != -1) {
    console.log(`Second Array Index: ${getDescendingIndex(secondArray)}`);
  }
}

application();

也许这种方法会有用:

找到最大和最小元素的索引。 检查此索引是数组的开头还是结尾,如果是则返回 -1。 最后 return 找到的最大索引。

  const first = [-4, -2, 9, 10];
  const second = [5, 4, 3, 2, 10, 11];
  
  const getMyIndex = (arr) => {
    const maxIndex = arr.indexOf(Math.max(...arr));
    const minIndex = arr.indexOf(Math.min(...arr));
    const getResult = (index) => 
      (index === 0) || (index === arr.length - 1) ? -1 : index;
    
    return Math.max(getResult(maxIndex), getResult(minIndex));   
  };
  
  console.log('arr:', first, 'result:', getMyIndex(first));
  console.log('arr:', second, 'result:', getMyIndex(second));
.as-console-wrapper{min-height: 100%!important; top: 0}