Problem · Array

Escape Game - Maximize Score

Learn this problem
MediumMicrosoftOA
See Microsoft hiring insights

Problem statement

Duplicate notice: This page is a duplicate of Maximum Escape Game Score. Please ignore this page and use the canonical version.

Given an integer array elements, in each round you may choose a value v, delete all elements equal to v from the array and add them to your score, while also deleting all elements equal to v + 1 or v - 1. Those neighboring values earn no points.

Repeat until the array is empty. Return the maximum score you can obtain.

Function

maxScore(elements: int[]) → int

Examples

Example 1

elements = [5, 6, 6, 4, 11]return = 27

First pick 11, scoring 11 points; there are no 10s or 12s to delete, leaving [5, 6, 6, 4].

Then pick 6, scoring 12 points, while deleting the 5 and 7. The 5 is removed, leaving [4].

Finally pick 4, scoring 4 points. Total: 27.

More Microsoft problems

drafts saved locally
public int maxScore(int[] elements) {
  // write your code here
}
elements[5, 6, 6, 4, 11]
expected27
checking account