Skip to content

Commit 9f0c545

Browse files
Merge pull request #207 from theforestvn88/jan
2375
2 parents 148e904 + 612ecf9 commit 9f0c545

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# @param {String} pattern
2+
# @return {String}
3+
def smallest_number(pattern, result = [], i = -1, prev_num = 0, prev_pat = "I", used = Array.new(10, false))
4+
return result.join if i == pattern.size
5+
6+
(1..9).each { |j|
7+
if !used[j] && (prev_pat == "D" ? (j < prev_num) : (j > prev_num))
8+
result << j
9+
used[j] = true
10+
11+
found = smallest_number(pattern, result, i + 1, j, pattern[i + 1], used)
12+
return found unless found.nil?
13+
14+
result.pop
15+
used[j] = false
16+
end
17+
}
18+
19+
nil
20+
end

0 commit comments

Comments
 (0)