2019/11/17【接雨水】

題目一:【接雨水】力扣——42

給定 n 個非負整數表示每個寬度爲 1 的柱子的高度圖,計算按此排列的柱子,下雨之後能接多少雨水。

上面是由數組 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度圖,在這種情況下,可以接 6 個單位的雨水(藍色部分表示雨水)。 感謝 Marcos 貢獻此圖。

示例:

輸入: [0,1,0,2,1,0,1,3,2,1,2,1]
輸出: 6

腳本:

#!/bin/bash
#接雨水
#author:yzt 2019-11-17
#
read -t 40 -p "請輸入數組:" contents
declare -i aa=1
declare -i bb=1
cat /dev/null >result.txt
echo "$contents"|sed 's#\[\|\]##g'|sed 's#,#\n#g'|grep -n -v "^$" >tmp1.txt
total=`cat tmp1.txt|wc -l`
while :
do
        i=`sed -n "$bb p" tmp1.txt`
        hangshu1=`echo "$i"|awk -F : '{print $1}'`
        zhugao=`echo "$i"|awk -F : '{print $2}'`
        hangshu2=`sed "$aa,$bb d" tmp1.txt|awk -F : -v j=$zhugao '$2>=j{print $1}'|head -n1`
        hangshu3=`sed "$aa,$bb d" tmp1.txt|awk -F : -v j=$zhugao '$2<j{print $0}'\
|sort -n -t ":" -k 2|tail -n1|awk -F : '{print $1}'`
        if [ ! -z $hangshu3 ];then
                zhugao1=`sed -n "$hangshu3 p" tmp1.txt|awk -F : '{print $2}'`
        fi
        if [ ! -z $hangshu2 ];then
                length1=$[$hangshu2-$hangshu1]
                shuiliang1=$[$zhugao * $length1]
                hangshu4=$[$hangshu2-1]
                sed -n "$hangshu1,$hangshu4 p" tmp1.txt|awk -F : '{print $2}' > tmp2.txt
                sum=0
                for j in `cat tmp2.txt`
                do
                        sum=$[$sum+$j]
                done
                shuiliang1=$[$shuiliang1-$sum]
                echo "$shuiliang1" >>result.txt
                bb=$hangshu2
        else
                if [ -z $hangshu3 ];then
                        break
                else
                        length2=$[$hangshu3-$hangshu1]
                        shuiliang2=$[$length2 * $zhugao1]
                        hangshu5=$[$hangshu1+1]
                        sed -n "$hangshu5,$hangshu3 p" tmp1.txt|awk -F : '{print $2}' >tmp2.txt
                        sum1=0
                        for k in `cat tmp2.txt`
                        do
                                sum1=$[$sum1+$k]
                        done
                        shuiliang2=$[$shuiliang2-$sum1]
                        echo "$shuiliang2" >>result.txt
                        bb=$hangshu3
                fi
        fi
        if [ $bb -ge $total ];then
                 break
        fi
done

totle_sum=o
for item in `cat result.txt`
do
        totle_sum=$[$totle_sum + $item]
done
echo "$totle_sum"
            

【腳本邏輯】:

第一:將數組中的非負整數想象爲牆高,接雨水的條件是:兩牆之間有凹槽存在。

第二:從左邊第一塊牆,稱爲a牆開始,如果右邊存在比a牆高(含相等)的b牆時,a牆與b牆之間能裝的雨水爲以a牆的高度乘以ab牆之間的寬度,並且減去a牆到b牆之前存在的小牆;如:(此時第5塊牆變爲左邊第一塊牆,繼續後續操作)

第三:從左邊第一塊牆,牆a開始,右邊並沒有比牆a高或相等的牆,此時需要採用另外一種做法:

獲取右邊的牆中“最高且排最右邊”的牆(有可能右邊有兩塊牆是最高的),先計算出其總的容量,再減去已存在牆體的容量,最後得到雨水的容量,如下所示:(此時左邊的第一塊牆變爲下圖的第10塊牆,並執行後續操作)

第四:程序將在左邊第一塊牆的行數大於或等於數組的個數時,退出循環,並開始計算雨水的總量。

 

腳本效果:

[root@localhost leetcode]# ./jieyushui.sh
請輸入數組:[0,0,4,0,1,3,9,0,4,8,1]
20
[root@localhost leetcode]# vim jieyushui.sh
[root@localhost leetcode]# ./jieyushui.sh
請輸入數組:[0,2,0,0,4,1]
4
[root@localhost leetcode]# ./jieyushui.sh
請輸入數組:[0,2,0,0,4,0,1]
5
[root@localhost leetcode]# ./jieyushui.sh
請輸入數組:[0,1,0,2,1,0,1,3,2,1,2,1]
6

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章