Loading...
You are given three 0-indexed integer arrays zeroBefore, oneBefore, and twoBefore, all of length n. They describe n positions arranged in a row and numbered 0 to n - 1: position i is adjacent to positions i - 1 and i + 1, so the first and last positions each have a single adjacent position.
The positions are activated one at a time, each exactly once, in an order you choose. When position i is activated, it scores points according to how many of its adjacent positions were activated earlier:
zeroBefore[i] points if neither adjacent position has been activated yet,oneBefore[i] points if exactly one of them has been activated,twoBefore[i] points if both of them have been activated.Return the maximum possible total score over all activation orders.
Note that an end position has only one adjacent position, so it never scores its twoBefore value, and when n = 1 the single position always scores zeroBefore[0].
Input: zeroBefore = [1,2,3,4], oneBefore = [4,4,2,1], twoBefore = [1,1,1,1]
Output: 14
Explanation: Activate the positions from right to left: 3, 2, 1, 0.
- Position 3 goes first: no adjacent position is active yet, so it scores zeroBefore[3] = 4.
- Position 2: its neighbor 3 is already active, so it scores oneBefore[2] = 2.
- Position 1: its neighbor 2 is already active, so it scores oneBefore[1] = 4.
- Position 0: its neighbor 1 is already active, so it scores oneBefore[0] = 4.
Total 4 + 2 + 4 + 4 = 14, and no order does better.
Input: zeroBefore = [3,4], oneBefore = [10,2], twoBefore = [7,7]
Output: 14
Explanation: Activate position 1 first (scores zeroBefore[1] = 4), then position 0
(its only neighbor is active, so it scores oneBefore[0] = 10), for a total of 14.
The other order scores 3 + 2 = 5. With two positions, neither has two adjacent
positions, so twoBefore never applies.
Input: zeroBefore = [5], oneBefore = [9], twoBefore = [9]
Output: 5
Explanation: A single position has no adjacent positions, so it always scores
zeroBefore[0] = 5.
n ≤105, where n = zeroBefore.length = oneBefore.length = twoBefore.lengthzeroBefore[i], oneBefore[i], twoBefore[i] ≤109Click "Run" to test with sample cases or "Submit" to run all tests.