Skip to content

Commit bf6f79b

Browse files
authored
2024-12-19 v. 7.4.0: added "659. Split Array into Consecutive Subsequences"
2 parents fc2bdbc + ab92bb3 commit bf6f79b

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,3 +604,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
604604
| 648. Replace Words | [Link](https://leetcode.com/problems/replace-words/) | [Link](./lib/medium/648_replace_words.rb) | [Link](./test/medium/test_648_replace_words.rb) |
605605
| 652. Find Duplicate Subtrees | [Link](https://leetcode.com/problems/find-duplicate-subtrees/) | [Link](./lib/medium/652_find_duplicate_subtrees.rb) | [Link](./test/medium/test_652_find_duplicate_subtrees.rb) |
606606
| 654. Maximum Binary Tree | [Link](https://leetcode.com/problems/maximum-binary-tree/) | [Link](./lib/medium/654_maximum_binary_tree.rb) | [Link](./test/medium/test_654_maximum_binary_tree.rb) |
607+
| 659. Split Array into Consecutive Subsequences | [Link](https://leetcode.com/problems/split-array-into-consecutive-subsequences/) | [Link](./lib/medium/659_split_array_into_consecutive_subsequences.rb) | [Link](./test/medium/test_659_split_array_into_consecutive_subsequences.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.9.1'
8+
s.version = '7.4.0'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/split-array-into-consecutive-subsequences/
4+
# @param {Integer[]} nums
5+
# @return {Boolean}
6+
def is_possible(nums)
7+
count = ::Hash.new(0)
8+
nums.each { |num| count[num] += 1 }
9+
tail = ::Hash.new(0)
10+
11+
nums.each do |num|
12+
next if count[num].zero?
13+
14+
count[num] -= 1
15+
16+
if tail[num - 1].positive?
17+
tail[num] += 1
18+
tail[num - 1] -= 1
19+
elsif count[num + 1].positive? && count[num + 2].positive?
20+
count[num + 1] -= 1
21+
count[num + 2] -= 1
22+
tail[num + 2] += 1
23+
else
24+
return false
25+
end
26+
end
27+
28+
true
29+
end
30+
31+
# count = Hash.new { 0 }
32+
# nums.each { |num| count[num] += 1 }
33+
# tail = Hash.new { 0 }
34+
35+
# nums.each do |num|
36+
# next if count[num] == 0
37+
38+
# count[num] -= 1
39+
40+
# if tail[num-1] > 0
41+
# tail[num] += 1
42+
# tail[num-1] -= 1
43+
# elsif count[num+1] > 0 && count[num+2] > 0
44+
# count[num+1] -= 1
45+
# count[num+2] -= 1
46+
# tail[num+2] += 1
47+
# else
48+
# return false
49+
# end
50+
# end
51+
52+
# true
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/659_split_array_into_consecutive_subsequences'
5+
require 'minitest/autorun'
6+
7+
class SplitArrayIntoConsecutiveSubsequencesTest < ::Minitest::Test
8+
def test_default_one = assert(is_possible([1, 2, 3, 3, 4, 5]))
9+
10+
def test_default_two = assert(is_possible([1, 2, 3, 3, 4, 4, 5, 5]))
11+
12+
def test_default_three = assert(!is_possible([1, 2, 3, 4, 4, 5]))
13+
end

0 commit comments

Comments
 (0)