Connection Status:
Competition Arena > LevelUp
SRM 170 · 2003-11-08 · by antimatter · Simple Search, Iteration
Class Name: LevelUp
Return Type: int
Method Name: toNextLevel
Arg Types: (vector<int>, int)
Problem Statement

Problem Statement

You are playing TopRPG, the latest and hottest new console-style RPG (role-playing game) to hit the market. Much like many RPGs, it features the main characters getting stronger as the game progresses by obtaining a nebulous sort of thing known as experience, which is typically acquired by killing monsters.

The strategy guide open in front of you tells you how much experience you'll need to get to each level, represented as a int[] expNeeded. For example, if expNeeded were {150, 450, 900, 1800}, you would need a total of 150 experience to get to level 1 (from level 0), then another (450 - 150) = 300 experience to get to level 2, and so on.

You have 0 experience at the start, and you are about to go through an area where you will get a fixed amount of experience. You want to know how far you will be from the next level after you finish. Given a int[] expNeeded, where the ith element is the total amount of experience required to advance to level i, and an int received, indicating the amount of experience you will get, return an int which is the amount of experience it will take you to get to the next level after you receive the experience.

Constraints

  • expNeeded will contain between 1 and 50 elements, inclusive.
  • Each element in expNeeded will be greater than all elements with lower indices.
  • All elements in expNeeded will be between 1 and 999999, inclusive.
  • received will be between 0, inclusive, and the largest element of expNeeded, exclusive.
Examples
0)
{150,450,900,1800}
133
Returns: 17

You receive 133 experience, which isn't enough to get to level 1. You need is 150 - 133 = 17 more to get there.

1)
{150,450,900,1800}
312
Returns: 138

With the same situation, you now receive 312 experience. The first 150 advances you to level 1. There is 162 left, which then goes to the 450 - 150 = 300 experience you need to get from level 1 to level 2. Thus to get to the next level (2), you need 300 - 162 = 138 more experience.

2)
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,999999}
157632
Returns: 842367
3)
{150,450,900,1800}
612
Returns: 288
4)
{159,212,599,920,1055,1359,1838,2241,2693,4195,5381,6818,7838,8888,10591,18782,19298,22195,28571,33318,35838,38691,44129,49831,54203,59005,65536,78371,88888,91203,97381,105883,157372,188281,195105,215832,245773,274831,293030,315838,375045,391885,412583,433818,583772,677731,683871,735885,870938,999999}}
375612
Returns: 16273
5)
{150,450,900,1800}
450
Returns: 450

You advance exactly to level 2, and thus need 900 - 450 = 450 experience to get to the next level.

Submissions are judged against all 33 archived test cases, of which 6 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class LevelUp with a public method int toNextLevel(vector<int> expNeeded, int received) · 33 test cases · 2 s / 256 MB per case

Submitting as anonymous