- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0904-fruit-into-baskets.rb
42 lines (32 loc) · 820 Bytes
/
0904-fruit-into-baskets.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true
# 904. Fruit Into Baskets
# https://leetcode.com/problems/fruit-into-baskets
# @param {Integer[]} fruits
# @return {Integer}
deftotal_fruit(fruits)
left=0
max_fruit=0
hash={}
hash.default=0
fruits.each_with_indexdo |fruit,right|
hash[fruit] += 1
whilehash.length > 2
hash[fruits[left]] -= 1
hash.delete(fruits[left])ifhash[fruits[left]].zero?
left += 1
end
max_fruit=[max_fruit,right - left + 1].max
end
max_fruit
end
# ********************#
# TEST #
# ********************#
require"test/unit"
classTest_total_fruit < Test::Unit::TestCase
deftest_
assert_equal3,total_fruit([1,2,1])
assert_equal3,total_fruit([0,1,2,2])
assert_equal4,total_fruit([1,2,3,2,2])
end
end