Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 606. Construct String from Binary Tree | [Link](https://leetcode.com/problems/construct-string-from-binary-tree/) | [Link](./lib/medium/606_construct_string_from_binary_tree.rb) | [Link](./test/medium/test_606_construct_string_from_binary_tree.rb) |
| 609. Find Duplicate File in System | [Link](https://leetcode.com/problems/find-duplicate-file-in-system/) | [Link](./lib/medium/609_find_duplicate_file_in_system.rb) | [Link](./test/medium/test_609_find_duplicate_file_in_system.rb) |
| 621. Task Scheduler | [Link](https://leetcode.com/problems/task-scheduler/) | [Link](./lib/medium/621_task_scheduler.rb) | [Link](./test/medium/test_621_task_scheduler.rb) |
| 622. Design Circular Queue | [Link](https://leetcode.com/problems/design-circular-queue/) | [Link](./lib/medium/622_design_circular_queue.rb) | [Link](./test/medium/test_622_design_circular_queue.rb) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '7.3.2'
s.version = '7.3.3'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
69 changes: 69 additions & 0 deletions lib/medium/622_design_circular_queue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

require_relative '../common/linked_list'

# https://leetcode.com/problems/design-circular-queue/
class MyCircularQueue
# @param {Integer} k
def initialize(k)
@size = k
@actual_size = 0
@head = ::ListNode.new(0)
@tail = nil
end

# @param {Integer} value
# @return {Boolean}
def en_queue(value)
return false if is_full

if @tail
@tail.next = ::ListNode.new(value)
@tail = @tail.next
else
@tail = ::ListNode.new(value)
@head.next = @tail
end

@tail.next = @head
@actual_size += 1

true
end

# @return {Boolean}
def de_queue
return false if is_empty

if @actual_size == 1
@head.next = nil
@tail = nil
else
@head.next = @head.next.next
end

@actual_size -= 1

true
end

# @return {Integer}
def front
return -1 if is_empty

@head.next.val
end

# @return {Integer}
def rear
return -1 if is_empty

@tail.val
end

# @return {Boolean}
def is_empty = @actual_size.zero?

# @return {Boolean}
def is_full = @size == @actual_size
end
36 changes: 36 additions & 0 deletions test/medium/test_622_design_circular_queue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/622_design_circular_queue'
require 'minitest/autorun'

class DesignCircularQueueTest < ::Minitest::Test
def test_default_one
my_circular_queue = ::MyCircularQueue.new(3)

assert(my_circular_queue.en_queue(1))
assert(my_circular_queue.en_queue(2))
assert(my_circular_queue.en_queue(3))
assert(!my_circular_queue.en_queue(4))

assert_equal(3, my_circular_queue.rear)

assert(my_circular_queue.is_full)
assert(my_circular_queue.de_queue)
assert(my_circular_queue.en_queue(4))

assert_equal(4, my_circular_queue.rear)
end

def test_additional_one
my_circular_queue = ::MyCircularQueue.new(1)

assert_equal(-1, my_circular_queue.front)

assert(my_circular_queue.en_queue(2))

assert_equal(2, my_circular_queue.front)

assert(my_circular_queue.de_queue)
end
end
Loading