arr leftminarray(const arr& hist) { arr out(hist.size()); stack<int> S; for(int i=0;i<hist.size(); ++i) { while( !S.empty() && hist[S.top()] >= hist[i]) S.pop(); if(!S.empty())out[i] = S.top(); else out[i] = -1; S.push(i); } return out; }
arr rightminarray(const arr& hist) { arr out(hist.size()); stack<int> S; for(int i=hist.size()-1 ;i>=0; --i) { while( !S.empty() && hist[S.top()] >= hist[i]) S.pop(); if(!S.empty())out[i] = S.top(); else out[i] = hist.size(); S.push(i); } return out; }
size_t lookup_by_name(arr &names, const char *name) { for (size_t idx = 0; idx < names.size(); ++idx) { if (strcmp(name, names[idx]) == 0) { return idx; } } return 0; }
//Given an array and a value, remove all instances of that value in place and return the new length. //The order of elements can be changed.It doesn't matter what you leave beyond the new length(超出新长度的元素可以任意). int Array::removeElement(arr& nums, int val) { int i, j; for (i = j = nums.size() - 1; i >= 0; i--) { if (nums[i] == val && i != j--) nums[i] = nums[j + 1]; } return j + 1; }
int max_area(const arr& hist) { int max_area = INT_MIN; arr left = leftminarray(hist); arr right = rightminarray(hist); for(int i=0; i < hist.size(); ++i) { int area = (right[i]-left[i]-1 )*hist[i]; if( area > max_area) max_area = area; } return max_area; }