Skip to content

Commit ecf0c02

Browse files
author
jiangl
committed
add some september's ruby solution
1 parent 140201a commit ecf0c02

File tree

12 files changed

+266
-0
lines changed

12 files changed

+266
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def largest_time_from_digits(a)
2+
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
3+
4+
return "" if out == nil
5+
out.to_s.rjust(4, '0').insert(2, ":")
6+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def contains_nearby_almost_duplicate(nums, k, t)
2+
length = nums.length
3+
out = false
4+
0.upto(length-1) do |i|
5+
(i+1).upto([length-1,i+k].min) do |n|
6+
7+
return true if (nums[n] - nums[i]).abs <= t
8+
9+
end
10+
end
11+
out
12+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def repeated_substring_pattern(s)
2+
out = false
3+
length = s.length / 2
4+
0.upto(length) do |n|
5+
out = is_repeated_sub?(s[0..n], s)
6+
return out if out == true
7+
end
8+
out
9+
10+
end
11+
12+
def is_repeated_sub?(sub, string)
13+
modulo = string.length % sub.length
14+
return false if modulo != 0
15+
multiplicator = string.length / sub.length
16+
return false if multiplicator <= 1
17+
return (sub * multiplicator) == string
18+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def partition_labels(s)
2+
last_indices = Array.new(26)
3+
s.each_char.with_index do |val, index|
4+
last_indices[val.ord - "a".ord] = index
5+
end
6+
last_indices
7+
start = 0
8+
finish = 0
9+
out = []
10+
11+
p last_indices
12+
0.upto((s.length)-1) do |n|
13+
val = s[n]
14+
finish = [finish, last_indices[val.ord - "a".ord]].max
15+
if finish == n
16+
17+
out << finish-start + 1
18+
start = finish + 1
19+
end
20+
end
21+
out
22+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
def get_all_elements(root1, root2)
2+
3+
arr_1 = in_order(root1, [])
4+
arr_2 = in_order(root2, [])
5+
res = []
6+
7+
len1 = arr_1.length
8+
len2 = arr_2.length
9+
index_1 = 0
10+
index_2 = 0
11+
12+
while (index_1 <= (len1 -1)) ||
13+
(index_2 <= (len2 -1))
14+
15+
if arr_1[index_1].nil? && !arr_2[index_2].nil?
16+
res << arr_2[index_2]
17+
index_2 += 1
18+
elsif !arr_1[index_1].nil? && arr_2[index_2].nil?
19+
res << arr_1[index_1]
20+
index_1 += 1
21+
22+
elsif arr_1[index_1] < arr_2[index_2]
23+
res << arr_1[index_1]
24+
index_1 += 1
25+
26+
else
27+
res << arr_2[index_2]
28+
index_2 += 1
29+
end
30+
31+
end
32+
33+
34+
res
35+
36+
end
37+
38+
def in_order(root, arr)
39+
if root.nil?
40+
return arr
41+
else
42+
in_order(root.left, arr)
43+
arr << root.val
44+
in_order(root.right, arr)
45+
end
46+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
def largest_overlap(a, b)
2+
3+
# c'est une solution que je n'ai pas tout comprise
4+
# pk [max_overlaps, shift_count(x, y, a, b)].max et [max_overlaps, shift_count(x, y, b, a)].max suffisent ??
5+
max_overlaps = 0
6+
7+
len = a.length
8+
9+
0.upto(len-1) do |y|
10+
0.upto(len-1) do |x|
11+
max_overlaps = [max_overlaps, shift_count(x, y, a, b)].max
12+
max_overlaps = [max_overlaps, shift_count(x, y, b, a)].max
13+
end
14+
end
15+
max_overlaps
16+
17+
end
18+
19+
20+
21+
def shift_count(x, y, m, r)
22+
len = m.length
23+
count = 0
24+
r_row = 0
25+
26+
y.upto(len - 1) do |m_row|
27+
r_col = 0
28+
29+
x.upto(len - 1) do |m_col|
30+
count += 1 if m[m_row][m_col] == 1 && m[m_row][m_col] == r[r_row][r_col]
31+
r_col += 1
32+
end
33+
34+
r_row +=1
35+
end
36+
count
37+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def word_pattern(pattern, str)
2+
arr = str.split
3+
return false if pattern.length != arr.length
4+
l = pattern.length
5+
h = {}
6+
out = true
7+
0.upto(l-1) do |n|
8+
9+
if h.key?(pattern[n])
10+
return out = false if h[pattern[n]] != arr[n]
11+
else
12+
return false if h.values.include?(arr[n])
13+
h.merge!(pattern[n] => arr[n])
14+
end
15+
end
16+
out
17+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def sum_root_to_leaf(root)
2+
rec(root, 0)
3+
end
4+
5+
def rec(root, val)
6+
if root.nil?
7+
return 0
8+
else
9+
val = val * 2 + root.val
10+
if root.left.nil? && root.right.nil?
11+
return val
12+
end
13+
rec(root.left, val) + rec(root.right, val)
14+
15+
end
16+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def compare_version(version1, version2)
2+
arr1 = version1.split(".")
3+
arr2 = version2.split(".")
4+
5+
len = [arr1.length-1, arr2.length-1].max
6+
7+
0.upto(len) do |n|
8+
if arr1[n].nil? && arr2[n].nil?
9+
return 0
10+
else
11+
a = arr1[n] || "0"
12+
b = arr2[n] || "0"
13+
if a.to_i > b.to_i
14+
return 1
15+
elsif a.to_i < b.to_i
16+
return -1
17+
else
18+
next
19+
end
20+
end
21+
end
22+
23+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
def get_hint(secret, guess)
2+
new_guess = []
3+
new_secret = []
4+
count_a = 0
5+
count_b = 0
6+
7+
guess.chars.to_a.each_with_index do |val, index|
8+
if val == secret[index]
9+
count_a += 1
10+
else
11+
new_guess << val
12+
new_secret << secret[index]
13+
end
14+
end
15+
16+
new_guess.sort!
17+
new_secret.sort!
18+
while new_guess != [] && new_secret != []
19+
s = new_secret.pop
20+
g = new_guess.pop
21+
if s == g
22+
count_b += 1
23+
elsif s.to_i > g.to_i
24+
new_guess << g
25+
elsif s.to_i < g.to_i
26+
new_secret <<s
27+
end
28+
end
29+
count_a.to_s + "A" + count_b.to_s + "B"
30+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def max_product(nums)
2+
3+
min = []
4+
min[0] = nums[0]
5+
max = []
6+
max[0] = nums[0]
7+
8+
nums.each_with_index do |val, n|
9+
10+
next if n == 0
11+
min[n] = [val, val * min[n-1], val * max[n-1]].min
12+
max[n] = [val, val * min[n-1], val * max[n-1]].max
13+
14+
end
15+
max.max
16+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def combination_sum3(k, n)
2+
path = []
3+
res = []
4+
bt(k, n, 1, path, res)
5+
res
6+
7+
end
8+
9+
def bt(k, n, s, path, res)
10+
if k == 0
11+
if n == 0
12+
res << path.clone
13+
return
14+
end
15+
end
16+
17+
s.upto(9) do |i|
18+
return if i > n
19+
path << i
20+
bt(k-1, n-i, i+1, path, res)
21+
path.pop
22+
end
23+
end

0 commit comments

Comments
 (0)