This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Deque | |
def initialize | |
@first = Node.new(nil) | |
@last = @first | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pushFront(number) | |
@first = Node.new(number, @first, nil) | |
@last = @first if @last and @last.value.nil? | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pushBack(number) | |
self.pushFront(number) && return if @first.value.nil? | |
@last.next_node = Node.new(number) | |
@last = @last.next_node | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def popFront | |
if @last == @first | |
@first = Node.new(nil) | |
@last = @first | |
return | |
end | |
@first = @first.next_node | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def popBack | |
@last = @last.prev_node and return unless @first == @last | |
@first = Node.new(nil) | |
@last = @first | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def topFront | |
@first.value unless @first.nil? | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def topBack | |
@last.value unless @last.nil? | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_empty? | |
@first.nil? | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer