If one has a jagged array and the "true index" of an element, which is just the index of the element if the array was flattened (coined by me, I don't know what that's called), what is the fastest way to retrieve the element, given it's index?
For example, take this array:
[[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9, 10, 11, 12, 13],
[14, 15],
[16, 17, 18]]
The value 11
in this jagged 2D array would have an index of 11
. What is the fastest and cleanest way to retrieve the value of an element in a jagged array (let's say it's 2D if that makes it easier) given it's "true index"?
Would appreciate a javascript answer, but as this is a general algorithmic question, I don't care what language it is in. If you will provide an answer that is not in javascript, perhaps don't use libraries/modules that wouldn't be available in javascript.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…