r/programmingchallenges • u/MyGiantTest • May 07 '18
Coding challenge about two superheros destroying a city.
Hello, I was doing this coding challenge to improve my skills and while I could understood the easy example (provided in the link) or other examples with easy combinations, I couldn't understand how to work with more complex ones.
In the first image you can read the text of the problem but basically there is a city with n buildings. Each building has a certain height. There are 2 superheros, one on the left and one on the right. The superhero on the right destroy building with an increasing height from his perspective while the one on the left does the same thing but with his point of view. You need to tell how many moves they need to destroy the city.
Link with details: https://imgur.com/a/4srl9Vo
Example (numbers are the height of the buildings): 1 2 5 3 1.
-> First move superhero left: destroy 1, 2 and 5.
-> Second move superhero right: destroy 1 and 3.
-> All buildings destroyed, answer is 2.
This was pretty easy. What I can't understand is what happens in a scenario like this.
- Buildings array: 100 10 1 200 40 30 10 25 30.
I know that the answer is 4. But i don't understand what's supposed to happen in a scenario where left has the first building with a bigger height than the second one. Does he destroy the first one? Does he advance until he can destroy something (like 1 and 200). I lost hours trying various things but i can't come up with 4 moves in a reasonable way. Probably I am overthinking.
Sadly there aren't more instructions.
View all comments
Show parent comment
1
u/fresnik May 07 '18
That is true, it does not specifically say that they must alternate, in which case we can get 4.
Therefore it seems that the challenge is a typical DP problem. You need to try every possible solution but store known outcomes (memoization).