Advertisement

The Score of Students Solving Math Expression - LeetCode 2019 Solution

The Score of Students Solving Math Expression - Complete Solution Guide

The Score of Students Solving Math Expression is LeetCode problem 2019, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given a string s that contains digits 0-9 , addition symbols '+' , and multiplication symbols '*' only , representing a valid math expression of single digit numbers (e.g., 3+5*2 ). This expression was given to n elementary school students. The students were instructed to get the answer of the expression by following this order of operations : Compute multiplication , reading from left to right ; Then, Compute addition , reading from left to right . You are given an integer array answers

Detailed Explanation

The problem asks us to evaluate the scores of students who solved a math expression. The expression contains single-digit numbers, addition '+', and multiplication '*' operations. Students were asked to calculate the result following the standard order of operations (multiplication before addition, from left to right). We're given the expression string `s` and an array `answers` containing students' submissions. The scoring is as follows: * 5 points if the answer matches the correct result of the expression. * 2 points if the answer can be obtained by performing operations in a different order, but with correct arithmetic. This means the student arrived at a correct numerical value through a different order of operations. * 0 points otherwise. The goal is to calculate the total score of all students, considering their answers and the correct result, along with all possible (incorrect order) results.

Solution Approach

The solution uses Dynamic Programming (DP) to generate all possible answers for the math expression with various operator precedence. Then, it calculates the correct answer following the proper order of operations. Finally, it iterates through the student's answers, assigning points based on whether the answer matches the correct result or is present in the set of possible, albeit incorrect-order, results.

Step-by-Step Algorithm

  1. Step 1: Parse the input string `s` to separate numbers and operators.
  2. Step 2: Implement a recursive DP function `dp(i, j)` that returns a set of all possible results for the substring `s[i:j+1]`. The base case is when i == j, in which case it returns a set containing only nums[i]. The DP uses memoization to avoid redundant calculations.
  3. Step 3: Inside `dp(i, j)`, iterate from `k = i` to `j - 1` to split the expression at each operator. Recursively compute the possible results for the left and right subexpressions using `dp(i, k)` and `dp(k + 1, j)`. Combine the results from both subexpressions based on the operator `ops[k]` to create new possible results, adding them to the set. Only consider results <= 1000.
  4. Step 4: Evaluate the expression correctly using standard order of operations (multiplication then addition). This typically uses an approach similar to expression parsing or a stack-based evaluation.
  5. Step 5: Iterate through the `answers` array. For each answer, add 5 to the total score if it's equal to the correct answer. Otherwise, if the answer is present in the set of possible answers (calculated using DP), add 2 to the total score.
  6. Step 6: Return the total score.

Key Insights

  • Insight 1: Dynamic Programming is essential to generate all possible results by evaluating the expression with different operator precedence, while respecting parentheses (even if implicit). The constraint on the maximum result (1000) helps to prune computations.
  • Insight 2: Correctly evaluating the original expression based on the standard order of operations (multiplication before addition) is crucial.
  • Insight 3: Using a set to store possible answers avoids duplicate computations and improves efficiency when searching for the existence of a student's answer within the set of possible answers.

Complexity Analysis

Time Complexity: O(n^3 * 1000)

Space Complexity: O(n^2 * 1000)

Topics

This problem involves: Array, Hash Table, Math, String, Dynamic Programming, Stack, Memoization.

Companies

Asked at: Flipkart.