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:
- First: Compute all multiplications from left to right
- 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
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
โ Quadratic Growth
Space Complexity
O(n^2 * m)
Memoization table stores O(n^2) subproblems, each with up to m results
โ 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)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code