이경로 2022. 12. 5. 21:42

Difficulty : Gold V

Problem Description

There is an array of digits.

You have to make a right equation using '-', '+'. '=' should be between the last digit and previous digit.

The output is the count of right equations that you can make.

 

Solution

I solved this problem by counting the number of cases of each digits.

 

'n' and 'l' is the input, and 's' is the last digit for output. The last digit isn't necessary for the input list, so pop it.

a is the count of each numbers. The index of the first input is 1 because of the initial value.

 

't' is temporal list for stroring the count.

If 'i' is 0, counts are the same with previous counts, so pass this repetition.

Otherwise, find the numbers that can be made up of previous numbers and 'i' and add all the counts.

Finally, input current count 't' into 'a'.

 

In addition, here is an example to explain why 't' is necessary.

In this case, a[6]=1. Digit 2 is already used to make 6, so it can never be used later.

But if there are not 't', digit 2 is used again by a[6]. This is the reason why I used an temporal list 't'.