leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、

11. Container With Most Waterhtml

https://www.cnblogs.com/grandyang/p/4455109.html數組

用雙指針向中間滑動,較小的高度就做爲當前狀況的高度,而後循環找容量的最大值。app

無論兩個指針中間有多少高度的柱子,只管兩頭,由於兩頭的才決定最大容量。spa

class Solution {
public:
    int maxArea(vector<int>& height) {
        if(height.empty())
            return 0;
        int res = 0;
        int begin = 0,end = height.size() - 1;
        while(begin < end){
            int h = min(height[begin],height[end]);
            res = max(res,(end - begin) * h);
            h == height[begin] ? begin++ : end --;
        }
        return res;
    }
};

 

42. Trapping Rain Water指針

https://www.cnblogs.com/grandyang/p/4402392.htmlcode

本題與11. Container With Most Water不一樣,11. Container With Most Water是求兩個柱子能裝的最多水量,這個題是求的全部柱子能裝的水量。htm

依舊能夠使用雙指針,一個begin指向第一個柱子,一個end指向最後一個柱子。而後至關於維護一個遞減的隊列,但又不是徹底遞減,只是說後面遍歷到的應該比這個開始的位置少。一旦出現比這個開始位置大,就要從新更新做爲比較的對象。對象

注意,選擇對比的是兩個柱子中較短的那根柱子。blog

class Solution {
public:
    int trap(vector<int>& height) {

        int begin = 0,end = height.size() - 1;
        int res = 0;
        while(begin < end){
            int h = min(height[begin],height[end]);
            if(h == height[begin]){
                int tmp = height[begin];
                begin++;
                while(begin < end && height[begin] < tmp){
                    res += tmp - height[begin];
                    begin++;
                }
            }
            else{
                int tmp = height[end];
                end--;
                while(begin < end && height[end] < tmp){
                    res += tmp - height[end];
                    end--;
                }
            }
        }
        return res;
    }
};

 

 

 

 

238. Product of Array Except Self隊列

https://www.cnblogs.com/grandyang/p/4650187.html

總體分紅左右兩個數組進行相乘,這種方式不用兩個數組進行存儲。

從左向右能夠用res[i]就代替前面的乘積,但從右向左就不行了,這個是後res[i]已是全部的在左側前方的乘積和,咱們還必須計算右側的乘積和,這個時候用一個變量right來累乘就行了。

其實從左向右也能夠用一個變量來累乘。

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        vector<int> res(nums.size(),1);
        for(int i = 1;i < nums.size();i++){
            res[i] = res[i-1] * nums[i-1];
        }
        int right = 1;
        for(int i = nums.size() - 1;i >= 0;i--){
            res[i] *= right;
            right *= nums[i];
        }
        return res;
    }
};

 

 

407. Trapping Rain Water II

https://www.cnblogs.com/grandyang/p/5928987.html

class Solution {
public:
    int trapRainWater(vector<vector<int>>& heightMap) {
        if(heightMap.empty())
            return 0;
        int m = heightMap.size(),n = heightMap[0].size();
        int res = 0,max_height = INT_MIN;
        vector<vector<bool>> visited(m,vector<bool>(n,false));
        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                if(i == 0 || i == m - 1 || j == 0 || j == n - 1){
                    int index = i * n + j;
                    q.push(make_pair(heightMap[i][j],index));
                    visited[i][j] = true;
                }
            }
        }
        while(!q.empty()){
            int height = q.top().first;
            int x = q.top().second / n;
            int y = q.top().second % n;
            q.pop();
            max_height = max(max_height,height);
            for(auto dir : dirs){
                int new_x = x + dir[0];
                int new_y = y + dir[1];
                if(new_x < 0 || new_x >= m || new_y < 0 || new_y >= n || visited[new_x][new_y] == true)
                    continue;
                visited[new_x][new_y] = true;
                if(heightMap[new_x][new_y] < max_height)
                    res += max_height - heightMap[new_x][new_y];
                int new_index = new_x * n + new_y;
                q.push(make_pair(heightMap[new_x][new_y],new_index));
            }
        }
        return res;
    }
private:
    vector<vector<int>> dirs{{-1,0},{0,-1},{1,0},{0,1}};
};
相關文章
相關標籤/搜索