Tap here to switch tabs
Problem
Submissions

Problem: Maximum Building Height

med
30 min
Try to solve the Maximum Building Height problem.

Statement

You are tasked with planning the construction of nn buildings in a line, indexed from 11 to nn.

The heights of the buildings must satisfy the following conditions:

  • The height of building 11 must be 00.

  • The absolute difference in height between any two adjacent buildings cannot exceed 11. That is, for every building ii where 1≤i<n1 \leq i < n, the condition ∣hi−hi+1∣≤1|h_i - h_{i+1}| \leq 1 must hold, where hih_i represents the height of building ii.

  • There is a 22D array restrictions, where each restrictions[i] =[idi,maxHeighti]= [id_i, maxHeight_i] specifies that building idiid_i can have a maximum height of maxHeightimaxHeight_i.

Return the maximum possible height among all the buildings that can be constructed.

Constraints:

  • 2≤2 \leq n ≤109\leq 10^9

  • 0≤0 \leq restrictions.length ≤min⁡(n−1,105)\leq \min(n - 1, 10^5)

  • 2≤idi≤2 \leq id_i \leq n

  • All idiid_i are unique.

  • 0≤maxHeighti≤1090 \leq maxHeight_i \leq 10^9

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximum Building Height

med
30 min
Try to solve the Maximum Building Height problem.

Statement

You are tasked with planning the construction of nn buildings in a line, indexed from 11 to nn.

The heights of the buildings must satisfy the following conditions:

  • The height of building 11 must be 00.

  • The absolute difference in height between any two adjacent buildings cannot exceed 11. That is, for every building ii where 1≤i<n1 \leq i < n, the condition ∣hi−hi+1∣≤1|h_i - h_{i+1}| \leq 1 must hold, where hih_i represents the height of building ii.

  • There is a 22D array restrictions, where each restrictions[i] =[idi,maxHeighti]= [id_i, maxHeight_i] specifies that building idiid_i can have a maximum height of maxHeightimaxHeight_i.

Return the maximum possible height among all the buildings that can be constructed.

Constraints:

  • 2≤2 \leq n ≤109\leq 10^9

  • 0≤0 \leq restrictions.length ≤min⁡(n−1,105)\leq \min(n - 1, 10^5)

  • 2≤idi≤2 \leq id_i \leq n

  • All idiid_i are unique.

  • 0≤maxHeighti≤1090 \leq maxHeight_i \leq 10^9