7 kyu

Simple Fun #334: Two Beggars And Gold

421 of 899myjinxin2015

Description:

Task

In the field, two beggars A and B found some gold at the same time. They all wanted the gold, and they decided to use simple rules to distribute gold:

They divided gold into n piles and be in line. 
The amount of each pile and the order of piles all are randomly.

They took turns to take away a pile of gold from the 
far left or the far right.

They always choose the bigger pile. That is to say, 
if the left is 1, the right is 2, then choose to take 2.

If the both sides are equal, take the left pile.

Given an integer array golds, and assume that A always takes first. Please calculate the final amount of gold obtained by A and B. returns a two-element array [amount of A, amount of B].

Example

For golds = [4,2,9,5,2,7], the output should be [14,15].

The pile of most left is 4, 
The pile of most right is 7, 
A choose the largest one -- > take 7

The pile of most left is 4, 
The pile of most right is 2, 
B choose the largest one -- > take 4

The pile of most left is 2, 
The pile of most left is 2, 
A choose the most left one -- > take 2

The pile of most left is 9, 
The pile of most right is 2, 
B choose the largest one -- > take 9

The pile of most left is 5, 
The pile of most left is 2, 
A choose the largest one -- > take 5

Tehn, only 1 pile left, 
B  -- > take 2

A: 7 + 2 + 5 = 14
B: 4 + 9 + 2 = 15

For golds = [10,1000,2,1], the output should be [12,1001].

A take 10
B take 1000
A take 2
B take 1

A: 10 + 2 = 12
B: 1000 + 1 = 1001
Fundamentals

Stats:

CreatedJun 29, 2017
PublishedJun 29, 2017
Warriors Trained1648
Total Skips30
Total Code Submissions4593
Total Times Completed899
JavaScript Completions421
Python Completions434
Ruby Completions65
Crystal Completions7
Scala Completions10
CoffeeScript Completions6
Total Stars38
% of votes with a positive feedback rating91% of 233
Total "Very Satisfied" Votes198
Total "Somewhat Satisfied" Votes28
Total "Not Satisfied" Votes7
Total Rank Assessments9
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • ZozoFouchtra Avatar
  • GiacomoSorbi Avatar
  • user9644768 Avatar
  • Just4FunCoder Avatar
  • KayleighWasTaken Avatar
  • saudiGuy Avatar
Ad