classSolution { public: intmaxArea(vector<int>& height){ int n = height.size(); int l = 0, r = n-1; int ans = 0; while (l < r) { ans = max(ans, min(height[l], height[r]) * (r-l)); if (height[l] < height[r]) { l ++; } else { r --; } } return ans; } };
classSolution { public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> ans; sort(nums.begin(), nums.end()); int n = nums.size(); for (int i = 0; i < n-2; i ++) { if (i != 0 && nums[i] == nums[i-1]) continue; int k = n - 1; for (int j = i + 1; j < n - 1 && k > j; j ++) { if (j != i + 1 && nums[j] == nums[j-1]) continue;
while (k > j && nums[i] + nums[j] + nums[k] > 0) k --;
classSolution { public: inttrap(vector<int>& height){ int n = height.size(); vector<int> left_max(n, 0); for (int i = 1; i < n; i ++) { left_max[i] = max(left_max[i-1], height[i-1]); } vector<int> right_max(n, 0); for (int i = n-2; i >= 0; i --) { right_max[i] = max(right_max[i+1], height[i+1]); }
int ans = 0; for (int i = 0; i < n; i ++) { ans += max(min(left_max[i], right_max[i]) - height[i], 0); } return ans; } };