Similar Problems
Similar Problems not available
The Score Of Students Solving Math Expression - Leetcode Solution
Companies:
LeetCode: The Score Of Students Solving Math Expression Leetcode Solution
Difficulty: Hard
Topics: dynamic-programming math string stack array
Problem Statement:
You are given a string expression representing a list of numbers and operators. The expression is well-formed, and all the numbers are non-negative integers. The expression is also guaranteed to be free of any white spaces. The operators are defined using the following rules:
"+" is used for addition. "-" is used for subtraction. "*" is used for multiplication. "/" is used for division. For simplicity, we will only allow integer division operations.
You have to calculate the score of each student who solves the expression in the following way:
If the student gets an operator of '+' or '-', their score will be the minimum of the absolute differences of the adjacent numbers in the expression. If the student gets an operator of '*' or '/', their score will be the sum of the adjacent number in the expression.
Return an array of integers, where each element i is the score for each student i who solved the expression. If the expression has only one number, then the score is 0, and the answer is returned as [-1].
Example:
Input: expression = "5+3-2" Output: [1,3,0] Explanation: The input string represent expression is "5+3-2". The adjacent numbers are [5,3], [3,-2] and scores are 2-5=3, 3-(-2)=5 and 2+(-2)=0. The score for each student is [1,3,0].
Solution:
We can solve this problem using a stack. We will iterate over the expression from left to right and implement the following steps:
If we encounter a number, we will push it onto the stack. If we encounter an operator, we will pop the top two elements from the stack and calculate the score according to the operator. a. If the operator is '+' or '-', we will calculate the minimum of the absolute differences of the adjacent numbers in the expression. b. If the operator is '*' or '/', we will calculate the sum of the adjacent numbers in the expression. We will push the score onto the stack. At the end of the iteration, we will return the stack.
If the expression has only one number, we will return [-1].
Code:
def scoreOfStudents(expression: str, answers: List[int]) -> int: stack = [] i = 0 while i < len(expression): if expression[i].isdigit(): j = i while j < len(expression) and expression[j].isdigit(): j += 1 num = int(expression[i:j]) stack.append(num) i = j elif expression[i] == '+' or expression[i] == '-': op = expression[i] i += 1 j = i while j < len(expression) and expression[j].isdigit(): j += 1 num2 = int(expression[i:j]) num1 = stack.pop() if op == '+': score = abs(num1 - num2) else: score = abs(num1 + num2) stack.append(score) i = j else: op = expression[i] i += 1 j = i while j < len(expression) and expression[j].isdigit(): j += 1 num2 = int(expression[i:j]) num1 = stack.pop() if op == '*': score = num1 + num2 else: score = num1 + num2 stack.append(score) i = j if len(stack) == 1: return [-1] else: correct_score = stack[-1] count = 0 for score in answers: if score == correct_score: count += 5 elif abs(score - correct_score) <= 1: count += 2 return count
Time Complexity:
The time complexity of the solution is O(n), where n is the length of the expression. We only iterate over the string once. The time complexity of pop and append operations on the stack is O(1), so we do not need to consider it in the analysis.
Space Complexity:
The space complexity of the solution is O(n), where n is the length of the expression. We store the numbers and scores on the stack, which can have up to n elements.
The Score Of Students Solving Math Expression Solution Code
1