Find each element that is less than some element to its right

后端 未结 6 1713
滥情空心
滥情空心 2021-01-12 03:05

I need to find elements of a vector that are less than one of more elements that come after it. It\'s easy to do in a loop:

x = some_vector_values;
for m = 1         


        
6条回答
  •  滥情空心
    2021-01-12 03:41

    This should be an algorithm that takes O(n) time and O(n) memory: Label the last element in your array the maximum element. Iterate backwards over the array. Whenever you have an element smaller than your maximum, save it. Otherwise, it becomes your new maximum. This should get you all of the elements you need with a single pass.

提交回复
热议问题