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
Similar Kata:
Stats:
Created | Jun 29, 2017 |
Published | Jun 29, 2017 |
Warriors Trained | 1648 |
Total Skips | 30 |
Total Code Submissions | 4593 |
Total Times Completed | 899 |
JavaScript Completions | 421 |
Python Completions | 434 |
Ruby Completions | 65 |
Crystal Completions | 7 |
Scala Completions | 10 |
CoffeeScript Completions | 6 |
Total Stars | 38 |
% of votes with a positive feedback rating | 91% of 233 |
Total "Very Satisfied" Votes | 198 |
Total "Somewhat Satisfied" Votes | 28 |
Total "Not Satisfied" Votes | 7 |
Total Rank Assessments | 9 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 8 kyu |