LeetCode 1840: Maximum Building Height Solution
Master LeetCode problem 1840 (Maximum Building Height), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
Problem Explanation
Explanation:
To solve this problem, we can use binary search to find the maximum possible height of the tallest building. We need to consider the restrictions and compute the maximum height that can be achieved. The main idea is to iterate through the restrictions and calculate the maximum allowed height at each position based on the adjacent restrictions.
- Sort the restrictions by their position.
- Initialize two arrays
leftandrightto store the maximum height achievable from the left and right side respectively. - For each restriction, calculate the maximum height that can be achieved at that position based on the adjacent restrictions.
- The maximum possible height at a position
iis the minimum of the heights calculated from the left and right side. - Use binary search to find the maximum possible height of the tallest building.
:
Solution Code
class Solution {
public int maxBuilding(int n, int[][] restrictions) {
Arrays.sort(restrictions, (a, b) -> a[0] - b[0]);
int m = restrictions.length;
int[] left = new int[m + 1];
int[] right = new int[m + 1];
left[0] = 0;
right[0] = 0;
for (int i = 0; i < m; i++) {
left[i + 1] = Math.min(restrictions[i][0] - 1, restrictions[i][1]);
}
for (int i = m - 1; i >= 0; i--) {
right[i] = Math.min(restrictions[i][0] - 1, restrictions[i][1]);
}
for (int i = 1; i <= m; i++) {
left[i] = Math.min(left[i], left[i - 1] + restrictions[i - 1][0] - restrictions[i - 2][0]);
}
for (int i = 1; i < m; i++) {
right[i] = Math.min(right[i], right[i + 1] + restrictions[i][0] - restrictions[i - 1][0]);
}
int maxHeight = 0;
for (int i = 0; i < m; i++) {
int leftHeight = left[i];
int rightHeight = right[i];
int distance = restrictions[i][0] - restrictions[i - 1][0];
int totalHeight = restrictions[i][1] + Math.max(leftHeight, rightHeight) + (distance - Math.abs(leftHeight - rightHeight)) / 2;
maxHeight = Math.max(maxHeight, totalHeight);
}
int lastPosition = restrictions[m - 1][0];
int lastHeight = restrictions[m - 1][1];
maxHeight = Math.max(maxHeight, lastHeight + n - lastPosition);
return maxHeight;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1840 (Maximum Building Height)?
This page provides optimized solutions for LeetCode problem 1840 (Maximum Building Height) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1840 (Maximum Building Height)?
The time complexity for LeetCode 1840 (Maximum Building Height) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1840 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1840 in Java, C++, or Python.