# [Community Puzzle] Robbery optimisation

#1

https://www.codingame.com/training/easy/robbery-optimisation

Created by @Potemkine,validated by @JBM,@Niako and @Alain-Delpuch.
If you have any issues, feel free to ping them.

#2

Hello amigos.

I fail the 50, 75 and 100 tests in this one. Any tips about the flaws in my alogorythm?

#3

Youâ€™re probably using an exponential-class algorithm. Thatâ€™d pass 20 and smaller.

#4

I donâ€™t think so, since I donâ€™t even know what that is.

What I do is making two arrays. One with the house value as value and the house number as key.
I then create a second array that has true as value and the house number as a key.

Then, I find the biggest house value and :

• add it to the thief total
• turn the value to 0
• turn the house in the second array to false
• turn the house to the right to false in the second array (if it exists)
• turn the house to the left to false in the second array (if it exists)

For the 8 houses cases, I had to check if the last biggest house value wasnâ€™t bigger than the sum of the values of the house to the left and right.

I believe the 50,75 and 100 may have a trick that looks like that. Any clue?

#5

I even fail at 20 houses and above itâ€¦
What I do is put index key for every houses then sorting it by first element (for values content).

sorted by first element
[ [ 12, 13 ], [ 11, 12 ], [ 10, 2 ], [ 10, 17 ], [ 9, 19 ], [ 9, 15 ], [ 8, 5 ], [ 7, 18 ], [ 7, 10 ], [ 7, 9 ], [ 6, 11 ], [ 6, 8 ], [ 6, 16 ], [ 5, 1 ], [ 5, 14 ], [ 5, 6 ], [ 4, 4 ], [ 2, 7 ], [ 2, 3 ], [ 2, 0 ] ]

filter by second element which not a side by side
[ [ 12, 13 ], [ 10, 2 ], [ 10, 17 ], [ 9, 19 ], [ 9, 15 ], [ 8, 5 ], [ 7, 10 ], [ 6, 8 ], [ 2, 0 ] ]

what i found is 73 but should expected 75.

#6

Well then, it seems like youâ€™re using a slightly evolved greedy algorithm. But are you sure it should work?

Letâ€™s consider the simple greedy (just take the best available).

It works for this: 1 0 1 â€” take the outside houses and you get the best solution 2.
It fails for this: 2 3 2 â€” take the best (middle) house of 3 and fail to get the best solution of 4.

IIUC your trick is [some variation of] avoiding the situation above by considering blocks of three.

But thatâ€™s still complicated by cases such as this: 3 4 3 4 3 â€” which blocks should be considered? Can it generalize?

So, in a nutshellâ€¦ I think your algorithm may be incorrect.

#7

If it helps convince you one exists (or debug your algorithm), hereâ€™s one selection on the 20-house test case that yields a total value of 75:

• (0,2)
• (2,10)
• (5,8)
• (7,2)
• (9,7)
• (11,6)
• (13,12)
• (15,9)
• (17,10)
• (19,9)

#8

Hi there. This is not an easy puzzle, please at least move it to medium. New players coming into this will be disgusted, it requires some skills to figure it out.

#9

The puzzle is in â€śmediumâ€ť now.

#10

Actually right now it seems to be back to easy!?

Iâ€™ve also noticed that the difficulty of some problems seems arbitrarily modified after validation (e.g. this one or this one which were both validated as hard and are now medium). Is there a rational explanation or are some people arbitrarily doing that without ever notifying it in the comments/forums?

#11

I wish weâ€™d get back to a simple â€śitâ€™s as easy as how many people solved itâ€ť

#12

I am confused by test case 06 (DonÂ´t just alternate houses).

The houses are listed as

a 5
b 1
c 15
d 10
e 13
f 16

The solution given is 31, which is 5 + 10 + 16 (houses a, d & f).
But if the robber robs houses a, c & f he gets 5 + 15 + 16 = 36.
Am I missing something?

Thanks,

#13

5 is not a house, just the nomber of house so itâ€™s :
a 1
b 15
c 10
d 13
e 16

And in fact b + e = 31

#14

Itâ€™s quite possible that people just move it around.
Or maybeâ€¦ Maybe when someone else has had the puzzle open to edit something else and meanwhile someone (e.g. eulersche) moved the puzzle to medium and the first one saved his changes, they overwrote the difficulty change.

#15

And there is the failure, right between the keyboard and the chair. Thanks!

#16

@Fluttershy AFAIK the server refuses to update the statement if your modifications are not based on the latest revision number. So that should not be incidental.

#17

Hi there.
I started this puzzle while it was still on easy.

It seems it does not take into account my modification and that the puzzle is still in the â€śeasy suggestionsâ€ť for me.

#18

@PatrickHectorPatrick As I said above, right now the puzzle is back to easy, most probably because somebody moved it back there without notifying anyone about it.

#19

What? Is that possible? Can we crush his soul?

#20

Who cares what category itâ€™s in?

The only sensible linear indication of difficulty is the number of people who solved it. Period.