BettingStrategy
SRM 339 · 2007-02-14 · by _efer_
Problem Statement
You are thinking of using the following betting strategy: in the first round, you bet one dollar. If you win the bet, you win the dollar and bet another dollar in the next round. Otherwise you lose the dollar and bet two dollars in the second round (provided you still have at least two dollars in your account). If you win, you get the two dollars and bet one dollar in the third round, otherwise you lose the two dollars and bet four dollars in the third round (provided you have at least that amount in your account) and so on. In other words, whenever you lose a bet, you double the value of the bet for the next round. Whenever you win, the bet for the next round will be one dollar.
For example, if you start with 10 dollars, and you win the bet in the first round, lose the bet in the next two rounds and then win the bet in the fourth round, you will end up with 10+1-1-2+4 = 12 dollars.
You will be given an
Constraints
- initSum will be between 1 and 1000, inclusive.
- outcome will contain between 1 and 50 characters, inclusive.
- Each character of outcome will be either 'W' or 'L'.
12 "WWWWWWWW" Returns: 20
You are really lucky. You win one dollar in each round, so you end up with 20 dollars.
12 "LLLLLLLL" Returns: 5
15 "LLLWLLLL" Returns: 1
After losing 7 dollars in the first three rounds, you have just enough money to cover your bet for the fourth round. You win 8 dollars in this round, but lose another 15 in the last four, so your final sum will be one dollar.
20 "WLWLWLWL" Returns: 23
You win one dollar in each odd numbered round and lose one dollar in the last round.
39 "W" Returns: 40
Submissions are judged against all 118 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class BettingStrategy with a public method int finalSum(int initSum, string outcome) · 118 test cases · 2 s / 256 MB per case