[Algorithm] Tower Hopper Problem
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的更多相关文章
- [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 ...
- [Algorithm] Max Chars Problem
// --- Directions // Given a string, return the character that is most // commonly used in the strin ...
- 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 ...
- cvpr2015papers
@http://www-cs-faculty.stanford.edu/people/karpathy/cvpr2015papers/ CVPR 2015 papers (in nicer forma ...
- Design and Analysis of Algorithms_Divide-and-Conquer
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- Dynamic Programming
We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...
- <<Numerical Analysis>>笔记
2ed, by Timothy Sauer DEFINITION 1.3A solution is correct within p decimal places if the error is l ...
- 【转】 svn 错误 以及 中文翻译
直接Ctrl+F 搜索你要找的错 # # Simplified Chinese translation for subversion package # This file is distribute ...
- (转)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 ...
随机推荐
- Python168的学习笔记2
关于for循环,其实质是利用被循环对象的__iter__,或者__getitem__属性接口,由可迭代对象得到迭代器.for循环就是不断调用.next(),直到最终捕获到stop. import re ...
- BZOJ 2743: [HEOI2012]采花 离线树状数组
2743: [HEOI2012]采花 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2743 Description 萧芸斓是Z国的公主, ...
- Codeforces Beta Round #97 (Div. 1) B. Rectangle and Square 暴力
B. Rectangle and Square 题目连接: http://codeforces.com/contest/135/problem/B Description Little Petya v ...
- mac下git+maven+jenkins自动打包发布
随着springboot+springcloud(dubbo)越来越多人使用,流行的微服务的概念越来越深入人心.分布式部署越来越复杂,给手动发布带来很大工作量.为了方便前期测试和后期线上部署更新,可使 ...
- 该死的Ubuntu 16.04不自动续租DHCP的IP
BUG,这是一个BUG,参考:https://bugs.launchpad.net/ubuntu/+source/isc-dhcp/+bug/1551351,如果不自动续租IP,导致的问题就是网线灯还 ...
- linux下TP5安装好Workerman 报错:Class 'think\worker\Server' not found
今天把功能放到服务器,本地测试正常,上传到服务器上报错Class 'think\worker\Server' not found 首先想到的是Windows和Linux下大小写的问题,查看了代码,并没 ...
- 主动找智能钥匙 PKE取代RKE是大势所趋
http://taobao.autos.cn.yahoo.com/ypen/20111128/725214.html 近日,在车友论坛上的一个热帖<悲喜交加:1分钟就能复制汽车遥控器?>在 ...
- A SCSI command code -- SIMPLIFIED DIRECT-ACCESS DEVICE (RBC)
SIMPLIFIED DIRECT-ACCESS DEVICE (RBC) ------------------------------------------ OP B Description -- ...
- 《TD式创新”祸国殃民》
TD式创新”祸国殃民> 作者:北京邮电大学 阚凯力 (2014年12月16日) 电信业内早就众所周知的TD-SCDMA真相,终于公之于天下.铁的事实是,它从来就不是什么“自主知识产权”,而是西门 ...
- superobject
GITHUB: https://github.com/hgourvest/superobject # SuperObject ## What is JSON ? - JSON (JavaScript ...