Hacker News new | ask | show | jobs
by acchow 4612 days ago
Your solution doesn't work when there is more than one "puddle":

https://gist.github.com/alexchow/7228746

Try the case: ([2,5,1,2,3,4,7,7,6,3,5], 12)

1 comments

it's not "more than one puddle"; it's that i don't count the second puddle because i don't ever "close" it by encountering it's level again. if i go off the edge, i should close it as best i can.
Ah, right. Now that I actually read your code, that's the real bug :p

The issue with this "state machine" approach is you don't have "lookahead".

In the two pass approach (approach from one side, then the other), you essentially have two machines, one implicitly serving as the "lookahead" for the other.