FastPrepFastPrep
Problem Brief

Find Max Health Sum

OA

Amazon is building a new data center with n servers of different types. The health and type of each server are represented in the arrays health and serverType. The developers need to build a server facility with a maximum of m distinct types of servers and the sum of their health should be maximized.

Given arrays health and serverType, find the maximum sum of the health for up to m types of servers.

Function Description

Complete the function findMaxHealthSum in the editor below.

findMaxHealthSum has the following parameters:

  1. 1. int health[n]: the health of each server
  2. 2. int serverType[n]: the type of each server
  3. 3. m: the maximum number of distinct types

Returns

long int: the maximum sum of health of the selected servers

1Example 1

Input
health = [4, 5, 5, 6], serverType = [1, 2, 1, 2], m = 1
Output
11
Explanation
Example 1 illustration
Since m = 1, all selected servers must be the same type. The better option is to select type 2 servers. Return 11.

Constraints

Limits and guarantees your solution can rely on.

  • 1 ≤ m ≤ n ≤ 10^5
  • 1 ≤ health[i] ≤ 10^9
  • 1 ≤ serverType[i] ≤ n
public long findMaxHealthSum(int[] health, int[] serverType, int m) {
  // write your code here
}
Input

health

[4, 5, 5, 6]

serverType

[1, 2, 1, 2]

m

1

Output

11

Sign in to submit your solution.