Description

In the four fundamental arithmetic operations, associative law is valid for plus (+) but not for minus (-).
For example, the expression 1 - 5 - 3 produces different results according to the following calculations.

  • ((1 - 5) - 3) = -7
  • (1 - (5 - 3)) = -1

As another example, the expression 1 - 3 + 5 - 8 produces 5 different results.

  • (((1 - 3) + 5) - 8) = -5
  • ((1 - (3 + 5)) - 8) = -15
  • (1 - ((3 + 5) - 8)) = 1
  • (1 - (3 + (5 - 8))) = 1
  • ((1 - 3) + (5 - 8)) = -5

As shown above, the results of different calculation order are [-15, -5, -5, 1, 1], where the maximum value is 1.
Given an array arr containing numbers in string format, plus symbol (+), and minus symbol (-) as parameters, write a function solution to return the maximum value among results of different calculation order.

Constraints
  • arr is an array containing two operators, "+" and "-", and numbers. Its length is between 3 and 201.
    • The length of arr is always odd value.
    • arr has more than or equal to 2 and less than or equal to 101 numbers. The number of operators is (the number of digits) -1.
    • The numbers in arr are between 1 and 1,000 and in string format (ex: 456).
  • The first and last elements of array are always numbers, and number and operator always appear alternately.

Examples
arr result
["1", "-", "3", "+", "5", "-", "8"] 1
["5", "-", "3", "+", "1", "+", "2", "-", "4"] 3

Example #1
As shown in above example, (1-(3+(5-8))) = 1.

Example #2
(5-(3+((1+2)-4))) = 3.

Result Stop
내가 제출한 코드가 왜 틀렸는지 프로그래머스 AI에게 물어보세요.
제출 후 채점하기를 눌러 30점 이상인 경우 물어볼 수 있어요.
베타 기간 동안에는 한 문제당 1번만 물어볼 수 있어요.