Skip to content

Commit

Permalink
New Problem Solution - "1840. Maximum Building Height"
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Apr 25, 2021
1 parent e836fa7 commit 2bdf2cb
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ LeetCode

| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|1840|[Maximum Building Height](https://leetcode.com/problems/maximum-building-height/) | [C++](./algorithms/cpp/maximumBuildingHeight/MaximumBuildingHeight.cpp)|Hard|
|1835|[Find XOR Sum of All Pairs Bitwise AND](https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/) | [C++](./algorithms/cpp/findXorSumOfAllPairsBitwiseAnd/FindXorSumOfAllPairsBitwiseAnd.cpp)|Hard|
|1834|[Single-Threaded CPU](https://leetcode.com/problems/single-threaded-cpu/) | [C++](./algorithms/cpp/singleThreadedCpu/SingleThreadedCpu.cpp)|Medium|
|1833|[Maximum Ice Cream Bars](https://leetcode.com/problems/maximum-ice-cream-bars/) | [C++](./algorithms/cpp/maximumIceCreamBars/MaximumIceCreamBars.cpp)|Medium|
Expand Down Expand Up @@ -541,3 +542,4 @@ LeetCode
|---| ----- | -------- | ---------- |
|1|[Search in a big sorted array](http://www.lintcode.com/en/problem/search-in-a-big-sorted-array/)|[Java](./algorithms/java/src/searchInABigSortedArray/searchInABigSortedArray.java)|Medium|
|2|[Search Range in Binary Search Tree](http://www.lintcode.com/en/problem/search-range-in-binary-search-tree/) | [Java](./algorithms/java/src/searchRangeInBinarySearchTree/searchRangeInBinarySearchTree.java)|Medium|

187 changes: 187 additions & 0 deletions algorithms/cpp/maximumBuildingHeight/MaximumBuildingHeight.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Source : https://leetcode.com/problems/maximum-building-height/
// Author : Hao Chen
// Date : 2021-04-25

/*****************************************************************************************************
*
* You want to build n new buildings in a city. The new buildings will be built in a line and are
* labeled from 1 to n.
*
* However, there are city restrictions on the heights of the new buildings:
*
* The height of each building must be a non-negative integer.
* The height of the first building must be 0.
* The height difference between any two adjacent buildings cannot exceed 1.
*
* Additionally, there are city restrictions on the maximum height of specific buildings. These
* restrictions are given as a 2D integer array restrictions where restrictions[i] = [idi, maxHeighti]
* indicates that building idi must have a height less than or equal to maxHeighti.
*
* It is guaranteed that each building will appear at most once in restrictions, and building 1 will
* not be in restrictions.
*
* Return the maximum possible height of the tallest building.
*
* Example 1:
*
* Input: n = 5, restrictions = [[2,1],[4,1]]
* Output: 2
* Explanation: The green area in the image indicates the maximum allowed height for each building.
* We can build the buildings with heights [0,1,2,1,2], and the tallest building has a height of 2.
*
* Example 2:
*
* Input: n = 6, restrictions = []
* Output: 5
* Explanation: The green area in the image indicates the maximum allowed height for each building.
* We can build the buildings with heights [0,1,2,3,4,5], and the tallest building has a height of 5.
*
* Example 3:
*
* Input: n = 10, restrictions = [[5,3],[2,5],[7,4],[10,3]]
* Output: 5
* Explanation: The green area in the image indicates the maximum allowed height for each building.
* We can build the buildings with heights [0,1,2,3,3,4,4,5,4,3], and the tallest building has a
* height of 5.
*
* Constraints:
*
* 2 <= n <= 10^9
* 0 <= restrictions.length <= min(n - 1, 10^5)
* 2 <= idi <= n
* idi is unique.
* 0 <= maxHeighti <= 10^9
******************************************************************************************************/

/*
At first , it's not difficult to work out the max height between two buildings.
**Case study**
considering the following restractions:
1) Building #1 `max-height = 1`, Building #5 `max-height = 1`
then we can have the building height list - `[1,2,3,2,1] `
2) Building #1 `max-height = 3`, Building #5 `max-height = 1`
then we can have the building height list - `[3,4,3,2,1]`
3) Building #1 `max-height = 1`, Building #5 `max-height = 9`
then we can have the building height list - `[1,2,3,4,5]`
So, we can figure out the following rules :
1) if two restraction has same limited height, suppose we have `[n ......... n]`,
then we can have the building height list `[n, n+1, n+2, ... n+m-1, n+m, n+m-1 ..., n+2, n+1, n]`
So, **`m = width /2`** - the `width` is the number of buildings.
2) if two restraction has different limited height, suppose we have `[n ...... n+x]`
then we still can have the building height list like 1) - we just add some buildings behind `[n .... n+x, (n+x-1... n) ]`
So, **`m = (width+x)/2`** - we need to extend x buildings
3) if there hasn't enough buildings between two restractions. then, the max height we can make is **`width`**. For examples:
- Building#1 max-height = 2, building#3 max-height = 5 : then, we only can make `[2,3,4]`
- Building#1 max-height = 2, building#2 max-height = 9 : then, we only can make `[2,3]`
So, we can have the following source code caluation the max height between two restractions.
```
int getMaxHeight(vector<int>& left, vector<int>& right) {
int width = right[0] - left[0];
int height_delta = abs(right[1] - left[1]);
int min_height = min (left[1], right[1]);
//if the `width` is enough to have `height_delta`
if (width >= height_delta) return min_height + (width + height_delta) / 2;
// if the `width` is not enought have `height_delta`
// then, the `width` is the max height we can make
int max_height = min_height + width;
return max_height;
}
```
BUT, we still have a case need to deal with, considering we have the following restractions:
`[1,1], [2,2] ,[3,3], [4,0]`
we can process them couple by couple.
- `[1,1], [2,2]` : max-height = 2
- `[2,2] ,[3,3]` : max-height = 3
- `[3,3], [4,0]` : max-height = 1
for the last couple restractions, we can see the building#3 max-height is 1, so we have go backwards to recaluate the building#2 and building#1.
- `[3,1], [4,0]` : max-height = 1
- `[2,2] ,[3,1]` : max-height = 2
- `[1,1], [2,2]` : max-height = 2
So, the correct answer of max height is `2`
finally, we have the whole source code with debug code inside.
*/

class Solution {
private:
void print(vector<vector<int>>& vv){
cout << "[" ;
for(int i = 0; i < vv.size()-1; i++) {
cout << "[" <<vv[i][0] << "," << vv[i][1] << "],";
}
int i = vv.size() - 1;
cout << "[" << vv[i][0] << "," << vv[i][1] << "]]" << endl;
}

public:
int getMaxHeight(vector<int>& left, vector<int>& right) {

int width = right[0] - left[0];
int height_delta = abs(right[1] - left[1]);
int min_height = min (left[1], right[1]);

//if the `width` is enough to have `height_delta`
if (width >= height_delta) return min_height + (width + height_delta) / 2;

// if the `width` is not enought have `height_delta`
// then, the `width` is the max height we can make
int max_height = min_height + width;

// if the restriction is higher then make it to right limitation.
left[1] = min (left[1], max_height);
right[1] = min (right[1], max_height);

return max_height;
}

int maxBuilding(int n, vector<vector<int>>& restrictions) {
restrictions.push_back({1,0});
restrictions.push_back({n, n-1});
sort(restrictions.begin(), restrictions.end());

//print(restrictions);

for(int i=0; i<restrictions.size()-1; i++){
int height = getMaxHeight(restrictions[i], restrictions[i+1]);
//cout << "[" << restrictions[i][0] << "," << restrictions[i][1]<< "] - "
// << "[" << restrictions[i+1][0] << "," << restrictions[i+1][1]<< "] = "
// << height << endl;
}
cout << endl;
int maxHeight = 0;
for(int i= restrictions.size()-1; i>0; i--){
int height = getMaxHeight(restrictions[i-1], restrictions[i]);
//cout << "[" << restrictions[i-1][0] << "," << restrictions[i-1][1]<< "] - "
// << "[" << restrictions[i][0] << "," << restrictions[i][1]<< "] = "
// << height << endl;
maxHeight = max(maxHeight, height);

}
// cout << endl;
return maxHeight;
}
};

0 comments on commit 2bdf2cb

Please sign in to comment.