Last Updated: 22 Apr, 2021

Ninja World Tournament

Easy
Asked in company
Amazon

Problem statement

Ninja was feeling bored during the lockdown. So, he decided to watch the Ninja World Tournament. The tournament consists of several matches, where the scores of past matches may affect future matches’ scores.

At the beginning of the match, every player starts with an empty track record. Ninja wants to calculate the final score for a player. So, given a list of strings, ‘MATCHRESULT’, where ‘MATCHRESULT[ i ]’ is the ‘i’th operation Ninja must apply to the track record and is one of the following:

1) An integer “A”: Introduce a new score of ‘A’ on the track record.
2) "+": Introduce a new score on the track record that is the sum of the previous two scores.
3) "C": Nullify the previous score, removing it from the track record.
4) "D": Introduce a new score on the track record that is double the previous score.

Ninja deduced that this could be easily solved using programming. So, he needs your help to calculate the sum of all the scores present in the track record.

Note:

It is guaranteed there will always be a previous score before the “C” and “D” operations and two previous scores before the “+” operation.

Input Format:

The first line of input contains an integer 'T' representing the number of test cases.

The first line of each test case contains an integer ‘N’ representing the number of operations in the list of strings, ‘MATCHRESULT’.

The next line of each test case contains ‘N’ space-separated strings representing the operations in the list of strings, ‘MATCHRESULT’.

Output Format:

For each test case, return the sum of all the scores on the track record.

The output of each test case will be printed in a separate line.

Constraints:

1 <= T <= 5
1 <= N <= 1000
MATCHRESULT[ i ] ∈ {[-3 * 10 ^ 4, 3 * 10 ^ 4], “+”, “C”, “D”}

Where ‘T’ is the number of test cases, ‘N’ is the number of operations in the list of strings, ‘MATCHRESULT’ and ‘MATCHRESULT[ i ]’ is the ‘i’th operation in the list of strings, ‘MATCHRESULT’.

Time limit: 1 second.

Note:

You do not need to print anything, it has already been taken care of. Just implement the given function.

Approaches

01 Approach

The idea is to use the brute force approach. We will traverse the list of strings, ‘MATCHRESULT’ and introduce the score in the ‘TRACKRECORD’ as per the given conditions.

 

Algorithm:

  • Declare a vector of integers, ‘TRACKRECORD’ to record the scores.
  • Run a loop for each operation string, ‘OP’ in ‘MATCHRESULT’.
    • if OP == “+”, that shows that we have to introduce a new score which is sum of the previous two scores. Therefore,
      • Sum the previous two scores and insert it at the end of the ‘TRACKRECORD’.
    • Else if OP == “C”, that shows that we have to nullify the previous score. Therefore,
      • Remove the last element from the ‘TRACKRECORD’.
    • Else if OP == “D”, that shows that we have to introduce a new score which is the double of the previous score. Therefore,
      • Double the previous score and insert it at the end of the ‘TRACKRECORD’.
    • Else, ‘OP’ is an integer string. Therefore,
      • Convert the string ‘OP’ to integer and insert it at the end of the ‘TRACKRECORD’.
  • Declare an integer variable, ‘SUMSCORE’ to store the sum of all the scores present in the ‘TRACKRECORD’.
  • Run a loop for each integer variable, ‘SCORE’ in the ‘TRACKRECORD’.
    • Add ‘SCORE’ to the ‘SUMSCORE’.
  • In the end, return ‘SUMSCORE’.