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
class Deque
def initialize
@first = Node.new(nil)
@last = @first
end
end
@nikasulo
nikasulo / pushFront
Last active September 3, 2019 16:52
pushFront method for deque
def pushFront(number)
@first = Node.new(number, @first, nil)
@last = @first if @last and @last.value.nil?
end
def pushBack(number)
self.pushFront(number) && return if @first.value.nil?
@last.next_node = Node.new(number)
@last = @last.next_node
end
@nikasulo
nikasulo / popFront
Last active September 4, 2019 22:12
popFront method for Ruby Deque
def popFront
if @last == @first
@first = Node.new(nil)
@last = @first
return
end
@first = @first.next_node
end
@nikasulo
nikasulo / popBack
Last active September 3, 2019 16:55
popBack method for Ruby deque
def popBack
@last = @last.prev_node and return unless @first == @last
@first = Node.new(nil)
@last = @first
end
@nikasulo
nikasulo / topFront
Last active September 3, 2019 16:57
topFront method for Ruby Deque
def topFront
@first.value unless @first.nil?
end
@nikasulo
nikasulo / topBack
Last active September 3, 2019 16:58
topBack method for Ruby deque
def topBack
@last.value unless @last.nil?
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