zoukankan      html  css  js  c++  java
  • [Leetcode 56] 55 Jump Game

    Problem:

    Given an array of non-negative integers, you are initially positioned at the first index of the array.

    Each element in the array represents your maximum jump length at that position.

    Determine if you are able to reach the last index.

    For example:
    A = [2,3,1,1,4], return true.

    A = [3,2,1,0,4], return false.

    Analysis:

    At first glance, it seems like a backtracking problem. But won't pass the large test cases.

    Then try to use simulation method, though can go further than backtracking, still can pass all test cases.

    The developed a one-scan algorithm. Observe that we can compute what's the furthest position we can go at each position. If the current position is less than the max position we can go, then it means that we can reach this place. And if from this place go to the furthest place we can go is greater than max. Then it means that the range is extended. We can continue this process until 1. reach the end of the array; 2. find the max covers the final place. Since we only need to scan the array only once, the total time complexity is O(n)

    Code:

    Backtracking Solution: TLE

     1 class Solution {
     2 public:
     3     int len;
     4     int* array;
     5     
     6     bool canJump(int A[], int n) {
     7         // Start typing your C/C++ solution below
     8         // DO NOT write int main() function
     9         len = n;
    10         array = A;
    11         bool res = false;
    12         
    13         bc(0, res);
    14         return res;
    15     }
    16     
    17     void bc(int pos, bool &res) {
    18         if (pos+1 > len)
    19             return ;
    20         
    21         if (pos+1 == len) {
    22             res = true;
    23             return ;
    24         }
    25         
    26         for (int i=0; i<=array[pos]; i++)
    27            bc(pos+i, res);
    28         
    29         return ;
    30     }
    31 };
    View Code

    Iterative Solution: TLE

     1 class Solution {
     2 public:
     3 
     4     bool canJump(int A[], int n) {
     5         // Start typing your C/C++ solution below
     6         // DO NOT write int main() function
     7         if (n==0) return false;
     8         
     9         bool tag[n];
    10         
    11         tag[0] = true;
    12         for (int i=1; i<n; i++) 
    13             tag[i] = false;
    14             
    15         for (int i=0; i<n; i++) {
    16             if (tag[i] == true) {
    17                 for (int j=A[i]; j>0; j--) {
    18                     if (i+j < n) 
    19                         tag[i+j] = true;
    20                     
    21                     if (tag[n-1] == true)
    22                         return true;
    23                 }
    24             }
    25         }
    26         
    27         return tag[n-1];
    28     }
    29 };
    View Code

     O(n) solution:

     1 class Solution {
     2  public:
     3  
     4      bool canJump(int A[], int n) {
     5          // Start typing your C/C++ solution below
     6          // DO NOT write int main() function
     7          if (n==0) return false;
     8          
     9          int max = 1+A[0];
    10          for (int i=2; i<+n; i++) {
    11              if (i<=max && (i+A[i-1]) > max) {
    12                  max = i+A[i-1];
    13              }
    14              
    15              if (max >= n)
    16                 break;
    17          }
    18          
    19          return (max >= n);
    20      }
    21  };
    View Code
  • 相关阅读:
    Leetcode题目:Remove Duplicates from Sorted List
    Leetcode题目:Lowest Common Ancestor of a Binary Search Tree
    Leetcode题目:Ugly Number
    Leetcode题目:Remove Linked List Elements
    Leetcode题目:Count and Say
    6-3 事务
    6-1 视图
    5-2 pymysql模块
    5-1 图形工具Navicat
    4-3 多表查询
  • 原文地址:https://www.cnblogs.com/freeneng/p/3099613.html
Copyright © 2011-2022 走看看