Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1983 from saip7795/sp/sliding-window-m…
Browse files Browse the repository at this point in the history
…aximum

Create: 0239-Sliding-Window-Maximum.rb
  • Loading branch information
tahsintunan authored Jan 11, 2023
2 parents 5a82e2f + a846f22 commit a82fd10
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 a82fd10

Please sign in to comment.