Problem · Binary Search

Server Upgrade Planning

Learn this problem
MediumRipplingINTERNOA

Problem statement

Two servers require t1 and t2 seconds of upgrade work. During each numbered second, at most one server can undergo an upgrade.

The first server receives requests at every multiple of req1 and cannot be upgraded during those seconds. The second server receives requests at every multiple of req2 and cannot be upgraded during those seconds.

There may be seconds during which neither server is upgraded. Return the minimum total number of seconds needed to complete both upgrades.

Function

getMinUpgradationTime(req1: int, t1: int, req2: int, t2: int) → long

Examples

Example 1

req1 = 2t1 = 3req2 = 3t2 = 1return = 5

Upgrade the first server during seconds 1, 3, and 5. Upgrade the second server during second 2. Each chosen second avoids that server's request multiples, so both upgrades finish after 5 seconds.

Example 2

req1 = 2t1 = 1req2 = 2t2 = 3return = 7

Neither server can be upgraded during an even-numbered second. Four one-second upgrade slots are needed, so the earliest usable slots are 1, 3, 5, and 7.

Constraints

  • 2 <= req1, req2 <= 3 * 10^4
  • 1 <= t1, t2 <= 10^9

More Rippling problems

drafts saved locally
public long getMinUpgradationTime(int req1, int t1, int req2, int t2) {
  // write your code here
}
req12
t13
req23
t21
expected5
checking account