Code To Count How Many Integers Are Strictly Larger Than All The Integers To Their Right Using Python
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=1594
Solution 1:
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]
defmax_so_far(l):
m = 0for i in l[::-1]:
m = max(m, i)
yield m
sum([x>y for x,y inzip(l[-2::-1], max_so_far(l))])
Solution 2:
How about this code?
mylist = list(map(int,input().split()))
count = 0for i,item inenumerate(mylist):
if i == len(mylist) - 1: #here we excluded the last element for a checkbreak
check = max(mylist[i+1:])
if check < item:
count = count + 1print(count)
Solution 3:
I am understating this problem in two way:
counter = 0for i in range(len(list_of_number)):
for j in range(i, len(list_of_number)):
if list_of_number[j] > list_of_number[i]:
counter += 1
Here, after it gets the first value, it scans all the list. The code that follow it will check only the number's right neighbour
counter = 0for i in range(len(list_of_number)-1):
if list_of_numbers[i+1] > list_of_numbers[i]:
counter +=1
Solution 4:
How about this?
defcounter(L):
returnsum([1for i inrange(len(L)-1) if L[i] < L[i+1]])
Post a Comment for "Code To Count How Many Integers Are Strictly Larger Than All The Integers To Their Right Using Python"