Statistics

Problem Statement for "LostParentheses"

Problem Statement

We have an arithmetic expression made up of positive integers, the + and - operators and parentheses. All the parentheses, however, have been erased by the cleaning staff and we want to calculate the minimum value the original expression may have had.

You will be given a String e containing the expression without the parentheses. Return the minimum value the original expression could have had before the parentheses were erased.

Definition

Class:
LostParentheses
Method:
minResult
Parameters:
String
Returns:
int
Method signature:
int minResult(String e)
(be sure your method is public)

Notes

  • All operations in the original expression were addition and subtraction; there were no parentheses placed between two consecutive digits.

Constraints

  • e will contain between 1 and 50 characters, inclusive.
  • Each character of e will be a digit ('0'-'9'), a plus sign ('+') or a minus sign ('-').
  • The first and last characters of e will be digits.
  • No two operators (characters '+' and '-') will appear consecutively in e.
  • There will not be a sequence of more than 5 consecutive digits in e.

Examples

  1. "55-50+40"

    Returns: -35

    The expression can only have two different values: 55-50+40=45 and 55-(50+40)=-35.

  2. "10+20+30+40"

    Returns: 100

    Since the sum is associative, any parenthesization of the expression would get the same result.

  3. "00009-00009"

    Returns: 0

    Numbers may contain leading zeroes.

  4. "50980-46441-24363-78452+26293+52584-94259-46416+29"

    Returns: -317857

  5. "62027+93403-16025+47588-94607+16226-24573"

    Returns: -43589

  6. "38222-5957+82602"

    Returns: -50337

  7. "81527-18177+61845+65698-17591+95310-74537+43131"

    Returns: -294762

  8. "7137-86285-90051+56183+18972-238"

    Returns: -244592

  9. "99795+89526+70916-92094-61129+45"

    Returns: 106969

  10. "16975+91232+63135-54496+90711+949-96915-13844+7002"

    Returns: -92575

  11. "7379-83954+27220"

    Returns: -103795

  12. "40965-39362-36823-80824+80367-61673-4820+95110"

    Returns: -358014

  13. "25698-30039+32266+60021-45328+50319+20492-13190"

    Returns: -225957

  14. "41"

    Returns: 41

  15. "4450"

    Returns: 4450

  16. "42739+87891+85469-2858-65821+48382+4997+26650+7308"

    Returns: 60083

  17. "86133+8985-12804"

    Returns: 82314

  18. "21196-31278-722"

    Returns: -10804

  19. "27221+97756+91020-75775-91072-92305-86067-44993"

    Returns: -174215

  20. "50956+7007+67669+30498-24273+61443+15738-64438+205"

    Returns: -9967

  21. "9074"

    Returns: 9074

  22. "90995-51290+67196+12592-50"

    Returns: -40133

  23. "5810+97806+39236+90152+70512-99317+84381+71191+89"

    Returns: 48538

  24. "270"

    Returns: 270

  25. "83356+41708-77"

    Returns: 124987

  26. "80652+20578+17593+13825+18993+97165+49485+47932-52"

    Returns: 346171

  27. "69594-28319+40247-60113"

    Returns: -59085

  28. "42793-20923+58263+47877+19356+843"

    Returns: -104469

  29. "50830+98022+5257+7612+72785+93704+77590+20710-161"

    Returns: 426349

  30. "35219-64702-71992-42002-31425-4018+99357-17501+271"

    Returns: -296049

  31. "61584-68038-93492+29808-1568+34442-85612-7725-34"

    Returns: -259135

  32. "11884-64635+2990-91109-52105+12769+294"

    Returns: -212018

  33. "42848-54676-42981-87989-3248-69503+4095-2706-3454"

    Returns: -225804

  34. "19925+20772+25099-21462-8740-360"

    Returns: 35234

  35. "62268-98376"

    Returns: -36108

  36. "41978-46051+26287+1791+24953-88957-59004-7461-1395"

    Returns: -213921

  37. "15280+37207-65852"

    Returns: -13365

  38. "9347+71262-41"

    Returns: 80568

  39. "62210-35286-97259-30790-10874-24444-74807+96161"

    Returns: -307411

  40. "51226-46442-96439-17463-55061-39777-98602-52174-37"

    Returns: -354769

  41. "30731-7118"

    Returns: 23613

  42. "96918-82628-40958-8114"

    Returns: -34782

  43. "90169-89036-80538-40791-22775-82971-94442-21988"

    Returns: -342372

  44. "68854-48688-11962-48507-35705"

    Returns: -76008

  45. "97682+87716+83178+35569+64458+83571+33336+9818+964"

    Returns: 496292

  46. "73691+76698+37825+3223+83796"

    Returns: 275233

  47. "66831+83033+32706"

    Returns: 182570

  48. "98979+1586+49415+37180+31929+36145+22151+60949+73"

    Returns: 338407

  49. "90727+60972+35106+35269+36553+11356+593"

    Returns: 270576

  50. "99999+99999+99999+99999+99999+99999+99999+99999+99"

    Returns: 800091

  51. "4+9-2"

    Returns: 11

  52. "063+539-03761-069-3627-15790+075-4907+11289-06170"

    Returns: -45086

  53. "0391+0241+02860-03603"

    Returns: -111

  54. "01474-09"

    Returns: 1465

  55. "0519+296+14112-2307+0517-1898-3754-32157+516+882"

    Returns: -27104

  56. "3487-08021-2868"

    Returns: -7402

  57. "09411-562-790-11612-1117-05595"

    Returns: -10265

  58. "02876-190-16152-1371+0028-0180-09802+8066+731-1229"

    Returns: -34873

  59. "1-2+3-4+5-6+7-8+9-10+11-12+13-14+15-16+17+18+19"

    Returns: -188

  60. "10-3+1+002+3-0004+5"

    Returns: -8

  61. "555"

    Returns: 555

  62. "9999-555+666-77"

    Returns: 8701

  63. "01+012-02-045-20+03+04-05+06+07-08+90+029"

    Returns: -206

  64. "1+1-1+1-1-1-1+1+1-1-1-1-1+1+1+1+1100-1-1-2-2+3-3-4"

    Returns: -1128

  65. "1-2+3-4+5-6+7-8-9-1+2-3+4+5-6+7-8+9+1-2-3-4+5-6+7"

    Returns: -116

  66. "1-100-1"

    Returns: -100

  67. "2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2"

    Returns: -46

  68. "1000-2000+3000-4000+5000-6000"

    Returns: -19000

  69. "5"

    Returns: 5

  70. "00010+20+30+40-34-329-394+28-334+20-329-394+28-39"

    Returns: -1829

  71. "1+2-3+4-5+6-7"

    Returns: -22

  72. "1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1"

    Returns: -23

  73. "00009-00009+0009-09+09-09+09-09"

    Returns: -54

  74. "5-5+5-15"

    Returns: -20

  75. "1-2+3-4+5-6+7-80+9-10+7-9+4-1+1+1+1-3-3-3-3-3-3+1"

    Returns: -168

  76. "1+2-3+4-5+6"

    Returns: -15

  77. "1-1+1+1+1+1+1+1+1+1-1+1+1+1+1+1-1+1+1+1+1+1-1+1-1"

    Returns: -23

  78. "5-5+5+5-5+5-5+5+5-5+5-5+5+5-5+5-5+5+5-5+5-5+5+5-5"

    Returns: -115

  79. "3+1+8-1-3+2+1-7"

    Returns: -2

  80. "10+10+10-50-50+10+15+20-10+5+6+7"

    Returns: -143

  81. "2-2-2"

    Returns: -2

  82. "2-2-3-100+50+120-1000-20"

    Returns: -1293

  83. "5-50+40-55-50+40-55-50+40-55-50+40-55-50+40-55-5+7"

    Returns: -732

  84. "3-5+2+4-2+3-43+43+42-234"

    Returns: -375

  85. "4+2+3-4-56-21+222-123-344+212-111-2200"

    Returns: -3284

  86. "50-20-100"

    Returns: -70

  87. "55123+24245-23245+52213-24225+44345-52125+12325-5"

    Returns: -129115

  88. "99999-99999-99999-99999-99999-99999-99999+99999"

    Returns: -599994

  89. "1-2+3-4+5-6+7"

    Returns: -26

  90. "10+20+30+40-20+40-30+20"

    Returns: -10

  91. "1-2+3+4+5+6+7"

    Returns: -26

  92. "1+2-3+3+4+5-7-7-6-9-8+2-3-4+5+6+5+6+7-5-6-7-8-9-1"

    Returns: -123

  93. "1-2+3+4-1+3+3+2+4+3-43-56-54-1-2-3-4"

    Returns: -187

  94. "1+2-100"

    Returns: -97

  95. "00005+123-231+213+23-237"

    Returns: -576

  96. "10+20+30+40-20+40-30+20-40+50+30-20-10+60-10"

    Returns: -230

  97. "003-23-453+34-2324+23-142+232-008"

    Returns: -3236

  98. "1-1+1-1+1-1+1-1+1-1+1-1+1-1+1-1+1-1+1-1+1-1+1-1+10"

    Returns: -32


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: