-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeautifulTowers2.cpp
91 lines (81 loc) · 3.4 KB
/
BeautifulTowers2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
You are given a 0-indexed array maxHeights of n integers.
You are tasked with building n towers in the coordinate line. The ith tower is built at coordinate i and has a height of heights[i].
A configuration of towers is beautiful if the following conditions hold:
1 <= heights[i] <= maxHeights[i]
heights is a mountain array.
Array heights is a mountain if there exists an index i such that:
For all 0 < j <= i, heights[j - 1] <= heights[j]
For all i <= k < n - 1, heights[k + 1] <= heights[k]
Return the maximum possible sum of heights of a beautiful configuration of towers.
Example 1:
Input: maxHeights = [5,3,4,1,1]
Output: 13
Explanation: One beautiful configuration with a maximum sum is heights = [5,3,3,1,1]. This configuration is beautiful since:
- 1 <= heights[i] <= maxHeights[i]
- heights is a mountain of peak i = 0.
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 13.
Example 2:
Input: maxHeights = [6,5,3,9,2,7]
Output: 22
Explanation: One beautiful configuration with a maximum sum is heights = [3,3,3,9,2,2]. This configuration is beautiful since:
- 1 <= heights[i] <= maxHeights[i]
- heights is a mountain of peak i = 3.
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 22.
Example 3:
Input: maxHeights = [3,2,5,5,2,3]
Output: 18
Explanation: One beautiful configuration with a maximum sum is heights = [2,2,5,5,2,2]. This configuration is beautiful since:
- 1 <= heights[i] <= maxHeights[i]
- heights is a mountain of peak i = 2.
Note that, for this configuration, i = 3 can also be considered a peak.
It can be shown that there exists no other beautiful configuration with a sum of heights greater than 18.
Constraints:
1 <= n == maxHeights <= 105
1 <= maxHeights[i] <= 109
*/
class Solution {
public:
long long maximumSumOfHeights(vector<int>& maxHeights) {
int n=maxHeights.size();
vector<int> nextSmallerLeft(n,-1) , nextSmallerRight(n,-1);
stack<int> s1,s2;
long long i;
vector<long long> maxFromLeft(n) , maxFromRight(n);
maxFromLeft[0] = maxHeights[0];
s1.push(0);
for(i=1;i<n;i++) {
while(!s1.empty() and maxHeights[s1.top()]>maxHeights[i]) {
s1.pop();
}
if(!s1.empty()){
nextSmallerLeft[i]=s1.top();
}
s1.push(i);
if(nextSmallerLeft[i]==-1)
maxFromLeft[i]=(i+1)*maxHeights[i];
else
maxFromLeft[i]=maxFromLeft[nextSmallerLeft[i]]+(maxHeights[i]*(i-nextSmallerLeft[i]));
}
maxFromRight[n-1] = maxHeights[n-1];
s2.push(n-1);
for(i=n-2;i>=0;i--) {
while(!s2.empty() && maxHeights[s2.top()] > maxHeights[i]) {
s2.pop();
}
if( !s2.empty() ) {
nextSmallerRight[i] = s2.top();
}
s2.push(i);
if( nextSmallerRight[i] == -1 )
maxFromRight[i]=(n-i)*maxHeights[i];
else
maxFromRight[i]=maxFromRight[nextSmallerRight[i]]+(maxHeights[i]*(nextSmallerRight[i]-i));
}
long long maxSum = 0;
for(i=0;i<n;i++) {
maxSum = max(maxSum , maxFromLeft[i] + maxFromRight[i] - maxHeights[i]);
}
return maxSum;
}
};