By given an array of number, each number indicate the number of step you can move to next index:

For example index = 0, value is 4, means at most you can reach index = 4, value is 2.  You can also choose take only 1 step, reach index = 1, value is 2. If in the end you can jump out of the array (values sum up larger than the length of array), then return true, if not able, then return false. If you get stuch with index which values is zero, then basiclly means you cannot reach outside of the array.

  1. function isHopperable(data) {
  2. function helper(current, data) {
  3. let max = ;
  4. if (data[current] === ) {
  5. return current;
  6. }
  7.  
  8. // We keep checking what is the max reach for us
  9. // for the given index value
  10. //[4,2,0,0,2,0]: for example index = 0;
  11. // max would be
  12. // case1: choose 1 step: 0 + 1 + 2 = 3
  13. // case2: choose 2 step: 0 + 2 + 0 = 2
  14. // case3: choose 3 step: 0 + 3 + 0 = 3
  15. // case4: choose 4 step: 0 + 4 + 2 = 6
  16. // WE will choose case 4, since 6 is max reach we can get
  17.  
  18. for (let i = data[current]; i > ; i--) {
  19. console.log(current, i, data[i+current])
  20. /**
  21. 0 4 2
  22. 0 3 0
  23. 0 2 0
  24. 0 1 2
  25. */
  26. max = Math.max(current + i + data[i + current], max);
  27. }
  28.  
  29. return max;
  30. }
  31.  
  32. let current = ;
  33. while (true) {
  34. if (current >= data.length) {
  35. return true;
  36. }
  37.  
  38. if (data[current] === ) {
  39. return false;
  40. }
  41.  
  42. current = helper(current, data);
  43. }
  44. }
  45.  
  46. const data = [, , , , , ];
  47. console.log(isHopperable(data));// true

[Algorithm] Tower Hopper Problem的更多相关文章

  1. [Algorithm] Array production problem

    Given an array of integers, return a new array such that each element at index i of the new array is ...

  2. [Algorithm] Max Chars Problem

    // --- Directions // Given a string, return the character that is most // commonly used in the strin ...

  3. Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  4. cvpr2015papers

    @http://www-cs-faculty.stanford.edu/people/karpathy/cvpr2015papers/ CVPR 2015 papers (in nicer forma ...

  5. Design and Analysis of Algorithms_Divide-and-Conquer

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  6. Dynamic Programming

    We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...

  7. <<Numerical Analysis>>笔记

    2ed,  by Timothy Sauer DEFINITION 1.3A solution is correct within p decimal places if the error is l ...

  8. 【转】 svn 错误 以及 中文翻译

    直接Ctrl+F 搜索你要找的错 # # Simplified Chinese translation for subversion package # This file is distribute ...

  9. (转)8 Tactics to Combat Imbalanced Classes in Your Machine Learning Dataset

    8 Tactics to Combat Imbalanced Classes in Your Machine Learning Dataset by Jason Brownlee on August ...

随机推荐

  1. Cipolla算法学习小记

    转自:http://blog.csdn.net/doyouseeman/article/details/52033204 简介 Cipolla算法是解决二次剩余强有力的工具,一个脑洞大开的算法. 认真 ...

  2. BZOJ 3956: Count 主席树 可持久化线段树 单调栈

    https://www.lydsy.com/JudgeOnline/problem.php?id=3956 从描述可以得到性质: 每个好点对 ( 除了差值为1的好点对 ) 中间的数 ( i , j ) ...

  3. [BZOJ3928/4048]Outer space invaders

    [BZOJ3928/4048]Outer space invaders 题目大意: 有\(n(n\le300)\)个物品,第\(i\)个物品会在\(a_i\sim b_i\)时刻出现,且离你的距离为\ ...

  4. JavaScript数组中的22个常用方法

    数组总共有22种方法,本文将其分为对象继承方法.数组转换方法.栈和队列方法.数组排序方法.数组拼接方法.创建子数组方法.数组删改方法.数组位置方法.数组归并方法和数组迭代方法共10类来进行详细介绍. ...

  5. [转]Android使用Application总结

        目录(?)[+]   Application 配置全局Context 第一步.写一个全局的单例模式的MyApplication继承自Application 覆盖onCreate ,在这个方法里 ...

  6. Easy WordPress Updates: Store FTP Info in wp-config.php

    Saw an interesting blog post on Twitter today about storing WordPress FTP information in wp-config.p ...

  7. qt 4.8.5 vxworks 6.8 demo

    2692407267@qq.com 环境vxworks 6.8.3 +  GNU Patch.Qt-commercial-4.8.5 0 先安装vxworks 6.8.安装mingw 1 先编wind ...

  8. 2008 SCI 影响因子(Impact Factor)

    2008 SCI 影响因子(Impact Factor) Excel download 期刊名缩写 影响因子 ISSN号 CA-CANCER J CLIN 74.575 0007-9235 NEW E ...

  9. MVC文件上传08-使用客户端jQuery-File-Upload插件和服务端Backload组件让每个用户有专属文件夹

    当需要为每个用户建立一个专属上传文件夹的时候,可以在提交文件的视图中添加一个隐藏域,并设置name="objectContext". 相关兄弟篇: MVC文件上传01-使用jque ...

  10. Spring Boot Jar包转War包 部署到Tomcat下

    原文:https://my.oschina.net/sdlvzg/blog/1562998 我们都知道springBoot中已经内置了tomcat,是不需要我们额外的配置tomcat服务器的,但是有时 ...