Skip to content

Commit

Permalink
Merge pull request #283 from leijunjiang/master
Browse files Browse the repository at this point in the history
Add ruby solutions for september daily challenge.
  • Loading branch information
mincong-h authored Sep 13, 2020
2 parents 87bc969 + 9d6044f commit 92cde71
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def largest_time_from_digits(a)
out = a.permutation.to_a.map {|e| e.join("").to_i}.sort.select{|elt| elt < 2359 && elt.to_s[-2..-1] .to_i < 60}.last

return "" if out == nil
out.to_s.rjust(4, '0').insert(2, ":")
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def contains_nearby_almost_duplicate(nums, k, t)
length = nums.length
out = false
0.upto(length-1) do |i|
(i+1).upto([length-1,i+k].min) do |n|

return true if (nums[n] - nums[i]).abs <= t

end
end
out
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def repeated_substring_pattern(s)
out = false
length = s.length / 2
0.upto(length) do |n|
out = is_repeated_sub?(s[0..n], s)
return out if out == true
end
out

end

def is_repeated_sub?(sub, string)
modulo = string.length % sub.length
return false if modulo != 0
multiplicator = string.length / sub.length
return false if multiplicator <= 1
return (sub * multiplicator) == string
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def partition_labels(s)
last_indices = Array.new(26)
s.each_char.with_index do |val, index|
last_indices[val.ord - "a".ord] = index
end
last_indices
start = 0
finish = 0
out = []

p last_indices
0.upto((s.length)-1) do |n|
val = s[n]
finish = [finish, last_indices[val.ord - "a".ord]].max
if finish == n

out << finish-start + 1
start = finish + 1
end
end
out
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
def get_all_elements(root1, root2)

arr_1 = in_order(root1, [])
arr_2 = in_order(root2, [])
res = []

len1 = arr_1.length
len2 = arr_2.length
index_1 = 0
index_2 = 0

while (index_1 <= (len1 -1)) ||
(index_2 <= (len2 -1))

if arr_1[index_1].nil? && !arr_2[index_2].nil?
res << arr_2[index_2]
index_2 += 1
elsif !arr_1[index_1].nil? && arr_2[index_2].nil?
res << arr_1[index_1]
index_1 += 1

elsif arr_1[index_1] < arr_2[index_2]
res << arr_1[index_1]
index_1 += 1

else
res << arr_2[index_2]
index_2 += 1
end

end


res

end

def in_order(root, arr)
if root.nil?
return arr
else
in_order(root.left, arr)
arr << root.val
in_order(root.right, arr)
end
end
37 changes: 37 additions & 0 deletions September-LeetCoding-Challenge/06-Image-Overlap/Image-Overlap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
def largest_overlap(a, b)

# c'est une solution que je n'ai pas tout comprise
# pk [max_overlaps, shift_count(x, y, a, b)].max et [max_overlaps, shift_count(x, y, b, a)].max suffisent ??
max_overlaps = 0

len = a.length

0.upto(len-1) do |y|
0.upto(len-1) do |x|
max_overlaps = [max_overlaps, shift_count(x, y, a, b)].max
max_overlaps = [max_overlaps, shift_count(x, y, b, a)].max
end
end
max_overlaps

end



def shift_count(x, y, m, r)
len = m.length
count = 0
r_row = 0

y.upto(len - 1) do |m_row|
r_col = 0

x.upto(len - 1) do |m_col|
count += 1 if m[m_row][m_col] == 1 && m[m_row][m_col] == r[r_row][r_col]
r_col += 1
end

r_row +=1
end
count
end
17 changes: 17 additions & 0 deletions September-LeetCoding-Challenge/07-Word-Pattern/Word-Pattern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def word_pattern(pattern, str)
arr = str.split
return false if pattern.length != arr.length
l = pattern.length
h = {}
out = true
0.upto(l-1) do |n|

if h.key?(pattern[n])
return out = false if h[pattern[n]] != arr[n]
else
return false if h.values.include?(arr[n])
h.merge!(pattern[n] => arr[n])
end
end
out
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def sum_root_to_leaf(root)
rec(root, 0)
end

def rec(root, val)
if root.nil?
return 0
else
val = val * 2 + root.val
if root.left.nil? && root.right.nil?
return val
end
rec(root.left, val) + rec(root.right, val)

end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def compare_version(version1, version2)
arr1 = version1.split(".")
arr2 = version2.split(".")

len = [arr1.length-1, arr2.length-1].max

0.upto(len) do |n|
if arr1[n].nil? && arr2[n].nil?
return 0
else
a = arr1[n] || "0"
b = arr2[n] || "0"
if a.to_i > b.to_i
return 1
elsif a.to_i < b.to_i
return -1
else
next
end
end
end

end
30 changes: 30 additions & 0 deletions September-LeetCoding-Challenge/10-Bulls-and-Cows/Bulls-and-Cows.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def get_hint(secret, guess)
new_guess = []
new_secret = []
count_a = 0
count_b = 0

guess.chars.to_a.each_with_index do |val, index|
if val == secret[index]
count_a += 1
else
new_guess << val
new_secret << secret[index]
end
end

new_guess.sort!
new_secret.sort!
while new_guess != [] && new_secret != []
s = new_secret.pop
g = new_guess.pop
if s == g
count_b += 1
elsif s.to_i > g.to_i
new_guess << g
elsif s.to_i < g.to_i
new_secret <<s
end
end
count_a.to_s + "A" + count_b.to_s + "B"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def max_product(nums)

min = []
min[0] = nums[0]
max = []
max[0] = nums[0]

nums.each_with_index do |val, n|

next if n == 0
min[n] = [val, val * min[n-1], val * max[n-1]].min
max[n] = [val, val * min[n-1], val * max[n-1]].max

end
max.max
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def combination_sum3(k, n)
path = []
res = []
bt(k, n, 1, path, res)
res

end

def bt(k, n, s, path, res)
if k == 0
if n == 0
res << path.clone
return
end
end

s.upto(9) do |i|
return if i > n
path << i
bt(k-1, n-i, i+1, path, res)
path.pop
end
end

0 comments on commit 92cde71

Please sign in to comment.