Santa visited you night, he had n chocolates. Each chocolate fills your heart with some amount of happiness.
The
i
th chocolate fills your heart with
a
i
happiness.
He placed the chocolates in front you in a table in this order -
1 (a_1) ,2 (a_2) ,3 (a_3)... n (a_n)
So Santa told you that you can choose as many chocolates you want to have but you cannot choose consecutive chocolates. (If you choose i th chocolate then you cannot choose ( i + 1 ) th chocolate or ( i − 1 ) th chocolate)
The total happiness is the sum of happiness of the choosen chocolates.
So obviously you want to maximize your total happiness.
So you are given n = 14
a 1 = 1 3 a 2 = 1 4 a 3 = 2 a 4 = 3 a 5 = 1 a 6 = 5 a 7 = 4 a 8 = 1 8 a 9 = 1 7 a 1 0 = 1 9 a 1 1 = 5 a 1 2 = 6 a 1 3 = 9 a 1 4 = 1
This problem is a part of Tessellate S.T.E.M.S.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The idea of dynamic programming might be helpful too. But good job on figuring this out
Log in to reply
Just came across dynamic programming. Thank you very much!
Explanation:
The maximum attainable happiness using the first k chocolates is going the be the largest of either
Python implementation:
1 2 3 4 5 6 7 8 9 |
|
Problem Loading...
Note Loading...
Set Loading...
As said by the problem, if one chooses the ( i ) th chocolate, one cannot then choose ( i − 1 ) th or ( i + 1 ) th chocolate. Therefore, in order to get the highest outcome, you have two possible start points for this: either a 1 = 1 3 or a 2 = 1 4 (you could start with any, but if you start with a later number, you'll miss some of the largest numbers of this set).
1) Choosing the ( i + 2 ) th option each time
Starting with a 2 gives you a better outcome, but we can do better than this. Why? The 6 largest numbers in this set are 9, 13, 14, 18, 17 and 19, which total to 90. We know that we cannot reach this total as (1) 17, 18 and 19 are besides each other, hence we cannot choose all three and (2) 13 and 14 are also besides each other. The first one includes 3 of them (13 + 17 + 9 = 39) and the second one also has 3 of them (14 + 18 + 19 = 51). However, they both have extremely small integers, too, such as 1. Choosing the ( i + 2 ) th method doesn't prove to be in this case.
2) Alternating between choosing the ( i + 2 ) th AND the ( i + 3 ) th option. This involves a little bit of looking ahead at the rest of the numbers in order to come up with the best result of happiness. Taking a 1 = 1 3 again, we now have the option of choosing a 3 = 2 or a 4 = 3 . Looking at solely these values will lead one to think about choosing a 4 , but before choosing that, we need to look at the option that would follow if we were to choose either. If I chose a 3 = 2 , then the (i+2)th option is a 5 = 1 . The addition of these two equals 3. Similarly, if I were to take a_4 = 3, then the next one would be a 6 = 5 , which both add up to 8 > 3. Therefore, we choose a 4 and a 6 . Using this method, we will get: 13 + 3 + 5 + 18 + 19 + 9 = 67.
However, going back to the start, if we were to choose a 4 , then we could start with both a 1 = 1 3 (as we did earlier) or alternatively a 2 = 1 4 . This addition of 1 to the total gives you 68. This number is also made up of 4 of the 6 largest numbers: 9, 14, 18 and 19.
Final solution: 14 + 3 + 5 + 18 + 19 + 9 = 68
(I don't really have an efficient method, so please do correct me or suggest improvements where possible.)