盛最多水的容器 - 中等难度

*************

C++

topic: 11. 盛最多水的容器 - 力扣(LeetCode)

*************

Inspect the topic:

Think about the familar one, and there is a harder one: 动态规划 - 直方图的水量 - 困难-优快云博客icon-default.png?t=O83Ahttps://blog.youkuaiyun.com/ElseWhereR/article/details/144052319?spm=1001.2014.3001.5501

This is a harder one. To solve the hard one, caculate each position storing and summation. For each position[i], look for the highest one on left and the highest one on right, make position[i] - min{highest-left, highest-right}, that is the storage of current pisition. However, this method doesnot work here.

To find the maxmum, th first spark is that find the toppest two dp[i] dp[j], and return = (j - i) * min{dp[i], dp[j]}. Thikk over then find it doesnot work. As is follow

 

SORT donot work here cuz it need not to be ordered.

UNORDERED_SET donot work either.

Aesthetic of violent always works. Here is the thought:

  1. identify a dp[i], presents the water capacity in position i;
  2. find the toppest one on left, name it left_max;
  3. find the toppest one on right, named it right_max;
  4. cacalate water capacity dp[i] = (j - i) * min{left_max, right_max};
  5. find the max dp[i];
class Solution {
public:
    int maxArea(vector<int>& height) {
        int n = height.size();
        if (n < 2) return 0;
        
        int max_area = 0;
        
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                int current_height = min(height[i], height[j]);
                int current_width = j - i;
                int current_area = current_height * current_width;
                if (current_area > max_area) {
                    max_area = current_area;
                }
            }
        }
        
        return max_area;
    }
};

Overtime, suck my dxxk.

Bubble sort, just zone out, seems to be fun, but donnot work here.

 I have two pointers:

There is no need for dp[i], just int max_area = 0, and interation it.

For me myself, I love interation so much. During my growth, interation becomes more and more important. With system 1.0 duiring my first 10 years, left-behind kid, starve. I remember the moring, I stood in front of the school gate, feel so cold because I had only a thin coat the whole winter where the temperate is -10℃. Toes hurt, because I have only broken shoes.

I hate cold. 

In the senond ten years, I made some friends at middle school, some are my teachers and some are my peer, they changed me. In the other word, I learned how to be a kid from them. Even though almost I lost touch with them, but they never left me because the things I learned from them flews in my blood. Suffering deserves nothing, it is just suffering. It is you, comes over pains again and again, count.

Life will find its way.

Back to the topic, move the pointers:

the very beginning is initilize all the elements.

class Solution {
public:
    int maxArea(vector<int>& height) {
        
        int n = height.size();
        int i = 0; // left pointer
        int j = n - 1; // right pointer
        int max_area = 0; // the return


    }
};

then enter the a loop,

class Solution {
public:
    int maxArea(vector<int>& height) {
        
        int n = height.size();
        int i = 0; // left pointer
        int j = n - 1; // right pointer
        int max_area = 0; // the return

        for(i = 0; i < n; i++){
            for(j = n - 1; j > i; j--){

                // do something here
            }
        }


    }
};

caculate the current water capacity:

class Solution {
public:
    int maxArea(vector<int>& height) {
        
        int n = height.size();
        int i = 0; // left pointer
        int j = n - 1; // right pointer
        int max_area = 0; // the return

        for(i = 0; i < n; i++){
            for(j = n - 1; j > i; j--){

                // do something here
                int h = min(height[i], height[j]);
                int area = h * (j - i);
            }
        }


    }
};

compare rcurrent area and the max_area:

class Solution {
public:
    int maxArea(vector<int>& height) {
        
        int n = height.size();
        int i = 0; // left pointer
        int j = n - 1; // right pointer
        int max_area = 0; // the return

        for(i = 0; i < n; i++){
            for(j = n - 1; j > i; j--){

                // do something here
                int h = min(height[i], height[j]);
                int area = h * (j - i);
                if(area > max_area){
                    area = max_area;
                }
            }
        }

        return max_area;


    }
};

the code runs but in a wrong way:

 sth. wrong, if area > max_area, the the max_area = area, in code is max_area = area.

class Solution {
public:
    int maxArea(vector<int>& height) {
        
        int n = height.size();
        int i = 0; // left pointer
        int j = n - 1; // right pointer
        int max_area = 0; // the return

        for(i = 0; i < n; i++){
            for(j = n - 1; j > i; j--){

                // do something here
                int h = min(height[i], height[j]);
                int area = h * (j - i);
                if(area > max_area){
                    //area = max_area;
                    max_area = area;
                }
            }
        }

        return max_area;


    }
};

changes it and it works:

It works and it doesnot work.

Actually it is still a method of enumeration. Look at the test case:

Iteration the code:

reduce the moving steps, but how. 

the area equation cannot be changed, it is what it is.

then changs the moving method. Donot need to move every time. Compare height[i] and height[j], moves the shorter one. 

class Solution {
public:
    int maxArea(vector<int>& height) {
        
        int n = height.size();
        int i = 0; // left pointer
        int j = n - 1; // right pointer
        int max_area = 0; // the return

        while(i < j){
            // Calculate the height of the current rectangle
            int h = min(height[i], height[j]);
            // Calculate the area of the current rectangle
            int area = h * (j - i);
            // Update max_area if the current area is larger
            if(area > max_area){
                max_area = area;
            }
            // Move the pointers
            if (height[i] < height[j]){
                i++;
            } else {
                j--;
            }
        }

        return max_area;
    }
};

it works.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值