mirror of
https://gitlab.com/mfocko/CodeWars.git
synced 2024-11-22 00:23:47 +01:00
5kyu: add best travel
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
e394821b7b
commit
b4062b40c8
2 changed files with 28 additions and 0 deletions
24
5kyu/best_travel/solution.rb
Normal file
24
5kyu/best_travel/solution.rb
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
def choose_best_sum(t, k, ls)
|
||||||
|
def choose_best_sum_rec(max_total_distance, n, distances)
|
||||||
|
if n == 0 && max_total_distance >= 0 then
|
||||||
|
return 0
|
||||||
|
elsif max_total_distance < 0 || distances.size < 1 then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
found = Array.new(2)
|
||||||
|
|
||||||
|
travelled = distances.pop
|
||||||
|
including_last = choose_best_sum_rec(max_total_distance - travelled, n - 1, distances)
|
||||||
|
if including_last != nil then
|
||||||
|
found[0] = including_last + travelled
|
||||||
|
end
|
||||||
|
|
||||||
|
found[1] = choose_best_sum_rec(max_total_distance, n, distances)
|
||||||
|
distances.push(travelled)
|
||||||
|
|
||||||
|
return found.select { |x| x != nil }.max
|
||||||
|
end
|
||||||
|
|
||||||
|
choose_best_sum_rec(t, k, ls.sort)
|
||||||
|
end
|
|
@ -113,6 +113,10 @@
|
||||||
|
|
||||||
- [Esolang Interpreters #2 - Custom Smallfuck Interpreter](https://www.codewars.com/kata/58678d29dbca9a68d80000d7) - [solution](5kyu/esolang_interpreters_ii)
|
- [Esolang Interpreters #2 - Custom Smallfuck Interpreter](https://www.codewars.com/kata/58678d29dbca9a68d80000d7) - [solution](5kyu/esolang_interpreters_ii)
|
||||||
|
|
||||||
|
### Ruby
|
||||||
|
|
||||||
|
- [Best travel](https://www.codewars.com/kata/55e7280b40e1c4a06d0000aa) - [solution](5kyu/best_travel)
|
||||||
|
|
||||||
## 6 kyu
|
## 6 kyu
|
||||||
|
|
||||||
### Rust
|
### Rust
|
||||||
|
|
Loading…
Reference in a new issue