the goal of my code is encode numbers by adding each number to the next bigger number and if no bigger number was found i use the same number. [4,3,7,3,2,8,6,1,10,3] =>[11,10,15,11,10,18,16,11,10,3]
my code's time complexity is O(n2) and i think i can do O(n log n) i dont know tho what kinda algorithm should i use since i also want to find the first bigger element
i did try binary search but i realized that it goes from the middle not the first.
import queue
q= queue.Queue()
a=[4,3,7,3,2,8,6,1,10,3]
for i in a:
q.put(i)
encoded=[]
while q:
current=q.get()
for i in range(q.qsize()):
if current<q.queue[i]:
encoded.append(q.queue[i]+current)
break
print(encoded)
Here is tree-based solution, If I count right should be
O(n log n): sorted is O(n log n) + searching in tree is O(n log n) too:Prints: