Skip to content

Instantly share code, notes, and snippets.

View nikasulo's full-sized avatar
🚀
Working Remotely

Nikasulo nikasulo

🚀
Working Remotely
View GitHub Profile
@nikasulo
nikasulo / Sliding_max_tutorial
Last active August 24, 2019 22:00
This is a tutorial for the sliding maximum problem using a deque to solve it
def sliding_maximum(k,array)
#First part
deque = Deque.new
result = []
#Second part
#your loop goes here
#Third and final part
for i in (0...array.length) do
#rest of your code in here
end
deque.pushBack(array[i]) if deque.is_empty?
@nikasulo
nikasulo / Sliding_max_tutorial
Created August 24, 2019 22:12
Set max element
def set_max(deque,max)
deque.pushFront(max)
end
@nikasulo
nikasulo / Sliding_max_tutorial
Created August 24, 2019 22:15
Initialize max variable
max = get_max(deque)
@nikasulo
nikasulo / Sliding_max_tutorial
Created August 24, 2019 22:16
Get max function
def get_max(deque)
deque.topFront
end
@nikasulo
nikasulo / Sliding_max_tutorial
Created August 24, 2019 22:23
Compare the max to other numbers in the window
#check if the current item in the array is greater than what our max currently is
if max < array[i]
#if it is, first remove the max
remove_max(deque)
#now set the current index value of the array as the max
set_max(deque, array[i])
end
@nikasulo
nikasulo / Sliding_max_tutorial
Created August 24, 2019 22:30
Remove maximum
def remove_max(deque)
deque.popFront
end
@nikasulo
nikasulo / Sliding_max_tutorial
Created August 24, 2019 22:35
Get the max and append it to the result
max = get_max(deque)
if i >= k - 1
add_max_to_result(result, max)
end
@nikasulo
nikasulo / Sliding_max_tutorial
Created August 24, 2019 22:57
Check for runner ups
#check if what's currently in the back is a potential winner
if deque.topBack > array[i]
#if it is, store the runner up behind it
deque.pushBack(array[i])
else
#if it's not, remove it and then store the new winner in the back
deque.popBack
deque.pushBack(array[i])
end