降水量(python)
生活随笔
收集整理的這篇文章主要介紹了
降水量(python)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
給定n個柱面的高度,表示降雨某地n塊區域的海拔高度。 計算降雨之后該地最大儲水面積。如果低于地平線,也就是小于0,則一定積水
輸入描述:
第一行輸入整數n.(1<=n<=10000) 第二行輸入n個高度整數h。(-10000<=h<=10000)
輸出描述:
輸出答案。
示例
輸入
12
0 1 0 2 1 0 1 3 2 1 2 1
輸出
6
代碼
class Solution:def __init__(self) -> None:passdef solution(self, n, arr):result = 0low_water = 0max_water = 0# TODO: 請在此編寫代碼i = 0for x in range(0, len(arr)-1):if arr[x] > arr[x+1]:i = x # print(i)breakhigh_water = arr[i]high_i = i # print(i)while(i < len(arr)-1): # print(str(i)+"|||||||"+str(len(arr)))i = i+1 # print(str(high_i)+"=="+str(high_water)+"=="+str(arr[i]))if i < len(arr)-2:if high_water <= arr[i] and arr[i] > arr[i+1]:low_water = arr[i]j_ori = high_i + 1if high_water == 0 and i>0:max_water = low_waterj_ori = high_ielif high_water > low_water:max_water = low_waterelse:max_water = high_water # print(str(max_water)+"++"+str(low_water))for j in range(j_ori, i):result = result + (max_water - arr[j])high_i = ihigh_water = arr[i]elif i == len(arr)-1:i = high_ihigh_i = high_i+1high_water = arr[high_i]else:if high_water <= arr[i]:low_water = arr[i]j_ori = high_i + 1if high_water == 0 and i>0:max_water = low_waterj_ori = high_ielif high_water > low_water:max_water = low_waterelse:max_water = high_water # print(str(max_water)+"++"+str(low_water)+"++high_i:"+str(high_i))for j in range(j_ori, i): # print("222:"+str(result))result = result + (max_water - arr[j]) # print("222:"+str(result))high_i = ihigh_water = arr[i]elif i == len(arr)-1:high_i = high_i+1high_water = arr[high_i]i = high_i-1if high_i == len(arr)-2:break# TODO: 請在此編寫代碼return resultif __name__ == "__main__":n = int(input().strip())arr = [int(item) for item in input().strip().split()]sol = Solution()result = sol.solution(n, arr)print(result)通過率40%
總結
以上是生活随笔為你收集整理的降水量(python)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android开发蓝牙传输图片,如何发送
- 下一篇: 川崎机器人f控制柜接线图_plc控制柜接