Skip to content

Commit 296e91b

Browse files
authored
2024-12-16 v. 7.3.5: added "641. Design Circular Deque"
2 parents 98d979c + 75e8078 commit 296e91b

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,3 +599,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
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) |
600600
| 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) |
601601
| 623. Add One Row to Tree | [Link](https://leetcode.com/problems/add-one-row-to-tree/) | [Link](./lib/medium/623_add_one_row_to_tree.rb) | [Link](./test/medium/test_623_add_one_row_to_tree.rb) |
602+
| 641. Design Circular Deque | [Link](https://leetcode.com/problems/design-circular-deque/) | [Link](./lib/medium/641_design_circular_deque.rb) | [Link](./test/medium/test_641_design_circular_deque.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.4'
8+
s.version = '7.3.5'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../common/linked_list'
4+
5+
# https://leetcode.com/problems/design-circular-deque/description/
6+
class MyCircularDeque
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 insert_front(value)
18+
return false if is_full
19+
20+
new_head = ::ListNode.new(value)
21+
if @tail
22+
new_head.next = @head.next
23+
@head.next = new_head
24+
else
25+
@tail = new_head
26+
@head.next = @tail
27+
@tail.next = @head
28+
end
29+
30+
@actual_size += 1
31+
32+
true
33+
end
34+
35+
# @param {Integer} value
36+
# @return {Boolean}
37+
def insert_last(value)
38+
return false if is_full
39+
40+
new_tail = ::ListNode.new(value)
41+
if @tail
42+
@tail.next = new_tail
43+
else
44+
@head.next = new_tail
45+
end
46+
47+
@tail = new_tail
48+
@actual_size += 1
49+
50+
true
51+
end
52+
53+
# @param {Boolean}
54+
def delete_front
55+
return false if @actual_size.zero?
56+
57+
@head.next = @head.next.next
58+
@tail = nil if @actual_size == 1
59+
60+
@actual_size -= 1
61+
62+
true
63+
end
64+
65+
# @param {Boolean}
66+
def delete_last
67+
return false if @actual_size.zero?
68+
69+
if @actual_size == 1
70+
@head.next = nil
71+
@tail = nil
72+
else
73+
new_tail = @head.next
74+
new_tail = new_tail.next until new_tail.next == @tail
75+
new_tail.next = @head
76+
@tail = new_tail
77+
end
78+
79+
@actual_size -= 1
80+
81+
true
82+
end
83+
84+
# @return {Integer}
85+
def get_front = @actual_size.zero? ? -1 : @head.next.val
86+
87+
# @return {Integer}
88+
def get_rear = @actual_size.zero? ? -1 : @tail.val
89+
90+
# @return {Boolean}
91+
def is_empty = @actual_size.zero?
92+
93+
# @return {Boolean}
94+
def is_full = @actual_size == @size
95+
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/641_design_circular_deque'
5+
require 'minitest/autorun'
6+
7+
class DesignCircularDequeTest < ::Minitest::Test
8+
def test_default_one
9+
my_circular_deque = ::MyCircularDeque.new(3)
10+
11+
assert(my_circular_deque.insert_last(1))
12+
assert(my_circular_deque.insert_last(2))
13+
assert(my_circular_deque.insert_front(3))
14+
assert(!my_circular_deque.insert_front(4))
15+
16+
assert_equal(2, my_circular_deque.get_rear)
17+
18+
assert(my_circular_deque.is_full)
19+
assert(my_circular_deque.delete_last)
20+
assert(my_circular_deque.insert_front(4))
21+
22+
assert_equal(4, my_circular_deque.get_front)
23+
end
24+
25+
def test_additional_one
26+
my_circular_deque = ::MyCircularDeque.new(5)
27+
28+
assert(my_circular_deque.insert_front(7))
29+
assert(my_circular_deque.insert_last(0))
30+
31+
assert_equal(7, my_circular_deque.get_front)
32+
33+
assert(my_circular_deque.insert_last(3))
34+
assert_equal(3, my_circular_deque.get_rear)
35+
36+
assert(my_circular_deque.insert_front(9))
37+
38+
assert_equal(3, my_circular_deque.get_rear)
39+
assert_equal(9, my_circular_deque.get_front)
40+
assert_equal(9, my_circular_deque.get_front)
41+
42+
assert(my_circular_deque.delete_last)
43+
44+
assert_equal(0, my_circular_deque.get_rear)
45+
46+
assert(my_circular_deque.delete_last)
47+
assert(my_circular_deque.delete_last)
48+
49+
assert(my_circular_deque.insert_front(9))
50+
assert(my_circular_deque.delete_front)
51+
assert(my_circular_deque.delete_last)
52+
end
53+
end

0 commit comments

Comments
 (0)