classSolution { public: vector<int> twoSum(vector<int>& numbers, int target){ int l = 0; int r = numbers.size() - 1; while(l < r){ int num = numbers[l] + numbers[r]; if(num == target){ break; }elseif(num < target){ l ++; }else{ r --; } } return {l + 1, r + 1}; } };
classSolution { public: intmaxArea(vector<int>& height){ int n = height.size(); int l = 0; int r = n - 1; int area = 0; int ans = 0; while(l < r){ area = min(height[l], height[r]) * (r - l); ans = max(area, ans); if(height[r] < height[l]){ r --; }else{ l ++; } } return ans; } };
classSolution { public: inttrap(vector<int>& height){ int n = height.size(); int l = 0; int r = n - 1; int pre_max = height[0]; int suf_max = height[r]; int ans = 0; while(l < r){ if(pre_max < suf_max){ ans += min(pre_max, suf_max) - height[l]; l ++; pre_max = max(height[l], pre_max); }else{ ans += min(pre_max, suf_max) - height[r]; r --; suf_max = max(height[r], suf_max); } } return ans;