Initial import

This commit is contained in:
Glenn Y. Rolland 2022-08-21 16:00:40 +02:00
commit 1ea977ed0c
3 changed files with 134 additions and 0 deletions

24
binary-gap/solution.rb Normal file
View File

@ -0,0 +1,24 @@
def solution(n)
gap = 0
maxgap = 0
bin = []
# revert number
while n > 0 do
bin.unshift(n%2)
n = (n>>1)
end
# count
bin.each do |b|
if b != 0 then
maxgap = gap if gap > maxgap
gap = 0
next
end
gap += 1
end
return maxgap
end

View File

@ -0,0 +1,13 @@
def solution(a)
return 1 if a.empty?
res = 1
a.sort!
a.each do |i|
res += 1 if res == i
end
return res
end

97
tmp/solution.rb Normal file
View File

@ -0,0 +1,97 @@
# Integers K, M and a non-empty array A consisting of N integers, not bigger than M, are given.
#
# The leader of the array is a value that occurs in more than half of the elements of the array, and the segment of the array is a sequence of consecutive elements of the array.
#
# You can modify A by choosing exactly one segment of length K and increasing by 1 every element within that segment.
#
# The goal is to find all of the numbers that may become a leader after performing exactly one array modification as described above.
#
# Write a function:
#
# def solution(k, m, a)
#
# that, given integers K and M and an array A consisting of N integers, returns an array of all numbers that can become a leader, after increasing by 1 every element of exactly one segment of A of length K. The returned array should be sorted in ascending order, and if there is no number that can become a leader, you should return an empty array. Moreover, if there are multiple ways of choosing a segment to turn some number into a leader, then this particular number should appear in an output array only once.
#
# For example, given integers K = 3, M = 5 and the following array A:
#
# A[0] = 2
# A[1] = 1
# A[2] = 3
# A[3] = 1
# A[4] = 2
# A[5] = 2
# A[6] = 3
# the function should return [2, 3]. If we choose segment A[1], A[2], A[3] then we get the following array A:
#
# A[0] = 2
# A[1] = 2
# A[2] = 4
# A[3] = 2
# A[4] = 2
# A[5] = 2
# A[6] = 3
# and 2 is the leader of this array. If we choose A[3], A[4], A[5] then A will appear as follows:
#
# A[0] = 2
# A[1] = 1
# A[2] = 3
# A[3] = 2
# A[4] = 3
# A[5] = 3
# A[6] = 3
# and 3 will be the leader.
#
# And, for example, given integers K = 4, M = 2 and the following array:
#
# A[0] = 1
# A[1] = 2
# A[2] = 2
# A[3] = 1
# A[4] = 2
# the function should return [2, 3], because choosing a segment A[0], A[1], A[2], A[3] and A[1], A[2], A[3], A[4] turns 2 and 3 into the leaders, respectively.
#
# Write an efficient algorithm for the following assumptions:
#
# N and M are integers within the range [1..100,000];
# K is an integer within the range [1..N];
# each element of array A is an integer within the range [1..M].
# K := integer
# M := integer
# A := Array of N integers where n in 1..M
require 'set'
def solution(k, m, a)
# results
res = Set.new
# arrays of arrays
subs = []
# 1. create sub-arrays
(a.size - k + 1).times do |idx|
subs.push [idx, idx+k-1]
end
# 2. increase sub-arrays
# 3. ... and count values
subs.each do |left, right|
count = {}
mod = a.clone.map.with_index do |v, idx|
if idx >= left and idx <= right
count[v+1] ||= 0
count[v+1] += 1
v += 1
else
count[v] ||= 0
count[v] += 1
v
end
end
m = count.keys.max_by {|k| count[k]}
res.add(m)
mod
end
res.to_a
end