Algorithm:接雨水

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

 

示例 1:



输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 
示例 2:

输入:height = [4,2,0,3,2,5]
输出:9
 

提示:

n == height.length
1 <= n <= 2 * 104
0 <= height[i] <= 105

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/trapping-rain-water
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

方法一:(超出时间限制)

class Solution {
public:
    int trap(vector<int>& height) {
        int size = height.size();
   
        if(size <= 2)
        {
            return 0;
        }

        int first_index = height[0] > height[1] ? 0 : 1;
        int second_index = height[0] < height[1] ? 0 : 1;

        for(int i = 2; i < size; i++)
        {
            if(height[i] >= height[first_index])
            {
                second_index = first_index;
                first_index = i;
            }
            else if(height[i] > height[second_index])
            {
                second_index = i;
            }
        }

        //cout << "first_index = " << first_index << endl;
        //cout << "second_index = " << second_index << endl;

        int left_index = first_index < second_index ? first_index : second_index;

        int right_index = first_index > second_index ? first_index : second_index;

        int result = 0;
        for(int i = left_index + 1; i < right_index; i++)
        {
            int single_capacity = height[second_index] - height[i];
            if(single_capacity > 0)
            {
                result += single_capacity;
            }
        }

        //cout << "result = " << result << endl;
        vector<int> left_height;
        vector<int> right_height;

        left_height.assign(height.begin(), height.begin() + left_index + 1);
        right_height.assign(height.begin()+right_index, height.end());

        result += trap(left_height);
        //cout << "left result = " << result << endl;
        
        result += trap(right_height);
        //cout << "right result = " << result << endl;

        return result;
    }
};

方法二:(超出时间限制)

class Solution {
public:
    int trap(vector<int>& height) {
        int size = height.size();
        int result = 0;

        for(int i = 1; i < size - 1; i++)
        {
            int max_left = 0, max_right = 0;

            for(int j = 0; j <= i; j++)
            {
                //if(height[j] > max_left)
                //{
                    //max_left = height[j];
                //}

                max_left = max(max_left, height[j]);
            }

            for(int j = i; j < size; j++)
            {
                //if(height[j] > max_right)
                //{
                    //max_right = height[j];
                //}
                max_right = max(max_right, height[j]);
            }

            //int min_heigh = max_left < max_right ? max_left : max_right;
            //int current_capacity = min(max_left, max_right) - height[i];

            //if(current_capacity > 0)
            //{
               // result += current_capacity;
            //}
            result += min(max_left, max_right) - height[i];
        }

        return result;
    }
};

方法三:

该方法是在方法二基础上面的优化。通过遍历两遍,找出对应的坐标,左侧最大值和右侧最大值。

class Solution {
public:
    int trap(vector<int>& height) {
        if (height.size() < 3)
            return 0;

        int ans = 0;
        int size = height.size();
        vector<int> left_max(size), right_max(size);
        left_max[0] = height[0];
        for (int i = 1; i < size; i++) {
            left_max[i] = max(height[i], left_max[i - 1]);
        }

        right_max[size - 1] = height[size - 1];
        for (int i = size - 2; i >= 0; i--) {
            right_max[i] = max(height[i], right_max[i + 1]);
        }
        for (int i = 1; i < size - 1; i++) {
            ans += min(left_max[i], right_max[i]) - height[i];
        }

        return ans;
    }
};

雨水是一道经典的算法问题,可以通过可视化来更好地理解。以下是一个基于C++的雨水可视化示例: ```cpp #include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <Windows.h> using namespace std; const int kScreenWidth = 100; // 屏幕宽度 const int kScreenHeight = 30; // 屏幕高度 void DrawBar(int x, int y, int height, char ch) { COORD pos; HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE); pos.X = x; pos.Y = y; SetConsoleCursorPosition(output, pos); for (int i = 0; i < height; i++) { cout << ch; pos.Y++; SetConsoleCursorPosition(output, pos); } } void DrawWater(int x, int y, int width, char ch) { COORD pos; HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE); pos.X = x; pos.Y = y; SetConsoleCursorPosition(output, pos); for (int i = 0; i < width; i++) { cout << ch; pos.X++; SetConsoleCursorPosition(output, pos); } } void Draw(vector<int>& height) { int maxHeight = *max_element(height.begin(), height.end()); int minHeight = *min_element(height.begin(), height.end()); int screenTop = kScreenHeight - 1; int screenBottom = 0; for (int i = 0; i < height.size(); i++) { int x = i * (kScreenWidth - 1) / (height.size() - 1); int y = screenTop - (height[i] - minHeight) * (screenTop - screenBottom) / (maxHeight - minHeight); DrawBar(x, y, kScreenHeight - y - 1, '#'); } for (int i = 1; i < height.size() - 1; i++) { int leftHeight = height[i]; for (int j = i - 1; j >= 0; j--) { leftHeight = max(leftHeight, height[j]); } int rightHeight = height[i]; for (int j = i + 1; j < height.size(); j++) { rightHeight = max(rightHeight, height[j]); } int water = min(leftHeight, rightHeight) - height[i]; if (water > 0) { int x = i * (kScreenWidth - 1) / (height.size() - 1); int y = screenTop - (min(leftHeight, rightHeight) - minHeight) * (screenTop - screenBottom) / (maxHeight - minHeight) - water; DrawWater(x, y, 1, '~'); } } } int main() { vector<int> height = {0,1,0,2,1,0,1,3,2,1,2,1}; Draw(height); return 0; } ``` 该程序使用了Windows API来控制控制台输出。首先,我们定义了`DrawBar`函数和`DrawWater`函数来分别绘制柱子和水。其中,`DrawBar`函数受柱子的坐标`(x, y)`和高度`height`,使用循环输出字符来绘制柱子;`DrawWater`函数受水的坐标`(x, y)`、长度`width`和字符`ch`,使用循环输出字符来绘制水。 着,我们定义了`Draw`函数来绘制整个场景。该函数首先计算出最高柱子和最低柱子的高度,并将屏幕分成若干个区间。然后,对于每个柱子,根据其高度计算出在屏幕上的位置,并使用`DrawBar`函数绘制柱子。最后,对于每个柱子,计算它左边和右边的最高柱子高度,根据高度差计算出水的高度,并使用`DrawWater`函数绘制水。 最后,在`main`函数中定义了一个示例输入,调用`Draw`函数绘制出结果。 运行程序,可以看到绘制出了一个柱状图和水的效果,可以更好地理解雨水问题的解法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AllenSun-1990

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值