Skip to content

Commit 2b0bcfc

Browse files
authored
Merge pull request #803 from fartem/621-Task-Scheduler
2024-12-13 v. 7.3.2: added "621. Task Scheduler"
2 parents 93b8ac8 + 39e3cb0 commit 2b0bcfc

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,3 +596,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
596596
| 581. Shortest Unsorted Continuous Subarray | [Link](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/) | [Link](./lib/medium/581_shortest_unsorted_continuous_subarray.rb) | [Link](./test/medium/test_581_shortest_unsorted_continuous_subarray.rb) |
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) |
599+
| 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) |

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.1'
8+
s.version = '7.3.2'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/medium/621_task_scheduler.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/task-scheduler/
4+
# @param {Character[]} tasks
5+
# @param {Integer} n
6+
# @return {Integer}
7+
def least_interval(tasks, n)
8+
count = ::Array.new(128, 0)
9+
max = 0
10+
max_count = 0
11+
12+
tasks.each do |task|
13+
i = task.ord
14+
count[i] += 1
15+
c = count[i]
16+
17+
if c == max
18+
max_count += 1
19+
elsif c > max
20+
max = c
21+
max_count = 1
22+
end
23+
end
24+
25+
[tasks.size, (max - 1) * (n + 1) + max_count].max
26+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/621_task_scheduler'
5+
require 'minitest/autorun'
6+
7+
class TaskSchedulerTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
8,
11+
least_interval(
12+
%w[A A A B B B],
13+
2
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
6,
21+
least_interval(
22+
%w[A C A B D B],
23+
1
24+
)
25+
)
26+
end
27+
28+
def test_default_three
29+
assert_equal(
30+
10,
31+
least_interval(
32+
%w[A A A B B B],
33+
3
34+
)
35+
)
36+
end
37+
end

0 commit comments

Comments
 (0)