赞
踩
题意:存在一根电线杆,可以是第一根或者最后一根,在该电线杆前的电线杆的高度是不增的,在它之后的电线杆的高度是不减的,请求出最少需要调整多少根电线杆的高度、
输入n+1行
第一行整数n表示电线杆数
剩下n行表示电线杆的高度
10
6 3 4 3 3 4 5 5 4 6
输出2
注意是存在,至少调整的数量、
- # 就是标记从左到右不满足非递减的数,标记从右到左不满足非递减的数,然后统计i处左右两的不满足条件的标记数,最后取最小
- class Solution:
- def min_adjustment(self, n: int, hight: 'list[int]'):
- dec, inc = [0] * n, [0] * n
- def adjusted():
- # mark non-decreasing Numbers
- low = hight[0]
- for i in range(1,n): # from left to right
- if hight[i] > low:
- dec[i] = 1 # need be adjusted
- else:
- low = hight[i] # update the value of low
- # mark non-increasing Numbers
- low = hight[-1]
- for j in range(n-2, -1,-1): # from right to left
- if hight[j] > low:
- inc[j] = 1
- else:
- low = hight[j]
-
- adjusted()
-
- res = [0] * n
- # Suppose i meets the conditions
- for i in range(n):
- res[i] = sum(dec[:i]) + sum(inc[i+1:])
- # return the smallest number
- return min(res)
-
- if __name__=='__main__':
- s = Solution()
- n = int(input().strip())
- hight = list(map(int,input().strip().split()))
- print(s.min_adjustment(n, hight))
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。