-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-StoneWall.rb
More file actions
98 lines (87 loc) · 2.09 KB
/
5-StoneWall.rb
File metadata and controls
98 lines (87 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# you can use puts for debugging purposes, e.g.
# puts "this is a debug message"
# incorrect and slow
# def solution(h)
# blocks = 0
# current = []
# h.each do |e|
# sum = current.any? ? current.inject(:+) : 0
# if sum > e
# current.each_with_index do |c, i|
# if e > c
# e -= c
# elsif e < c
# blocks += 1
# current[i] = e
# break
# else
# # use the same block
# current.slice!(i + 1)
# break
# end
# end
# elsif sum < e
# blocks += 1
# current.push e - sum
# else
# # use the same blocks
# end
# end
# blocks
# end
# correct, but slow
# def solution(h)
# blocks = 1
# current = [h.shift]
# h.each { |height|
# i = 0
# while current[i] and height >= current[i]
# height -= current[i]
# i += 1
# end
# current.slice!(i..-1)
# if height > 0
# current.push height
# blocks += 1
# end
# }
# blocks
# end
# correct solution
# def solution(h)
# stack = [0] * h.count
# num = 0
# blocks = 0
# h.each { |height|
# while num > 0 and stack[num - 1] > height
# num -= 1
# end
# if num == 0 or stack[num - 1] != height
# stack[num] = height
# num += 1
# blocks += 1
# end
# }
# blocks
# end
# a nicer solution
def solution(h)
stack = []
blocks = 0
h.each { |height|
while stack.any? and stack.last > height
stack.pop
end
if stack.empty? or stack.last != height
stack.push height
blocks += 1
end
}
blocks
end
# require "byebug"
# byebug
input = [8, 8, 5, 7, 9, 8, 7, 4, 8]
# input = (1..100_000).to_a
# input = (1..50_000).to_a + (1...50_000).to_a.reverse
puts solution(input)