[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 ...
随机推荐
- luoguP4571 [JSOI2009]瓶子和燃料 裴蜀定理
裴蜀定理的扩展 最后返回的一定是\(k\)个数的\(gcd\) 因此对于每个数暴力分解因子统计即可 #include <map> #include <cstdio> #incl ...
- 异常:The server committed a protocol violation
异常记录: Exception rethrown at [0]: 在 Wintop.Windows.FrmLogin.btnLogin_Click(Object sender, EventArgs e ...
- [BZOJ3928/4048]Outer space invaders
[BZOJ3928/4048]Outer space invaders 题目大意: 有\(n(n\le300)\)个物品,第\(i\)个物品会在\(a_i\sim b_i\)时刻出现,且离你的距离为\ ...
- HDU 1269 移动城堡 联通分量 Tarjan
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- flask 中 session的源码解析
1.首先请求上下文和应用上下文中已经知道session是一个LocalProxy()对象 2.然后需要了解整个请求流程, 3.客户端的请求进来时,会调用app.wsgi_app(),于此此时,会生成一 ...
- DeJaVu update history
17.05.08 <-> Added Audi RB8 random code direct change -> Now can adapt VIN based keys or ke ...
- MP1593 RT8272 ACT4070 制作的DC-DC稳压电源
http://www.ideyi.org/article/11-05/2575971305526398.html?sort=2068_2073_2092_0 MP1593制作的DC-DC稳压电源,这款 ...
- How to match between physical usb device and its drive letter?
struct tagDrives { WCHAR letter; WCHAR volume[ BUFFER_SIZE ]; } g_drives[ ]; // WCHAR GetUSBDrive( ) ...
- sql server 游标continue,总是死循环
也遇上过: 死循环是因为continue后又执行与上次相同的fetch了.在continue前加一个fetch next from就可以了.
- MVC使用Dotnet.HighCharts做图表01,区域图表
如果想在MVC中使用图表显示的话,DotNet.HighCharts是不错的选择.DotNet.HighCharts是一个开源的JavaScript图表库,支持线型图表.柱状图标.饼状图标等几十种图标 ...