Skip to content

Commit

Permalink
Ruby Solution for Sliding Window Maximum
Browse files Browse the repository at this point in the history
  • Loading branch information
saip7795 committed Jan 10, 2023
1 parent eff3535 commit a846f22
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions ruby/0239-sliding-window-maximum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

def max_sliding_window(nums, k)
output = []

q = []

l = r = 0

while r < nums.length()

while q[l] && nums[q[-1]] < nums[r]
q.pop()
end

q.append(r)

l +=1 if q[l] == r - k

output.append(nums[q[l]]) if (r+1) >= k

r +=1
end
return output
end

0 comments on commit a846f22

Please sign in to comment.