Skip to content

Commit 50b6011

Browse files
committed
Solve problems.
1 parent 8775d8b commit 50b6011

File tree

5 files changed

+118
-47
lines changed

5 files changed

+118
-47
lines changed

lib/Problems/daily-coding-problems/find_min_max_ary.rb

Lines changed: 0 additions & 35 deletions
This file was deleted.

lib/Problems/daily-coding-problems/problem1.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,32 @@
99
#
1010
class Problem1
1111
def self.solution(values, k)
12+
return store_seen(values, k)
13+
end
14+
15+
# Time complexity: O(N) => Lookup in hash is O(1)
16+
# Space complexity: O(N)
17+
def self.store_seen(values, k)
18+
seen = {}
19+
values.each do |value|
20+
if(seen[k - value])
21+
return true
22+
end
23+
seen[value]=true
24+
end
25+
return false
26+
end
27+
28+
# Time complexity: O(N^2)
29+
# Space complexity: O(1)
30+
def self.brute_fource(values, ke)
1231
size = values.size
1332
if(size <= 1 || k < 1)
1433
return false
1534
elsif(size == 2)
1635
return (values[0] + values[1]) == k
1736
end
37+
1838
(0...size).each do |i|
1939
(1...size).each do |j|
2040
if(values[i]+values[j] == k)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Given an array of numbers of length N, find both the minimum and maximum using less than 2 * (N - 2) comparisons.
2+
3+
class Problem235
4+
5+
# Non Recursive
6+
def self.non_recursive_solution(values, comparisons)
7+
if(values.size == 1)
8+
return values[0], values[0], comparisons
9+
end
10+
if(values.size == 2)
11+
return (values[0] < values[1]) ? [values[0], values[1], comparisons+1] : [values[1], values[0], comparisons+1]
12+
end
13+
min = max = values[0]
14+
compare = lambda { |x,y| x < y ? [x, y] : [y, x] }
15+
values.each_slice(2) do |arr|
16+
comparisons += 1
17+
smaller, bigger = compare.call(arr[0], arr[1])
18+
comparisons += 1
19+
min = min(min, smaller)
20+
comparisons += 1
21+
max = max(max, bigger)
22+
end
23+
return min, max, comparisons
24+
end
25+
# Recursive
26+
def self.recursive_solution(values, comparisons)
27+
if(values.size == 1)
28+
return values[0], values[0], comparisons
29+
elsif(values.size == 2)
30+
return (values[0] < values[1]) ? [values[0], values[1], comparisons+1] : [values[1], values[0], comparisons+1]
31+
else
32+
n = values.size / 2
33+
lmin, lmax, comparisons = recursive_solution(values[0...n], comparisons+1)
34+
rmin, rmax, comparisons = recursive_solution(values[n...values.size], comparisons+1)
35+
return min(lmin, rmin), max(lmax, rmax), comparisons
36+
end
37+
end
38+
39+
def self.min(x, y)
40+
x < y ? x : y
41+
end
42+
43+
def self.max(x, y)
44+
x > y ? x : y
45+
end
46+
47+
def self.find_min_max_in_ary(values)
48+
min, max = 0, 0
49+
comparisons = 0
50+
comparisons += 1
51+
if(values.size == 1)
52+
min = values[0]
53+
max = values[0]
54+
return min, max
55+
end
56+
comparisons += 1
57+
if(values[0] <= values[1])
58+
min = values[0]
59+
max = values[1]
60+
else
61+
min = values[1]
62+
max = values[0]
63+
end
64+
values.shift
65+
values.shift
66+
values.each do |value|
67+
comparisons += 1
68+
if(value < min)
69+
min = value
70+
end
71+
comparisons += 1
72+
if(value > max)
73+
max = value
74+
end
75+
end
76+
[min, max, comparisons]
77+
end
78+
end

spec/Problems/daily-coding-problems/find_min_max_ary_spec.rb

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
describe Problem235 do
2+
3+
# Given an array of numbers of length N, find both the minimum and
4+
# maximum using less than 2 * (N - 2) comparisons.
5+
describe '#non_recursive_solution' do
6+
it 'return min and max value of array with "2 * (n-1)" comparison' do
7+
values = [9, 2, 4, 6, 10, 1, 11, -1, 3, 5]
8+
comparisons = 3 * (values.length / 2)
9+
expect(Problem235.non_recursive_solution(values, 0)).to eq([-1, 11, comparisons])
10+
end
11+
end
12+
13+
describe '#recursive_solution' do
14+
it 'return min and max value of array with "2 * (n-1)" comparison' do
15+
values = [9, 2, 4, 6, 10, 1, 11, -1]
16+
comparisons = 3 * (values.length / 2) - 2
17+
expect(Problem235.recursive_solution(values, 0)).to eq([-1, 11, comparisons])
18+
end
19+
end
20+
end

0 commit comments

Comments
 (0)