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.

function isHopperable(data) {
function helper(current, data) {
let max = ;
if (data[current] === ) {
return current;
} // We keep checking what is the max reach for us
// for the given index value
//[4,2,0,0,2,0]: for example index = 0;
// max would be
// case1: choose 1 step: 0 + 1 + 2 = 3
// case2: choose 2 step: 0 + 2 + 0 = 2
// case3: choose 3 step: 0 + 3 + 0 = 3
// case4: choose 4 step: 0 + 4 + 2 = 6
// WE will choose case 4, since 6 is max reach we can get for (let i = data[current]; i > ; i--) {
console.log(current, i, data[i+current])
/**
0 4 2
0 3 0
0 2 0
0 1 2
*/
max = Math.max(current + i + data[i + current], max);
} return max;
} let current = ;
while (true) {
if (current >= data.length) {
return true;
} if (data[current] === ) {
return false;
} current = helper(current, data);
}
} const data = [, , , , , ];
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. 通过Nuget添加Mvvmlight框架发生错误

    IDE:Visual Studio 2013 场景:通过Nuget添加Mvvmlight框架 具体错误: 解决办法:删除Nuget,然后添加新版本的Nuget Package Manager 具体操作 ...

  2. Linux常用命令&定位生产报错日志

    1. cd / 到根目录下 2. cd .. 返回上层目录 3.ls 显示当前目录有哪些文件 4. pwd 显示当前目录 5. ps -ef|grep tomcat7 查看当前运行进程 6. kill ...

  3. mysql另类分页方法

    mysql> limit ,;select found_rows(); +----+-----------------+---------+--------+ | id | rankName | ...

  4. MySQL Innodb 存储引擎学习篇

    master thread的县城优先级别最高.其内部由几个循环(loop)组成:主循环(loop).后台循环(background loop).刷新循环(flush loop).暂停循环(suspen ...

  5. JDK篇

    卸载系统自带的jdk 使用以下命令查看是否已经安装了jdk  rpm -qa|grep java  rpm -qa|grep jdk 如果已经安装了可能会得到下面的结果: java-1.4.2-gcj ...

  6. oracle sql语句怎么查询所有存储过程中是否包含某个注释?

    select text from all_source where type='PROCEDDURE' and name='过程名'and instr(text,'注释内容')>0

  7. max_binlog_cache_size

    http://blog.mimvp.com/2017/07/mysql-yi-ge-can-shu-yin-qi-de-dang-ji-xue-an/ max_binlog_cache_size 表示 ...

  8. make and make bzImage

    2.6内核 make = make bzImage + make modules 无非是改下Makefile而已 2.4 内核 01.make menuconfig 02.make dep 03.ma ...

  9. Oracle数据库--解决单张表中数据量巨大(大数据、数据量上百万级别,后查询,更新数据等耗时剧增)

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/QQ578473688/article/details/54561397 思路1:采用备份表 备份表中 ...

  10. JS 中div内容的显示和隐藏

    1. document.getElementById("dialog-auclot-status").style.display="none";//页面加载时隐 ...