使用python来计算有多少个整数严格比其右边的所有整数大的代码 [英] Code to count how many integers are strictly larger than all the integers to their right using python
问题描述
以下是在O(N)中执行此操作的解决方案,其中N是列表的大小。
l = [12,4,4,2,2,2,3]
def max_so_far(l):
m = 0
对于i in l [::-1]:
m = max(m,i)
收益m
sum([x中y的x,y的y(x [l:-2 ::-1],max_so_far(l))]))
Given the list of integers, X, https://www.google.com/url?q=https://docs.google.com/document/d/1TjeNYpZ_PbdBISlJPF-_WqULBc1WpuYthLClovjB3Rs/edit?usp%3Dsharing&sa=D&ust=1594968749928000&usg=AFQjCNG8bAv1lX8pXr4CYcgaDfYFxcbgCg
I want to write code to count how many integers are strictly larger than all the integers to their right excluding the last digit since it doesn’t have a number to its right. E.g. for [2,3,1] the answer should be 1 while for [12,4,4,2,2,3] the answer is 2. I have no clue how to write the code. I will appreciate any guidance on how to proceed.
Here's a solution that does that in O(N), where N is the size of the list.
l = [12,4,4,2,2,3]
def max_so_far(l):
m = 0
for i in l[::-1]:
m = max(m, i)
yield m
sum([x>y for x,y in zip(l[-2::-1], max_so_far(l))])
这篇关于使用python来计算有多少个整数严格比其右边的所有整数大的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!