鱼C论坛

 找回密码
 立即注册
查看: 7208|回复: 23

[技术交流] 鱼C论坛Python精英挑战赛(第四季02期)

[复制链接]
发表于 2017-11-30 11:32:16 | 显示全部楼层
本帖最后由 蓝色王魂 于 2017-11-30 12:40 编辑

目前只能想出这个方法,效率还行,但是代码丑。是分别计算每层的水洼面积再累加的。
  1. def max_rainwater(list_of_number):
  2.     result_of_max_rainwater = 0
  3.     length = len(list_of_number)
  4.     max_of_list = max(list_of_number)
  5.     for times in range(max_of_list):
  6.         for i in range(length):
  7.             if list_of_number[i]>0:
  8.                 list_of_number[i] -= 1
  9.                 start = i
  10.                 break
  11.         for i in range(start+1,length):
  12.             if list_of_number[i] > 0:
  13.                 list_of_number[i] -= 1
  14.                 distance = i-start-1
  15.                 start = i
  16.                 if distance>0:
  17.                     result_of_max_rainwater += distance
  18.     return result_of_max_rainwater
复制代码

点评

验算答案正确,效率有待提高。  发表于 2017-11-30 16:33

评分

参与人数 1荣誉 +2 鱼币 +2 贡献 +2 收起 理由
jerryxjr1220 + 2 + 2 + 2 答题奖励,不用着急,还有时间可以再优化

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-12-2 22:25:10 | 显示全部楼层
本帖最后由 蓝色王魂 于 2017-12-2 22:41 编辑

两种优化方式,方法都是是积水后的面积减原来的面积,如果都大于一个较大的数,比如说都大于10000,第一种方法效率就会很低,或者如果对与list(set(list_of_number)),相邻的值都差个2,3之类的,第一个方法效率也会变低,但如果数字是从0到最大值基本上每个都有取到的话,第一种方法会稍快。
  1. def max_rainwater2(list_of_number):
  2.     max_value = max(list_of_number)
  3.     sum_all = sum(list_of_number)
  4.     length = len(list_of_number)
  5.     sum_watered = 0
  6.     for i in range(1,max_value+1):
  7.         for j in range(length):
  8.             if list_of_number[j] >= i:
  9.                 start = j
  10.                 break
  11.         for j in range(length-1,-1,-1):
  12.             if list_of_number[j] >= i:
  13.                 end = j
  14.                 break
  15.         sum_watered += end - start + 1
  16.     return (sum_watered-sum_all)

  17. def max_rainwater3(list_of_number):
  18.     sum_all = sum(list_of_number)
  19.     sum_watered = 0
  20.     value_list = list(set(list_of_number))
  21.     length = len(list_of_number)
  22.     shuiwei = 0
  23.     for each in value_list:
  24.         for i in range(length):
  25.             if list_of_number[i] >= each:
  26.                 start = i
  27.                 break
  28.         for i in range(length-1,-1,-1):
  29.             if list_of_number[i] >= each:
  30.                 end = i
  31.                 break
  32.         sum_watered += (each-shuiwei) * (end - start + 1)
  33.         shuiwei = each
  34.     return (sum_watered-sum_all)
复制代码

点评

还是太复杂,我能用一行代码写出来你信不信?...  发表于 2017-12-3 20:32
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-12-4 18:49:23 | 显示全部楼层
最终版
  1. def max_rainwater4(list_of_number):
  2.     sum_watered = max(list_of_number)
  3.     sum_all = sum(list_of_number)
  4.     max_index = list_of_number.index(sum_watered)
  5.     shuiwei = list_of_number[0]
  6.     start = 0
  7.     length = len(list_of_number)
  8.     for i in range(1,max_index+1):
  9.         if list_of_number[i] >= shuiwei:
  10.             sum_watered += shuiwei*(i-start)
  11.             start = i
  12.             shuiwei = list_of_number[i]
  13.     shuiwei = list_of_number[-1]
  14.     start = length - 1
  15.     for i in range(length-2,max_index-1,-1):
  16.         if list_of_number[i] >= shuiwei:
  17.             sum_watered += shuiwei*(start - i)
  18.             start = i
  19.             shuiwei = list_of_number[i]
  20.     return (sum_watered-sum_all)
复制代码

点评

我很赞同!: 5.0
我很赞同!: 5
0.002s,1000列表长度,验算正确。100万列表长度,0.081s,非常棒!  发表于 2017-12-5 21:16
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-4-1 21:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表