The Score of Students Solving Math Expression - Problem

You're a teacher grading math homework! ๐Ÿ“ You've given your elementary school students a simple arithmetic expression containing only single digits (0-9), addition (+), and multiplication (*) symbols.

Here's the catch: you taught them a specific order of operations:

  1. First: Compute all multiplications from left to right
  2. Then: Compute all additions from left to right

For example, with expression "3+5*2":

  • Correct answer: 3 + (5*2) = 3 + 10 = 13
  • Wrong order: (3+5) * 2 = 8 * 2 = 16

Now you need to grade the students' answers based on these rules:

  • 5 points: Correct answer using proper order of operations
  • 2 points: Wrong answer BUT could be obtained by applying operations in some different (but mathematically valid) order
  • 0 points: Completely wrong answer that can't be explained by any order of operations

Goal: Return the total points earned by all students.

Input & Output

example_1.py โ€” Basic Expression
$ Input: s = "7+3*1*2", answers = [20, 13, 42]
โ€บ Output: 7
๐Ÿ’ก Note: Correct answer: 7+(3*1*2) = 7+6 = 13. Student with answer 13 gets 5 points. Answer 20 could come from (7+3)*1*2 = 20, so 2 points. Answer 42 is impossible with any valid parenthesization, so 0 points. Total: 5+2+0 = 7
example_2.py โ€” Simple Addition
$ Input: s = "3+5*2", answers = [13, 0, 10, 13, 13, 16, 16]
โ€บ Output: 19
๐Ÿ’ก Note: Correct answer: 3+(5*2) = 13. Three students got 13 (5ร—3=15 points). Two students got 16 from (3+5)*2 (2ร—2=4 points). Answers 0 and 10 are impossible (0ร—2=0 points). Total: 15+4+0 = 19
example_3.py โ€” Single Number
$ Input: s = "6+0*1", answers = [6, 1, 0]
โ€บ Output: 7
๐Ÿ’ก Note: Correct answer: 6+(0*1) = 6+0 = 6. Student with answer 6 gets 5 points. Answer 0 could come from (6+0)*1 = 0, so 2 points. Answer 1 is impossible, so 0 points. Total: 5+2+0 = 7

Visualization

Tap to expand
Student Answer Grading SystemExpression: "3 + 5 * 2"Taught Rule: Multiply first, then AddCorrect Answer (5 points)3 + (5 ร— 2)= 3 + 10 = 13Alternative Valid (2 points)(3 + 5) ร— 2= 8 ร— 2 = 16Impossible Results (0 points)Any other numberslike 15, 20, 0, etc.Grading ExampleStudent Answers: [13, 16, 15, 13, 0]โ€ข Answer 13: Correct โ†’ 5 points (ร—2 students = 10 points)โ€ข Answer 16: Valid alternative โ†’ 2 pointsโ€ข Answers 15, 0: Impossible โ†’ 0 points eachTotal Score: 10 + 2 + 0 + 0 = 12 points
Understanding the Visualization
1
Parse Expression
Break down '3+5*2' into numbers and operators
2
Find Correct Answer
Apply taught rules: multiply first (5*2=10), then add (3+10=13)
3
Generate All Possibilities
Use DP to find alternative valid results: (3+5)*2=16
4
Grade Students
Award 5 points for correct (13), 2 points for valid alternative (16), 0 for impossible
Key Takeaway
๐ŸŽฏ Key Insight: Use interval dynamic programming to efficiently compute all mathematically valid results, then award partial credit for creative but correct approaches.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n^3 * m)

O(n^2) subproblems, each taking O(n*m) time where m is the average number of results per subproblem

n
2n
โš  Quadratic Growth
Space Complexity
O(n^2 * m)

Memoization table stores O(n^2) subproblems, each with up to m results

n
2n
โš  Quadratic Space

Constraints

  • 3 โ‰ค s.length โ‰ค 31
  • s consists of digits and operators '+' and '*' only
  • All the integer operands in the expression are in the range [0, 9]
  • 1 โ‰ค answers.length โ‰ค 104
  • 0 โ‰ค answers[i] โ‰ค 1000
  • The expression is guaranteed to be valid (alternating numbers and operators)
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen