Problem · Array

Find Max Health Sum

Learn this problem
EasyAmazonOA
See Amazon hiring insights

Problem statement

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

findMaxHealthSum(health: int[], serverType: int[], m: int) → long

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

Examples

Example 1

health = [4, 5, 5, 6]serverType = [1, 2, 1, 2]m = 1return = 11
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

  • 1 ≤ m ≤ n ≤ 10^5
  • 1 ≤ health[i] ≤ 10^9
  • 1 ≤ serverType[i] ≤ n

More Amazon problems

drafts saved locally
public long findMaxHealthSum(int[] health, int[] serverType, int m) {
  // write your code here
}
health[4, 5, 5, 6]
serverType[1, 2, 1, 2]
m1
expected11
checking account