Codey and Team Selection
https://www.hackerrank.com/contests/codenection-2023-preliminary-round-closed-category/challenges/cn-c6
Question
Codey is a coach, and it is assembling a team for a competitive sports league. It is going to select n players for the team. Each player possesses a skill point represented by ai. The player selection process consists of n steps, and in each step:
Let
bto be the sorted array of skill points for unselected players. Choose an integerj, where 1≤j≤k, and k is the current total of unselected players, that player joins the team and gains an additional skill point, sj. In other words, the total team skill increases by bj+sj.
Codey wants to strategically select players to maximize the overall team skill for the upcoming sports league, while following the constraints of the selection process. Can you help Codey to achieve this?
Input Format
The first line contains a single integer n, which represents the number of players.
The second line contains n integers a1,a2,...,an, each representing the skill points of the i-th player.
The third line contains n integers s1,s2,...,sn, each representing the additional skill points gained for selecting the j-th player in the array b.
Constraints
Output Format
Output an integer representing the maximum total team skill.
Sample Inputs:
Input
Output
Explanation
We can select the players as such:
The current team skill is 0 and
b = [1, 2, 3]. We choosej = 1and the team skill increase by b1+s1=1+3=4.The current team skill is 0+4=4 and
b = [2, 3]. We choosej = 2and the team skill increase by b2+s2=3+3=6.The current team skill is 4+6=10 and
b = [2]. We choosej = 1and the team skill increase by b1+s1=2+3=5.All players are selected, selection ended. The total team skill is 0+4+6+5=15.
Solution - Question is Confusing
This question is so confusing that I couldn't finish at the competition QAQ
After been three days of reading, finally I get it how the coach selects the players. Here's the details:
Each player position affects how many skill points, sj, can be chosen.
For example, player[0] can only choose s[0], player[1] can choose between s[0] and s[1], and so on.
Which means, we had to do two loops which:
loop through players and indexes
find the maximum skill points that player can get based on the position of player.
But this will get a time limit exceeded, as the array is large enough to cough the program out of time.
One way I thought to optimize it is by memorization, which is to by memorize the current maximum value from that array, next time only need to compare that max with the next index cell.
And not surprising, memorization works and passed all test cases!
Here's my code:
Last updated