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. String StringBuffer stringbuilder 区别

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 字符串类 ,长度不可变. 字符串缓存器类, 长度可变, 线程安全, 字符串构造器类,长度 ...

  2. 冒泡排序(初级版)之C++实现

    冒泡排序(初级版)之C++实现 一.源代码:BubbleSortLow.cpp /*冒泡排序思想: 从第一个元素开始,对数组中两两相邻的元素比较,将值较小的元素放在前面,值较大的元素放在后面: 一轮比 ...

  3. PHP中CGI,FastCGI,PHP-CGI与PHP-FPM对比

    CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一 ...

  4. java的多线程之四(线程的操作)

    本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17560467,转载请注明. 线程中断    线程中断涉及到三个方法,如下 ...

  5. python 加密方式(MD5&sha&hashlib)

    1.MD5加密 import md5 m = md5.new() #或者m = md5.md5() m.update('123456') m.hexdigest() #或者md5.md5('12345 ...

  6. python开发_tkinter_菜单选项中英文切换_菜单选项不可用操作_博主推荐

    我使用的python版本为:3.3.2 如果你对python中tkinter模块的菜单操作不是很了解,你可以看看: python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推 ...

  7. Android之基于HTTP协议的通信详解

    Android系统中本身是有下载机制的,比如浏览器使用的DownloadManager.可遗憾的是,DownloadManager只提供给浏览器使用,一般的应用程序没法调用它. 另外,如果下载调用频繁 ...

  8. ZeptoLab Code Rush 2015 C. Om Nom and Candies 暴力

    C. Om Nom and Candies Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526 ...

  9. Git_分支管理

    分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你也没啥影响.不过,在某个时间点,两个平行宇宙合并 ...

  10. sublime插件汇总

    JsFormat javascript格式化 有时从网上扒了人家的js代码来学习学习,打开发现被压缩了,这时就能够用JsFormat插件格式化js代码,恢复未压缩时候的排版,挺给力的.按快捷键Ctrl ...