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 / Node Class for a deque
Last active August 23, 2019 18:13
Ruby Node For a Deque
class Node
attr_accessor :value, :next_node, :prev_node
def initialize(value, next_node = nil, prev_node = nil)
@value = value
@next_node = next_node
@prev_node = prev_node
end
end
@nikasulo
nikasulo / isEmpty?
Created August 23, 2019 18:58
isEmpty method for Ruby Deque
def is_empty?
@first.nil?
end
@nikasulo
nikasulo / brute_force_sliding_maximum
Created August 24, 2019 20:42
A brute force solution to the sliding maximum problem
def sliding_maximum(k, array)
res = []
for i in 0..array.length - k do
max = array[i]
for j in i...i+k do
max = array[j] > max ? array[j] : max
end
res << max
end
res
for i in (0...array.length) do
#rest of your code in here
end
@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
@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