Skip to content

Commit 30ad586

Browse files
committed
2024-12-30 v. 7.5.5: added "763. Partition Labels"
1 parent dbc31b3 commit 30ad586

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,3 +619,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
619619
| 729. My Calendar I | [Link](https://leetcode.com/problems/my-calendar-i/) | [Link](./lib/medium/729_my_calendar_i.rb) | [Link](./test/medium/test_729_my_calendar_i.rb) |
620620
| 735. Asteroid Collision | [Link](https://leetcode.com/problems/asteroid-collision/) | [Link](./lib/medium/735_asteroid_collision.rb) | [Link](./test/medium/test_735_asteroid_collision.rb) |
621621
| 739. Daily Temperatures | [Link](https://leetcode.com/problems/daily-temperatures/) | [Link](./lib/medium/739_daily_temperatures.rb) | [Link](./test/medium/test_739_daily_temperatures.rb) |
622+
| 763. Partition Labels | [Link](https://leetcode.com/problems/partition-labels/) | [Link](./lib/medium/763_partition_labels.rb) | [Link](./test/medium/test_763_partition_labels.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.5.4'
8+
s.version = '7.5.5'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/medium/763_partition_labels.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/partition-labels/
4+
# @param {String} s
5+
# @return {Integer[]}
6+
def partition_labels(s)
7+
last = ::Array.new(128, 0)
8+
(0...s.size).each { |i| last[s[i].ord] = i }
9+
10+
j = 0
11+
anchor = 0
12+
result = []
13+
(0...s.size).each do |i|
14+
j = [j, last[s[i].ord]].max
15+
16+
next unless i == j
17+
18+
result << i - anchor + 1
19+
anchor = i + 1
20+
end
21+
22+
result
23+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/763_partition_labels'
5+
require 'minitest/autorun'
6+
7+
class PartitionLabelsTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
[9, 7, 8],
11+
partition_labels(
12+
'ababcbacadefegdehijhklij'
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
[10],
20+
partition_labels(
21+
'eccbbbbdec'
22+
)
23+
)
24+
end
25+
end

0 commit comments

Comments
 (0)