如何有效地找到元素跳出数组所需的跳转次数?

发布于 2025-01-21 03:35:15 字数 613 浏览 3 评论 0原文

我正在尝试编写一种有效的算法,该算法将发现典当退出数组所需的跳跃数量。假设我们给我们一个数组,对于数组中的每个元素,如果数组[k] = n,那么数组[k]等于array [k + n];

例如,我们有此数组[2,3,-1,1,3]。

最初,典当在数组[0],在第一次跳转时,它从阵列[0]移动到数组[2],因为0 + array [0]等于2。在第二个跳转时,典当从[2]移动[1]是因为2 + a [2] = 1;在第三跳时,典当从[1]移至[4],因为1 + a [1] = 4; 在第四跳时,典当跳出了阵列。该测试案例返回4。

如果典当无法跳出数组,我们将返回-1;

以下是我为此问题编写的算法,它适用于一些测试用例,但未能通过大型测试用例。如何使该算法更有效。

function jumpOut(A) { 
  let start = 0;
  let end = A.length - 1
  let pawn = 0;
  
  while (start < end){
    pawn= start + A[start]  
    start++;
  }
  
  if(pawn === end){
    return pawn
  }
  
  return -1;
} 

I am trying to write an efficient algorithm that will find the number of jumps it will take for a pawn to exit an array. Lets say we are given an array, for each element in the array if array[k] = n then array[k] is equal to array[k + n];

For example, we have this array [2,3,-1,1,3].

Initially the pawn is at array[0], on first jump it moves from array[0] to array[2] because 0 + array[0] is equal to 2. on the second jump, the pawn moves from A[2] to A[1] because 2 + A[2] = 1; on the third jump, the pawn moves from A[1] to A[4] because 1 + A[1] = 4;
on the fourth jump, the pawn jumps out of the array. It returns 4 for this test case.

If the pawn cant jump out of the array we return -1;

Below is the algorithm I wrote for this problem and it works for a few test cases but fails large test cases. How can I make this algorithm more efficient.

function jumpOut(A) { 
  let start = 0;
  let end = A.length - 1
  let pawn = 0;
  
  while (start < end){
    pawn= start + A[start]  
    start++;
  }
  
  if(pawn === end){
    return pawn
  }
  
  return -1;
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2025-01-28 03:35:15

如果典当始终在给定数组中指定的量始终跳跃,则可以在线性时间内找到离开数组所需的移动次数。

在每次跳跃之前,您可以检查先前访问的当前位置。如果是,那意味着存在典当无法退出的循环-1。否则,继续跳跃并返回最终计数。

function getNumRequiredMoves(jumps) {
  // Remember the positions that have been visited
  let isVisited = Array(jumps.length).fill(false);
  // Position of the pawn
  let position = 0;
  // Final result
  let numMoves = 0;

  while (position >= 0 && position < jumps.length) {
    if (isVisited[position]) {
      // This position has been visited before
      return -1;
    }
    // Mark this position as visited
    isVisited[position] = true;
    // Jump
    position += jumps[position];
    // Increment the jump counter
    ++numMoves;
  }

  return numMoves;
}

现在在给定的时间,如果pawn可以在[1,跳跃[position]]之间跳跃,则可以使用动态编程有效地找到所需的动作数量。

Finding the number of moves required to get out of the array can be done in a linear time if the pawn always jumps EXACTLY the amount specified in the given array.

Before each jump, you can check if the current position of the pawn has been previously visited. If yes, that means there exists a loop that the pawn cannot get out of so return -1. Otherwise keep jumping and return the final count.

function getNumRequiredMoves(jumps) {
  // Remember the positions that have been visited
  let isVisited = Array(jumps.length).fill(false);
  // Position of the pawn
  let position = 0;
  // Final result
  let numMoves = 0;

  while (position >= 0 && position < jumps.length) {
    if (isVisited[position]) {
      // This position has been visited before
      return -1;
    }
    // Mark this position as visited
    isVisited[position] = true;
    // Jump
    position += jumps[position];
    // Increment the jump counter
    ++numMoves;
  }

  return numMoves;
}

Now at a given time if the pawn can jump anywhere between [1, jumps[position]], you can use dynamic programming to efficiently find the number of required moves.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文