You are given N disks and two rods, each with one initial disk.
On the left rod, disks can be placed in decreasing order of size (smaller disks on top of bigger ones). On the right rod, disks can be placed in increasing order of size (bigger disks on top of smaller ones). Note that it is not permissible to place two disks of equal size on top of each other. The initial disks cannot be moved.
Write a function:
def solution(a, l, r)
that, given an array A of integers representing the sizes of the N disks and two integers L and R representing the size of the initial disks on the left and right rods respectively, returns the maximum number of disks from A that can be placed on the rods while satisfying the rules presented above.
Examples:
1. Given A = [2, 3, 3, 4], L = 3 and R = 1, your function should return 3, since only three disks can be placed on the rods. Note that the disk of size 2 can be placed on either the left rod or the right rod.
2. Given A = [1, 4, 5, 5], L = 6 and R = 4, your function should return 4.
3. Given A = [5, 2, 5, 2], L = 8 and R = 1, your function should return 4.
4. Given A = [1, 5, 5], L = 2 and R = 4, your function should return 2.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..50,000];
- each element of array A is an integer within the range [1..1,000,000,000];
- L and R are integers within the range [1..1,000,000,000].
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
a.sort!
count = 0
count_started = false
a.each_with_index do |val,i|
if count_started and val != a[i-1]
count += 1
next
end
if val
end
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
a.sort!
count = 0
count_started = false
a.each_with_index do |val,i|
if count_started and val != a[i-1]
count += 1
next
end
if val >
end
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
a.sort!
count = 0
count_started = false
a.each_with_index do |val,i|
if count_started and val != a[i-1]
count += 1
next
end
if val >
end
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val]
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] += 1
next
end
end
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] >
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] += 1
next
end
end
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] > 2
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] += 1
next
end
end
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] > 2
next
end
if hash[val]
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] += 1
next
end
end
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] > 2
next
end
if hash[val] and hash[val] == 1
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] += 1
next
end
end
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] > 2
next
end
if hash[val] and hash[val] == 1
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] += 1
next
end
end
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] > 2
next
end
if hash[val] and hash[val] == 1
hash[val] += 1
if val > r and val < l
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] += 1
next
end
end
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] > 2
next
end
if hash[val] and hash[val] == 1
hash[val] += 1
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] += 1
next
end
end
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] > 2
next
end
if hash[val] and hash[val] == 1
hash[val] += 1
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] += 1
next
end
end
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] > 2
next
end
if hash[val] and hash[val] == 1
hash[val] += 1
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] += 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] > 2
next
end
if hash[val] and hash[val] == 1
hash[val] += 1
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] += 1
next
end
end
count
end
exec.rb:21:in `block in solution': undefined method `+' for nil:NilClass (NoMethodError) from exec.rb:8:in `each' from exec.rb:8:in `solution' from exec.rb:139:in `<main>'
exec.rb:21:in `block in solution': undefined method `+' for nil:NilClass (NoMethodError) from exec.rb:8:in `each' from exec.rb:8:in `solution' from exec.rb:139:in `<main>'
exec.rb:21:in `block in solution': undefined method `+' for nil:NilClass (NoMethodError) from exec.rb:8:in `each' from exec.rb:8:in `solution' from exec.rb:139:in `<main>'
exec.rb:21:in `block in solution': undefined method `+' for nil:NilClass (NoMethodError) from exec.rb:8:in `each' from exec.rb:8:in `solution' from exec.rb:139:in `<main>'
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] > 2
next
end
if hash[val] and hash[val] == 1
hash[val] += 1
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] = 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] > 2
next
end
if hash[val] and hash[val] == 1
hash[val] += 1
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] = 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] >= 2
next
end
if hash[val] and hash[val] == 1
hash[val] += 1
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] = 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] >= 2
next
end
if hash[val] and hash[val] == 1
hash[val] += 1
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] = 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] >= 2
next
end
if hash[val] and hash[val] == 1
hash[val] += 1
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] >= 2
next
end
if hash[val] and hash[val] == 1
hash[val] += 1
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
if hash[val] and hash[val] >= 2
next
end
if hash[val] and hash[val] == 1
hash[val] += 1
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] = 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
hash
if hash[val] and hash[val] >= 2
next
end
if hash[val] and hash[val] == 1
hash[val] += 1
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] = 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
hash[val] = 0 if hash[val].nil?
hash[val] += 1
if hash[val] and hash[val] >= 2
next
end
if hash[val] and hash[val] == 1
hash[val] += 1
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] = 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
hash[val] = 0 if hash[val].nil?
hash[val] += 1
if hash[val] > 2
next
end
if hash[val] and hash[val] == 1
hash[val] += 1
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] = 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
hash[val] = 0 if hash[val].nil?
hash[val] += 1
if hash[val] > 2
next
end
if hash[val] == 2
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l) and hash[val].nil?
count += 1
hash[val] = 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
hash[val] = 0 if hash[val].nil?
hash[val] += 1
if hash[val] > 2
next
end
if hash[val] == 2
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l)
count += 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
hash[val] = 0 if hash[val].nil?
hash[val] += 1
if hash[val] > 2
next
end
if hash[val] == 2
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l)
count += 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
hash[val] = 0 if hash[val].nil?
hash[val] += 1
if hash[val] > 2
next
end
if hash[val] == 2
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l)
count += 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
hash[val] = 0 if hash[val].nil?
hash[val] += 1
if hash[val] > 2
next
end
if hash[val] == 2 and val > r and val < l
count += 1
next
end
if (val > r or val < l)
count += 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
hash[val] = 0 if hash[val].nil?
hash[val] += 1
if hash[val] > 2
next
end
if hash[val] == 2 and val > r and val < l
count += 1
next
end
if val > r or val < l
count += 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
hash[val] = 0 if hash[val].nil?
hash[val] += 1
if hash[val] > 2
next
end
if hash[val] == 2 and val > r and val < l
count += 1
next
end
if val > r or val < l
count += 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
hash[val] = 0 if hash[val].nil?
hash[val] += 1
if hash[val] > 2
next
end
if hash[val] == 2
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l)
count += 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
hash[val] = 0 if hash[val].nil?
hash[val] += 1
if hash[val] > 2
next
end
if hash[val] == 2
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l)
count += 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
hash[val] = 0 if hash[val].nil?
hash[val] += 1
if hash[val] > 2
next
end
if hash[val] == 2
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l)
count += 1
next
end
end
count
end
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, l, r)
# write your code in Ruby 2.2
count = 0
hash = {}
a.each do |val|
hash[val] = 0 if hash[val].nil?
hash[val] += 1
if hash[val] > 2
next
end
if hash[val] == 2
if val > r and val < l
count += 1
end
next
end
if (val > r or val < l)
count += 1
next
end
end
count
end
The solution obtained perfect score.
Small random tests, every value occurs at least twice. N = 100.