Question about 6.4 in 1.4.7

#1

Hi,

Why does the solution use last_reach? It doesn’t seem to be very useful. Can we just do -

bool can_reach(const vector& A) {
int farthest_reach = 0;
for (int i = 0; i < A.size(); ++i) {
if (i > farthest_reach) {
return false;
}
farthest_reach = max(farthest_reach, i + A[i]);
}
return true;
}

We just need to update farthest_reach and check whether i goes beyond it, right?

edit: sorry abt the format, it seems to ignore the leading white spaces when display the post.

0 Likes

#2

Hey Ray,

Thanks for your question, and following is my improved code at https://github.com/epibook/epibook.github.io/blob/master/solutions/cpp/jump-game.cc recently, please take a look and provide your comment.

I think you are right, it definitely has room to improve, and it is a very important thing in interview. Try to refactor your code after you complete the initial version.

0 Likes

#3

Thanks! Looks awesome. Is github’s code newer than google’s? Is there a plan to consolidate them?

0 Likes

#4

Hey Ray,

We are working on migrating to gitHub (where most developers in the world use). Eventually we shall retire the Google Code side.

1 Like

#5

Hi Tsung-Hsien,

Will future versions of the book reflect the improved code as well? I see several other better coded solutions in your gitHub, e.g. 8.10, 8.13 in 1.4.7. Thanks.

0 Likes

#6

Hey Ray,

We do push new codes to future versions of EPI. However, we haven’t done that push for at least two months since too rapid push may cause trouble to our readers. Thanks for your asking.

0 Likes