From 39e3cb01e24828e3b72930c08f9a27a604d0a133 Mon Sep 17 00:00:00 2001 From: fartem Date: Fri, 13 Dec 2024 10:24:10 +0300 Subject: [PATCH] 2024-12-13 v. 7.3.2: added "621. Task Scheduler" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- lib/medium/621_task_scheduler.rb | 26 ++++++++++++++++++ test/medium/test_621_task_scheduler.rb | 37 ++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 lib/medium/621_task_scheduler.rb create mode 100644 test/medium/test_621_task_scheduler.rb diff --git a/README.md b/README.md index 5e1d0488..342a3d54 100644 --- a/README.md +++ b/README.md @@ -596,3 +596,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 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) | | 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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index e9807e63..18184910 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -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.1' + s.version = '7.3.2' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/621_task_scheduler.rb b/lib/medium/621_task_scheduler.rb new file mode 100644 index 00000000..8f5d0d91 --- /dev/null +++ b/lib/medium/621_task_scheduler.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/task-scheduler/ +# @param {Character[]} tasks +# @param {Integer} n +# @return {Integer} +def least_interval(tasks, n) + count = ::Array.new(128, 0) + max = 0 + max_count = 0 + + tasks.each do |task| + i = task.ord + count[i] += 1 + c = count[i] + + if c == max + max_count += 1 + elsif c > max + max = c + max_count = 1 + end + end + + [tasks.size, (max - 1) * (n + 1) + max_count].max +end diff --git a/test/medium/test_621_task_scheduler.rb b/test/medium/test_621_task_scheduler.rb new file mode 100644 index 00000000..1f76be30 --- /dev/null +++ b/test/medium/test_621_task_scheduler.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/621_task_scheduler' +require 'minitest/autorun' + +class TaskSchedulerTest < ::Minitest::Test + def test_default_one + assert_equal( + 8, + least_interval( + %w[A A A B B B], + 2 + ) + ) + end + + def test_default_two + assert_equal( + 6, + least_interval( + %w[A C A B D B], + 1 + ) + ) + end + + def test_default_three + assert_equal( + 10, + least_interval( + %w[A A A B B B], + 3 + ) + ) + end +end