Statistics

Problem Statement for "MaximumRangeDiv2"

Problem Statement

Cat Noku was given a calculator for his birthday. The calculator is very simple. It can only store a single variable. The variable is called X and its initial value is 0. The calculator has only two buttons: + and -. Pressing + increments X by 1 and pressing - decrements X by 1. For example, if X is 4 and Noku presses +, X is incremented to 5.

A string of '+' and '-' characters can be seen as a sequence of instructions to press the corresponding buttons. The range of such a sequence of instructions is the difference between the largest and the smallest value stored in X while executing that sequence of instructions, in order.

For example, the range of "+++++++" is 7: the largest value of X is 7 (at the end) and the smallest value is 0 (in the beginning). The range of "---" is 3: maximum is 0, minimum is (-3), their difference is 0 - (-3) = 3. The range of "+-+-+-" is 1. The range of an empty sequence of instructions is 0.

Noku's calculator came with a piece of paper that contained a String s. Each character of s was either '+' or '-'. Noku will choose and execute any (not necessarily contiguous) subsequence of these characters. Help him do so in a way that maximizes the range of the executed sequence.

Compute and return the maximal range of a subsequence of the String s.

Definition

Class:
MaximumRangeDiv2
Method:
findMax
Parameters:
String
Returns:
int
Method signature:
int findMax(String s)
(be sure your method is public)

Constraints

  • s will contain between 1 and 50 characters, inclusive.
  • Each element of s will be '+' or '-'.

Examples

  1. "+++++++"

    Returns: 7

    One optimal solution is to skip the fourth instruction (the '+' in the middle of s). Thus, the sequence of button presses will be "+----+". The value of X will change as follows: 0,1,0,-1,-2,-3,-2. The maximum number we see is 1 while the minimum number is -3, so the range is 1 - (-3) = 4.

  2. "+--+--+"

    Returns: 4

    One optimal solution is to skip the 4th instruction. Thus, the sequence of button presses will be "+----+". The number we see after each button press is 1,0,-1,-2,-3,-2. The maximum number we see is 1, while the minimum number is -3, so their range is 4.

  3. "++++--------"

    Returns: 8

  4. "-+-+-+-+-+-+"

    Returns: 6

  5. "+"

    Returns: 1

  6. "---"

    Returns: 3

  7. "---------+------"

    Returns: 15

  8. "--+--+-+-++-+-++---"

    Returns: 11

  9. "-----------------------------------------"

    Returns: 41

  10. "+++-++-"

    Returns: 5

  11. "++++-+-++++++-++-+++"

    Returns: 16

  12. "-----+"

    Returns: 5

  13. "-+-+----++-----+-+--+-+--------+-++---+-++-+--+"

    Returns: 31

  14. "--------------------------------+-----------+-"

    Returns: 44

  15. "-+-"

    Returns: 2

  16. "-----+--------"

    Returns: 13

  17. "----+------+-------------------+--++----+--+-"

    Returns: 38

  18. "+-+++-++++-+++-++"

    Returns: 13

  19. "-+++++-++++++--++++-++++--+-++++"

    Returns: 24

  20. "--+--++----+-+++----+++-+---+-+-+--+-----+"

    Returns: 26

  21. "----"

    Returns: 4

  22. "-+++-+++++-+++-+-++++++++-+++++++-"

    Returns: 27

  23. "--+---+-+-+++----+------+++--+----++----------+"

    Returns: 33

  24. "++++++-++--+++++----++-++-+++-"

    Returns: 20

  25. "-+--+-++-+--+---+++----++------+-"

    Returns: 21

  26. "+--++---"

    Returns: 5

  27. "---+---+--+------"

    Returns: 14

  28. "-+--+----+-----------------------+----+-+-"

    Returns: 36

  29. "---+-+-+--+---+++++-+-+-+--"

    Returns: 15

  30. "++-+++--+----+-----+-++---+++++-++++-+---+-++-++-+"

    Returns: 26

  31. "+-+++++"

    Returns: 6

  32. "++--+++++++++++-++++++++++++"

    Returns: 25

  33. "+++"

    Returns: 3

  34. "+-++-++++--+++----+--+--++--++--+++--+--+---+"

    Returns: 23

  35. "+++-+-++++++-+-+++++-+++++++-+--++-++-++-+--++++-"

    Returns: 35

  36. "++++++++++++++++++++++++++++++"

    Returns: 30

  37. "-+-++------++--++----++---"

    Returns: 17

  38. "+++"

    Returns: 3

  39. "+-++--+-+-"

    Returns: 5

  40. "+-++"

    Returns: 3

  41. "+--+++++++++-++++++"

    Returns: 16

  42. "--------"

    Returns: 8

  43. "------"

    Returns: 6

  44. "+"

    Returns: 1

  45. "-+-+------+-++-"

    Returns: 10

  46. "+-++----+--------+---+--++-----------+--+--+--+-"

    Returns: 36

  47. "++++++++++-++++++++++++++-++++++++++++++-++++++"

    Returns: 44

  48. "-++------+-+-+--+-----++-+-+-+--+-+++-++++-+"

    Returns: 24

  49. "+-+---++++-+--++---+--+++--+-------+++++++++++---+"

    Returns: 26

  50. "++--++-+++++-++-+-++---++--+----+--+"

    Returns: 19

  51. "+++-+++++++++-+++++++++-+--+++++"

    Returns: 27

  52. "++++++-++++++-+++++++++--+-++--+++++--+++++++"

    Returns: 36

  53. "+-----+---------------+-+"

    Returns: 21

  54. "++--+-+-+--+-++++-++++-+++--+++++-++-++++--+++++-+"

    Returns: 34

  55. "+++++++++-+++++++++++++++++++++++++-+++++++++++++"

    Returns: 47

  56. "+-+-+-"

    Returns: 3

  57. "-+-+-+-+-+-+"

    Returns: 6

  58. "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-"

    Returns: 25

  59. "+-+-+-+-+"

    Returns: 5

  60. "-------"

    Returns: 7

  61. "+-++--"

    Returns: 3

  62. "++++++++++++++++++++++++++++++++++++++++++++++++++"

    Returns: 50

  63. "+--+--+"

    Returns: 4

  64. "++-+"

    Returns: 3

  65. "+++++++++-----+-+-+-+-+"

    Returns: 14


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: