6
1
You are given a list, L, with the length N. L contains N random positive integer values. You are only able to select from the outer edges of the list (left/right), and after each selection every value in the remaining list increase by a factor of the current selection. The goal is to maximize the total sum of the selection you make until the list is empty.
An example of L where N = 3, [1,10,2].
Starting list [1,10,2], factor = 1.
- Chosen path left
New list [10 * factor, 2 * factor] => [20, 4], where factor = 2
- Chosen path right
New list [10 * factor] => [30], where factor = 3
Total value = 1 + 4 + 30 => 35.
Output: should be the total sum of all selections, and the list of all the directions taken to get there. 35 (left, right, left). Keep in mind that the the most important part is the total sum. There may exist more than one path that leads to the same total sum, for example 35 (left, right, right).
Test case 1
[3,1,3,1,1,1,1,1,9,9,9,9,2,1] => 527
(right left left left left left left left left right left left left left)
Test case 2
[1,1,4,1,10,3,1,2,10,10,4,10,1,1] => 587
(right left left right left left left left left left right right left right)
Rules
This is code-golf, shortest codes in bytes win.
Standard loopholes are forbidden.
I will update the question to make it more clear. The values of the remaining list increase by a factor of the current selection. In the first list [1,10,2] the factor is 1 since it is the first selection. Then the list becomes [10 * factor, 2 * factor], the factor is 2 since it is the second selection. The last list is [10 * factor], which is 3 since it is the third selection. I hope that makes it more clear. – feddy – 2018-10-07T19:20:16.617
Both, I will update the question. – feddy – 2018-10-07T19:33:20.967
You've tagged this with code-golf, could you make it explicit what is required to be valid and how answers are scored in the question? Also btw, if you visit our meta we have a sandbox where you can get feedback before posting. – Post Rock Garf Hunter – 2018-10-07T19:49:22.070
@JonathanAllan. I hope I understood you correctly, I added the two other possible paths for each test case. – feddy – 2018-10-07T19:51:08.740
@WW Sorry and thank you for the suggestion. I am going to check it out now to ensure it is properly formatted for next post. I have now removed the 'code-golf' tag from this post. – feddy – 2018-10-07T19:54:00.573
I must point out that all challenges here need some scoring criteria. That is a method of objectively ranking valid answers. [tag:code-golf] is one. When you say the goal is to maximize the sum of the selections, do you mean that a valid answer must output the maximum? – Post Rock Garf Hunter – 2018-10-07T19:56:30.837
1@JonathanAllan fixed! – feddy – 2018-10-07T19:57:17.127
@WW Yes I do, the answer must output the absolute maximum sum. How would you approach adding criteria to a question like this where there always exists a maximum path? – feddy – 2018-10-07T19:59:54.597
Since maximum path is already a fine validity criteria I think [tag:code-golf] would probably be the best fit. It is the most popular and usually considered the default. – Post Rock Garf Hunter – 2018-10-07T20:01:18.883
You misunderstood W W: there must be one accepted best answer which is what we mean by scoring criteria. – Quintec – 2018-10-07T22:27:46.073
@WW I think you are right. I have now added rules header! – feddy – 2018-10-08T05:28:10.163
@Quintec I absolutely did. The question is now updated, I have chosen code-golf. – feddy – 2018-10-08T05:28:37.210
@Quintec There doesn't necessarily have to be one accepted best answer for [tag:code-golf]. It's in fact discouraged to accept an answer for [tag:code-golf], since it is a competition for shortest in each language – Jo King – 2018-10-08T05:32:12.920
I'm a bit confused about the directions, and your example. Do the left and right indicate the leading and trailing numbers that will be removed and used for the sum? So with the example
[1,10,2]
and(left,right,right)
, it means we first take the left nr (1
) and multiply it with the factor (1
):1*1=1
, then we remove this left number leaving[10,2]
, then we take the factor2
with the right nr2
:2*2=4
, then remove this right number leaving[10]
, then we take the factor3
with the right (or left) nr10
:3*10=30
. And the result is the sum of those1+4+30=35
. – Kevin Cruijssen – 2018-10-11T06:43:24.763In your example, what do you mean by new list. You calculate
[10 * factor, 2 * factor] => [20, 4]
, but you're not using it later on.. This is the main part that confuses me in your example, if what I understand the challenge correctly by what I describe in my comment above. Also, if what I describe above is correctly, wouldn't[3,1,3,1,1,1,1,1,9,9,9,9,2,1]
start withright right
to maximize the sum. Maybe there is something wrong with my reasoning, but wouldn't we always want to have the smallest leading/trailing nr with the current factor to maximize the sum. – Kevin Cruijssen – 2018-10-11T06:45:48.6031@KevinCruijssen As best as I can tell, you understand the algorithm correctly. And yet, if you start with
(right, right, ...)
, then the maximum you can get seems to be523
. The reason as to why becomes a bit clearer (to me, at least) when you think of this backwards. Start by multiplying the9
s by large numbers that keep getting smaller. Then you have the option of multiplying a relatively large number by1
on the left, or2
on the right. You probably want to multiply by2
, which is why you don't take it immediately, and why the first moves are(right, left, ...)
. – zgrep – 2018-10-15T07:56:25.083@KevinCruijssen I'm not certain how addition works for you, but adding together everything in the left column of your pastebin sums to 523 for me:
– zgrep – 2018-10-16T00:34:34.653+/(1+!14)*1 2 3 1 3 1 1 1 1 1 9 9 9 9
in k.@zgrep Woops.. You're indeed right.. Not sure how that happened.. I'll delete my comment and will take a look at the path of the current solution. – Kevin Cruijssen – 2018-10-16T06:17:09.377