Skip to content

Commit d62708e

Browse files
authored
2024-12-13 v. 7.3.3: added "622. Design Circular Queue"
2024-12-13 v. 7.3.3: added "622. Design Circular Queue"
2 parents 2b0bcfc + 652d6e8 commit d62708e

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,3 +597,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
597597
| 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) |
598598
| 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) |
599599
| 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) |
600+
| 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) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '7.3.2'
8+
s.version = '7.3.3'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../common/linked_list'
4+
5+
# https://leetcode.com/problems/design-circular-queue/
6+
class MyCircularQueue
7+
# @param {Integer} k
8+
def initialize(k)
9+
@size = k
10+
@actual_size = 0
11+
@head = ::ListNode.new(0)
12+
@tail = nil
13+
end
14+
15+
# @param {Integer} value
16+
# @return {Boolean}
17+
def en_queue(value)
18+
return false if is_full
19+
20+
if @tail
21+
@tail.next = ::ListNode.new(value)
22+
@tail = @tail.next
23+
else
24+
@tail = ::ListNode.new(value)
25+
@head.next = @tail
26+
end
27+
28+
@tail.next = @head
29+
@actual_size += 1
30+
31+
true
32+
end
33+
34+
# @return {Boolean}
35+
def de_queue
36+
return false if is_empty
37+
38+
if @actual_size == 1
39+
@head.next = nil
40+
@tail = nil
41+
else
42+
@head.next = @head.next.next
43+
end
44+
45+
@actual_size -= 1
46+
47+
true
48+
end
49+
50+
# @return {Integer}
51+
def front
52+
return -1 if is_empty
53+
54+
@head.next.val
55+
end
56+
57+
# @return {Integer}
58+
def rear
59+
return -1 if is_empty
60+
61+
@tail.val
62+
end
63+
64+
# @return {Boolean}
65+
def is_empty = @actual_size.zero?
66+
67+
# @return {Boolean}
68+
def is_full = @size == @actual_size
69+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/622_design_circular_queue'
5+
require 'minitest/autorun'
6+
7+
class DesignCircularQueueTest < ::Minitest::Test
8+
def test_default_one
9+
my_circular_queue = ::MyCircularQueue.new(3)
10+
11+
assert(my_circular_queue.en_queue(1))
12+
assert(my_circular_queue.en_queue(2))
13+
assert(my_circular_queue.en_queue(3))
14+
assert(!my_circular_queue.en_queue(4))
15+
16+
assert_equal(3, my_circular_queue.rear)
17+
18+
assert(my_circular_queue.is_full)
19+
assert(my_circular_queue.de_queue)
20+
assert(my_circular_queue.en_queue(4))
21+
22+
assert_equal(4, my_circular_queue.rear)
23+
end
24+
25+
def test_additional_one
26+
my_circular_queue = ::MyCircularQueue.new(1)
27+
28+
assert_equal(-1, my_circular_queue.front)
29+
30+
assert(my_circular_queue.en_queue(2))
31+
32+
assert_equal(2, my_circular_queue.front)
33+
34+
assert(my_circular_queue.de_queue)
35+
end
36+
end

0 commit comments

Comments
 (0)