Problem · Dynamic Programming

Efficient Deployments 🌨️

Learn this problem
HardSnowflakeINTERNOA
See Snowflake hiring insights

Problem statement

A supercomputer has n processors arranged in a row from 1 to n. The efficiency contributed by each processor depends on how many of its adjacent processors were deployed earlier.

For processor i, its efficiency is noAdjacent[i], oneAdjacent[i], or bothAdjacent[i] when zero, one, or both adjacent processors have already been deployed. Processors 1 and n have only one adjacent processor.

Find the maximum possible sum of efficiencies over all deployment orders.

Complete maxEfficiency with the following parameters:

  • int noAdjacent[n]: efficiencies when no adjacent processor was deployed earlier
  • int oneAdjacent[n]: efficiencies when one adjacent processor was deployed earlier
  • int bothAdjacent[n]: efficiencies when both adjacent processors were deployed earlier

Returns: long, the maximum possible total efficiency.

Function

maxEfficiency(noAdjacent: int[], oneAdjacent: int[], bothAdjacent: int[]) → long

Examples

Example 1

noAdjacent = [1, 2, 3, 4]oneAdjacent = [4, 4, 2, 1]bothAdjacent = [0, 1, 1, 0]return = 14

Deploy processors in the order 4 → 3 → 2 → 1.

  • Processor 4 contributes noAdjacent[4] = 4.
  • Processor 3 contributes oneAdjacent[3] = 2.
  • Processor 2 contributes oneAdjacent[2] = 4.
  • Processor 1 contributes oneAdjacent[1] = 4.

The total is 4 + 2 + 4 + 4 = 14, which is the maximum.

Example 2

noAdjacent = [2, 1, 3]oneAdjacent = [4, 2, 1]bothAdjacent = [1, 2, 3]return = 9

The order 3 → 2 → 1 contributes 3 + 2 + 4 = 9, which is optimal.

Example 3

noAdjacent = [1, 6]oneAdjacent = [2, 3]bothAdjacent = [3, 2]return = 8

Deploy processor 2 first for efficiency 6, then processor 1 for efficiency 2. The maximum total is 8.

Constraints

  • 2 ≤ n ≤ 10^5
  • 1 ≤ noAdjacent[i], oneAdjacent[i], bothAdjacent[i] ≤ 10^9

More Snowflake problems

drafts saved locally
public long maxEfficiency(int[] noAdjacent, int[] oneAdjacent, int[] bothAdjacent) {
    // write your code here
}
noAdjacent[1, 2, 3, 4]
oneAdjacent[4, 4, 2, 1]
bothAdjacent[0, 1, 1, 0]
expected14
checking account